PHP Unit Test Tool: PHPUnit deep usage (ii)

Source: Internet
Author: User
Tags array contains manual file system regular expression variable

In the previous PHP Unit Test tool: PHPUnit The article, we have a preliminary understanding of PHPUnit, in this article will continue to delve into the next phpunit some usages.

  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 ());
catch (Databseexception $e) {
$this->marktestskipped ("This test is skipped because there was a database problem");
}
}
?>

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 hours in the ' Day to have I tests go green ');
$trueVariable = true;
$this->asserttrue ($trueVariable);
}
?>

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
Class testable
{
Public $trueProperty = true;
Public $resetMe = true;
Public $testArray = Array (
"The" => 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 write is as follows:

<?php
Class Testabletest extends Phpunit_framework_testcase
{
Private $_testable = null;
Public Function setUp ()
{
$this->_testable = new testable ();
}
Public Function teardown ()
{
$this->_testable = null;
}
/** test methods'll go
}
?>

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");
}
?>

Asserttrue and Assertfalse have been introduced in the previous article, and here's a second argument, which means customizing the display information when the assertion's test fails. For example, in this test method, when Trueproperty is not a true value, the message "Trueproperty isn" is displayed.

Next look at the assertion usages that are phpunit in numerical terms:

<?php
Public Function Testvalueequals ()
{
$valueOne = 4;
$valueTwo = 2;
$this->assertequals ($this->_testable->addvalues ($valueOne, $valueTwo), 6);
}
Public Function Testvaluegreaterthan ()
{
$valueOne = 4;
$valueTwo = 2;
$this->assertgreaterthan ($valueTwo, $valueOne);
}
Public Function testlessthanorequal ()
{
$valueOne = 4;
$valueTwo = 2;
$this->assertlessthanorequal ($valueTwo, $valueOne);
}
Public Function testareobjectsequal ()
{
$testTwo = new testable ();
$this->_testable->resetme = false;
$this->assertequals ($this->_testable, $testTwo);
}
?>

Among them, assertequals to judge whether is equal, Assertgreaterthan to judge whether is greater than, assertlessthanorequal judge whether is less than or equal to, but assertequals here should notice, It can also be used to determine whether two objects are equal, for example, to determine whether an instance of this testable class is equal to the $testtwo of the newly set Resetme object.

In addition to the numerical assertion, there are a number of features in the character that can be asserted, looking at the following code:

<?php
Public Function teststringending ()
{
$testString = $this->_testable->getteststring ();
$this->assertstringendswith ("Frood", $testString);
}
Public Function Teststringstarts ()
{
$testString = $this->_testable->getteststring ();
$this->assertstringstartswith ("Hoopy", $testString);
}
Public Function testequalfilecontents ()
{
$this->assertstringequalsfile ("/path/to/textfile.txt", "foo");
}
Public Function Testdoesstringmatchformat ()
{
$testString = $this->_testable->getteststring ();
$this->assertstringmatchesformat ("%s", $testString);
}
?>

Where the Assertstringstartswith assertion is to determine whether the string begins with the specified string, Assertstringendswith asserts whether the string ends with the specified string. The Assertstringequalsfile assertion determines whether a given file contains a specified character, for example, to determine whether the file contains string foo in Textfile.txt.

Assertstringmatchesformat allows the user to specify a matching pattern to determine whether a string meets the requirements, such as $this->assertstringmatchesformat ("%s", $testString);

Here is to determine whether $teststring is a string type, specific reference to the PHPUnit manual.

Let's look at the following code:

<?php
Public Function Teststringisnotnull ()
{
$notANull = "I" m not a null! ";
$this->assertnull ($notANull);
}
Public Function Teststringissame ()
{
$numberAsString = "1234";
$this->assertsame (1234, $numberAsString);
}
?>

Where assertnull determines whether a variable is null, and assertsame strictly determines whether two variables are the same type, although in PHP is a weak type language, but here through the assertsame can still determine $numberasstring as a string type Does not match the expected 1234-digit type, so the test does not pass.

Finally, let's take a look at assertions that might not be commonly used, but it might be very helpful for your unit test work to see the code as follows:

<?php
Public Function testarraykeyexists ()
{
$this->assertarrayhaskey ("A", $this->_testable->testarray);
}
Public Function testattributeexists ()
{
$this->assertclasshasattribute ("Resetme", Get_class ($this->_testable));
}
Public Function Testfileisreal ()
{
$this->assertfileexists ("/path/to/file.txt");
}
Public Function testisinstance ()
{
$this->assertinstanceof ("Otherclass", $this->_testable);
}
<?php
Public Function Testdoesmatchregex ()
{
$testString = $this->_testable->getteststring ();
$this->assertregexp ("/[a-z]+/", $testString);
}
?>

The first assertion in the code, Assertarrayhaskey, is used to check whether every key value in an array exists, as in our array, the value of "Firstkey" is corresponding to the key 1, so the test passes. And Assertclasshasattribute can determine whether a class has a corresponding attribute, in this case the test can also pass;

Assertfileexists, however, determines whether the specified file exists in the local file system. Assertinstanceof, however, determines whether an object you are creating is an instance of a class. Assertregexp believe everyone knows that this is to determine whether a string matches a given regular expression.

  Summarize

This paper further discusses some important methods and assertions in PHPUnit, PHPUnit also has a lot of rich assertions, to improve unit testing is very helpful, please refer to the PHPUnit user manual.




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.