PHP Unit testing tool PHPUNIT in-depth usage (2) page 1/2

Source: Internet
Author: User
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 ");
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.