PHP unit testing tool PHPUNIT deep usage (3) page 1/2

Source: Internet
Author: User

In this article, I will introduce you to the two advanced concepts and usage in phpunit, although it is not necessarily used in your daily unit testing, however, understanding and learning their usage is very important for learning phpunit.

  Annotations in Phpunit

Developers with experience in other programming languages should be familiar with Annotations (Annotations). In fact, in phpunit, a simple comment below can also be considered as Annotations:

<? Php
Class MyTestClass Extends PHPUnit_Framework_TestCase
{
/* *
* Testing the answer to "do you love unit tests ?"
*/
Public Function TestDoYouLoveUnitTests ()
{
$ Love = True ;
$ This -> AssertTrue ( $ Love );
}
}
?>

As you can see, a piece of text marked with/***/can be considered as an Annotations. However, Annotations are not just simple Annotations, it is the annotation of information or metadata associated with a program element. It does not affect the running of the program, but the related software tools or frameworks can convert it into special metadata tags, to help developers improve efficiency with less Code (for example, through. If you are familiar with Java, you will find that Annotations are widely used in Java SE 5 and frameworks such as Spring.

However, because php is not a compilation language like Java, it lacks the Annotations parsing mechanism. Fortunately, phpunit provides such a function. The following code is used as an example:

<? Php
Class MyMathClass
{
/* *
* Add two given values together and return sum
*/
Public Function AddValues ( $ , $ B )
{
Return $ + $ B ;
}
}
?>

The above is just a simple addition example. For this reason, we use Annotations to write a unit test. In the last two articles, we used the method of manually writing unit tests, this article describes how to use the phpunit command line to automatically generate a unit test framework. The method is as follows:

First, save the above class as MyMathClass. php, and then run the following command on the command line:

Phpunit-skeleton - Test MyMathClass

Phpunit automatically generates the following framework unit test code:

<? Php
Require_once ' /Path/to/MyMathClass. php ' ;
/* *
* Test class for MyMathClass.
* Generated by PHPUnit on at 12:22:07.
*/
Class MyMathClassTest Extends PHPUnit_Framework_TestCase
{
/* *
* @ Var MyMathClass
*/
Protected $ Object ;
/* *
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
Protected Function SetUp ()
{
$ This -> Object = New MyMathClass;
}
/* *
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
Protected Function TearDown ()
{
}
/* *
* @ Todo Implement testAddValues ().
*/
Public Function TestAddValues ()
{
// Remove the following lines when you implement this test.
$ This -> MarkTestIncomplete (
' This test has not been implemented yet. '
);
}
}
?>

As you can see, phpunit automatically introduces the original MyMathClass for the unit test code we generated. php also generates setUp and tearDown methods, but the only core unit test code is left for us to write. If we want to generate the unit test code more quickly on this basis, how can we implement it? That's right. It's just using annotations! We can add the following annotations to the original MyMathClass. php.

<? Php
Class MyMathClass
{
/* *
* Add two given values together and return sum
* @ Assert (1, 2) = 3
*/
Public Function AddValues ( $ , $ B )
{
Return $ + $ B ;
}
}
?>

Then run the following command in the command line as above:

Phpunit-skeleton - Test MyMathClass

At this time, the following unit test code will be generated for us:

<? Php
/* *
* Generated from @ assert (1, 2) = 3.
*/
Public Function TestAddValues ()
{
$ This -> AssertEquals (
3 ,
$ This -> Object -> AddValues ( 1 , 2 )
);
}
?>

Have you seen it? If the annotation @ assert () = 3 is added to the original class, phpunit automatically generates the correct unit test code for us. Of course, you can refer to the phpunit manual to learn more about the rules for using the @ assert annotation.

The following is an example of annotations. Assume that a method in our program only requires data input and does not rely on XML or the database to provide data sources. In order to test this method, one method we may think of is to set a test dataset in the program for testing, but here we will introduce a simple method, that is, to use annotation @ dataProvider to modify MyMathClass. php is as follows:

<? Php
/* *
* Data provider for test methods below
*/
Public Static Function Provider ()
{
Return Array (
Array ( 1 , 2 , 3 ) ,
Array ( 4 , 2 , 6 ) ,
Array ( 1 , 5 , 7 )
);
}
/* *
* Testing addValues returns sum of two values
* @ DataProvider provider
*/
Public Function TestAddValues ( $ , $ B , $ Sum )
{
$ This -> AssertEquals (
$ Sum ,
$ This -> Object -> AddValues ( $ , $ B )
);
}
?>

The annotation @ dataProvider is used to indicate that the data provider of the test case is an array returned by the provider method. Therefore, in unit test, the first element in the array is assigned to $ a, the second element is assigned to B, and the second element is assigned to sum. You can see that, the data provided by the above 3rd arrays cannot pass the unit test, because 1 + 5 is not equal to 7.

In addition, two commonly used annotations are briefly introduced. For example, the @ expectedException annotation can be used to test whether an exception is thrown correctly in the code, for example:

<? Phprequire_once ' PHPUnit/Framework. php ' ;
Class Predictiontest Extends PHPUnit_Framework_TestCase {
/* *
* @ ExpectedException InvalidArgumentException */
Public Function TestException (){
}
}

?>

The annotation method is used to indicate that the exception type that must be thrown in testException is InvalidArgumentException.

The other is the @ cover annotation. The function of phpunit is to identify only the methods or scopes in the class to generate test code, for example:

/* *
* @ Covers SampleClass: publicMethod
* @ Covers SampleClass: <! Public>
* @ Covers HelperClass <extended>
*/
Public Function TestMethod ()
{
$ Result = SampleClass :: Method ();
}

Phpunit only generates unit test code for the publicMethod method in the SampleClass class, all non-public declared methods in the SampleClass class, And the HelperClass class or one of its parent classes.

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.