In this article, the author will introduce two advanced concepts and usages in phpunit, although it is not necessarily used in your daily unit tests, but understanding and learning their usage is important for learning phpunit.
The annotations in the PHPUnit
If you have other programming language experience developers, should be annotations (annotation) is not unfamiliar, in fact, in PHPUnit, a simple example of the following note can also be considered annotations:
!--
code highlighting produced by Actipro Codehighlighter (freeware)
http://www. CODEHIGHLIGHTER.COM/
--> ;? PHP
class Mytestclass extends phpunit_framework_testcase
{
/* *
* Testing the answer to "Do Your Love unit tests?"
*/
public function testdoyouloveunittests ()
{
$love = true ;
$this asserttrue ( $love );
}
}
?
Can be seen, in fact, a paragraph with/** **/as the mark of the text, can be considered a kind of annotations, but annotations is not just a simple annotation, it is associated with a program element information or metadata annotation, it does not affect the operation of the program, But relevant software tools or frameworks can transform them into special metadata tokens to make it easier for developers to use less code to improve efficiency. If you are familiar with Java, you will find that annotations is used in a large number of frameworks, such as Java SE 5 and like spring.
However, since PHP is not a compiled language like Java, and therefore lacks the mechanism to parse annotations itself, fortunately phpunit to provide such functionality, we take the following code as an example:
? PHP
Class Mymathclass
{
/**
* Add two given values together and return sum
*/
Public function addvalues ($a,$b)
{
return $a+$b;
}
}
?>
The above is just a simple addition example, so we use annotations to write a unit test, in the last two articles, we use the method of hand-writing unit test, and in this article, we will introduce the method of using the PHPUnit command line to automatically generate the framework of unit test, The method is as follows:
First save the above class as mymathclass.php, and then run the following command at the command line:
Phpunit–skeleton-test mymathclass
At this point PHPUnit will automatically generate the following frame unit test code:
--> ?Php
Require_once '/path/to/mymathclass.php';
/**
* Test class for Mymathclass.
* Generated by PHPUnit to 2011-02-07 at 12:22:07.
*/
ClassMymathclasstestExtendsPhpunit_framework_testcase
{
/**
* @var Mymathclass
*/
Protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This 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 is called after the a test is executed.
*/
protected function teardown ()
{
}
/* *
* @ Todo implement Testaddvalues ().
*/
public function testaddvalues ()
{
// Remove the Following lines when your implement this test.
$this marktestincomplete (
' This test has not been implemented yet. '
);
}
??
As you can see, PHPUnit has automatically introduced the original mymathclass.php for the unit test code we generated, while also generating the setup and Teardown methods, but the only core unit test code is left for us to write. How do we do this if we want to build the unit test code that we want on this basis more quickly? Yes, that is, 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 ($a,$b)
{
return $a+$b;
}
}
?>
Then run at the command line as described above:
Phpunit–skeleton-test mymathclass
This will generate the following unit test code for US:
? PHP
/**
* Generated from @assert (1,2) = 3.
*/
Public function testaddvalues ()
{
$this->assertequals (
3,
$this->object->addvalues (1,2)
);
}
?>
See that? We added the annotation @assert (1,2) ==3 to the original class, then phpunit automatically generated the correct unit test code for us. Of course, you can refer to the PHPUnit manual to learn more about the rules used in @assert annotations.
Here is another example to explain annotations. Suppose a method in our program just needs input of data and does not rely on XML or database to provide a data source, in order to test this method, one of the ways we might think of is to set up a test dataset in the program to test, but here is a simpler way to do this. is to use the annotation @dataprovider, modify the mymathclass.php as follows:
--> ?Php
/**
* Data Provider for test methods below
*/
Public Static functionProvider ()
{
Return Array(
Array(1,2, 3 ) ,
array ( 4 , 2 , 6 ) ,
Array ( 1 , 5 , 7 )
);
}
/* *
* testing addvalues returns sum of two values
* @dataProvider provider
*/
Span>public function testaddvalues ( $a , $b , $sum )
{
$this assertequals (
$sum ,
$this , object addvalues ( $a , $b )
);
}
.
As you can see, the annotation @dataprovider is used here to indicate that the data provider for the test case is an array returned by the provider method. So in unit testing, the No. 0 element in the array is assigned to $a, and the 1th element is assigned to B, and the 3rd element is assigned to sum, and you can see that the data provided by the 3rd array above cannot be tested by unit because the 1+5 is not equal to 7.
In addition, here is a brief introduction to two commonly used annotations, such as @expectedexception annotations can test whether the code correctly throws an exception, such as:
? phprequire_once 'phpunit/framework.php';
Class Exceptiontest extends phpunit_framework_testcase{
/**
* @expectedException invalidargumentexception * *
Public function testexception () {
}
}
?>
Here we use the annotation method to indicate that the exception type that must be thrown in TestException is invalidargumentexception.
The other one is @cover annotation. Its purpose is to identify phpunit to generate test code for only those methods or scopes in a class, such as:
/**
* @covers SampleClass::p ublicmethod
* @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, for all non-public declared methods and Helperclass classes in the SampleClass class, or for one of its parent classes.
Current 1/2 page
12 Next read the full text