In the last PHP Unit test tool: PHPUNIT, we have a preliminary understanding of phpunit. in this article, we will continue to explain in depth some usage of phpunit. 1. in phpunit, markTestSkipped and markTestIncomplete have two useful methods: markTestSkipped and markTestIncomp.
In the last PHP Unit test tool: PHPUNIT, we have a preliminary understanding of phpunit. in this article, we will continue to explain in depth some usage of phpunit.
1. markTestSkipped and markTestIncomplete
In phpunit, there are two useful methods: markTestSkipped and markTestIncomplete. They allow you to write unit tests not only with pass and failure results. MarkTestSkipped does not allow PHPUNIT to execute a written test method. For example, the following program:
Public function testThisMightHaveADb ()
{
$ MyObject-> createObject ();
Try {
$ Db = new Database ();
$ This-> assertTrue ($ db-> rowExists ());
} Catch (DatabseException $ e ){
$ This-> markTestSkipped ('This test was skipped because there was a database problem ');
}
}
?>
In the above program, it is a test method to determine whether the data exists after connecting to the database. However, if the database connection exception is considered, when an exception is thrown, markTestSkipped indicates that the test method should be ignored because of exceptions. Note that the code you write may be correct, but an exception occurs, in this way, phpunit does not simply output fail.
MarkTestIncomplete is a bit similar, but a little different, it is used by developers when writing an unfinished test method, marking that a test method has not been completed yet, similarly, the test result is not fail, but the phpunit test method has not been completed. The example is as follows:
Public function testAreNotEnoughHours ()
{
$ This-> markTestIncomplete ("There aren't enough hours in the day to have my tests go green ");
$ TrueVariable = true;
$ This-> assertTrue ($ trueVariable );
}
?>
2. Learn more about the assertions in phpunit.
In the previous article, I have explained the use of some basic assertions in phpunit. here is an example. The following is a class code:
Class Testable
{
Public $ trueProperty = true;
Public $ resetMe = true;
Public $ testArray = array (
'First key' => 1,
'Second key' => 2
);
Private $ testString = "I do love me some strings ";
Public function _ construct ()
{
}
Public function addValues ($ valueOne, $ valueTwo ){
Return $ valueOne + $ valueTwo;
}
Public function getTestString ()
{
Return $ this-> testString;
}
}
?>
The initial framework of the unit test code we have compiled is as follows:
Class TestableTest extends PHPUnit_Framework_TestCase
{
Private $ _ testable = null;
Public function setUp ()
{
$ This-> _ testable = new Testable ();
}
Public function tearDown ()
{
$ This-> _ testable = null;
}
/** Test methods will go here */
}
?>
The setUp and tearDown methods have been introduced in the previous article. in the setUp method, the Testable () instance is created and saved in the variable $ _ testable, in the tearDown method, the object is destroyed.
Next, write some assertions to test. First, check assertTrue and assertFalase:
Public function testTruePropertyIsTrue ()
{
$ This-> assertTrue ($ this-> _ testable-> trueProperty, "trueProperty isn't true ");
}
Public function testTruePropertyIsFalse ()
{
$ This-> assertFalse ($ this-> _ testable-> trueProperty, "trueProperty isn't false ");
}
?>
The assertTrue and assertFalse parameters have been introduced in the previous article. here, we pay attention to the second parameter, which means that custom display information is displayed when the test of this asserted fails. For example, in this test method, when the trueProperty is not true, the information of "trueProperty isn' t true" is displayed.
Next, let's look at the example of phpunit's assertion in numerical terms: