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

Source: Internet
Author: User
PHP Unit testing tool PHPUNIT in-depth usage (2) 12th page. 1. markTestSkipped and markTestIncomplete in phpunit, there are two useful methods: markTestSkipped and markTestIncomplete. They allow you to write unit tests not only through 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:

Php
Public FunctionTestThisMightHaveADb ()
{
$ MyObject->CreateObject ();
Try{
$ Db = NewDatabase ();
$ 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:

Php
Public FunctionTestAreNotEnoughHours ()
{
$ 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:

Php
ClassTestable
{
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 FunctionAddValues ($ ValueOne,$ ValueTwo){
Return $ ValueOne+$ ValueTwo;
}
Public FunctionGetTestString ()
{
Return $ This->TestString;
}
}
?>

The initial framework of the unit test code we have compiled is as follows:

Php
ClassTestableTestExtendsPHPUnit_Framework_TestCase
{
Private $ _ Testable = Null;
Public FunctionSetUp ()
{
$ This->_ Testable= NewTestable ();
}
Public FunctionTearDown ()
{
$ 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:

Php
Public FunctionTestTruePropertyIsTrue ()
{
$ This->AssertTrue ($ This->_ Testable->TrueProperty,"TrueProperty isn' t true");
}
Public FunctionTestTruePropertyIsFalse ()
{
$ This->AssertFalse ($ This->_ Testable->TrueProperty, "TrueProperty isn' t false");
}
?>

Pipeline has two useful methods in phpunit: markTestSkipped and markTestIncomplete. They allow you to write unit tests not only pass...

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.