1, marktestskipped and Marktestincomplete
In PHPUnit, there are two useful methods marktestskipped and marktestincomplete. They can allow you to write a unit test that is not just a result of passing and failing. Marktestskipped allows PHPUnit not to perform a test method that has already been written. For example, let's say the following procedure:
--> ;? PHP
public function testthismighthaveadb ()
{
$myObject CreateObject ();
try {
$db = new Database ();
$this asserttrue ( $db , rowexists ());
 &NBSP} Catch (databseexception $e ) {
$this - marktestskipped ( ' This test is skipped because there was a database problem ' );
 &NBSP}
}
. ,
In the above program, it is a test method that determines whether the data exists after the database is connected, but if you consider the connection exception of the database, you should use marktestskipped to indicate that the test method should be ignored when you throw an exception, because there is an exception and when you notice At this point it is possible that you write the code is correct, but there is an exception, so that phpunit in the output will not simply output fail.
And Marktestincomplete is a bit similar, but a bit different is that it is used when a developer writes an unfinished test method that marks a test method that has not been written yet, and the same test result is not fail, Just tell PHPUnit this test method has not been written yet, as the following example:
? PHP
Public function testarenotenoughhours ()
{
$this->marktestincomplete ("There aren ' t enough hoursin the", "I have go green" c14>);
2, more in-depth understanding of the assertions in PHPUnit
In the previous article, there was a basic explanation for the use of assertions in some basic phpunit, here for example, the following is the code for a class:
--> ?Php
ClassTestable
{
Public $trueProperty = True;
Public $resetMe = True;
Public $testArray = Array(
'A-key' => 1,
'Second key' => 2
);
Private $testString = " I do love me some strings " ;
public function __construct ()
{
public function addvalues ( $valueOne , $valueTwo ) {
   &NBSP return $valueOne + $valueTwo ;
 &NBSP}
public function getteststring ()
{
  &NBSP return $this teststring
 &NBSP}
}
. ,
The initial framework of the unit test code we write is as follows:
--> ;? PHP
class testabletest extends phpunit_framework_testcase
{
private $_testable = null ;
public function setUp ()
{
$ This _testable = new testable ();
 &NBSP}
public function teardown ()
{
$this _testable = null ;
 &NBSP}
/* * Test methods'll go here */
}
In the previous article, the Setup method and the Teardown method have been introduced, where the Setup method creates a testable () instance and holds it in the variable $_testable, and in the Teardown method, the object is destroyed.
Next, start writing some assertions to test, first look at Asserttrue and assertfalase:
? PHP
Public function testtruepropertyistrue ()
{
$this->asserttrue ($this->_testable->trueproperty," trueproperty isn ' t true');
}
Public function testtruepropertyisfalse ()
{
$this->assertfalse ($this->_testable->trueproperty, "trueproperty isn ' t false");
}
?>
current 1/2 page
1 2 Next read the full text