what PHPUnit is.
It is a lightweight PHP test framework
why to use PHPUnit.
1. Facebook is using
2. You can manipulate the test script by command
3. Can test performance
4. Can test code coverage
5. Can automatically update the parameter data of test cases
6. Various formats of the log
6. The most important thing is that the function is so dazzling, it is very simple to use
installation of PHPUnit
Pear channel-discover pear.phpunit.de
pear Install Phpunit/phpunit
Quick Start
<?php
require_once ' phpunit/framework.php ';
Class Arraytest extends Phpunit_framework_testcase
{public
function Testnewarrayisempty ()
{
// Creates an array fixture.
$fixture = Array ();
The size of the assertion array fixture is 0.
$this->assertequals (0, sizeof ($fixture));
}
? >
1. Arraytest for Test class
2. Arraytest inherited from Phpunit_framework_testcase
3. Test method Testnewarrayisempty (), the test method must be public permission, usually beginning with test, or you can optionally give it a comment @test to indicate that the function is a test function
/**
* @test
/Public Function testnewarrayisempty ()
{
$fixture = array ();
$this->assertequals (0, sizeof ($fixture));
command line start test
PHPUnit the test file name, where you want to test the arraytest.php file
PHPUnit arraytest
phpunit 3.2.10 by Sebastian Bergmann ...
time:0 seconds
OK (2 tests)
Command line arguments
PHPUnit--help phpunit 3.2.10 by Sebastian Bergmann.
usage:phpunit [Switches] unittest [unittest.php]--log-graphviz <file> log test execution in Graphviz markup.
--log-json <file> log test execution in JSON format.
--log-tap <file> log test execution in tap format to file.
--log-xml <file> log test execution in XML format to file.
--log-metrics <file> Write Metrics in XML format.
--LOG-PMD <file> Write violations in PMD XML format.
--coverage-html <dir> Generate code coverage in HTML format.
--coverage-xml <file> Write Code Coverage information in XML format.
--TEST-DB-DSN <dsn> DSN for the test database.
--test-db-log-rev <r> Revision information for database logging. --test-db-prefix ...
Prefix that should is stripped from filenames. --test-db-log-info ...
Additional information for database logging. --testdox-html <file> Write AGILe documentation in HTML format to file.
--testdox-text <file> Write Agile Documentation in the text format to file.
--filter <pattern> Filter which tests to run. --group ...
Only runs tests from the specified group (s). --exclude-group ...
Exclude tests from the specified group (s).
--loader <loader> Testsuiteloader implementation to use.
--repeat <times> runs the test (s) repeatedly.
--tap the test execution progress in tap format.
--testdox the test execution progress in testdox format.
--no-syntax-check Disable Syntax check of test source files.
--stop-on-failure stop execution upon the A/failure.
--verbose Output more verbose information.
--wait Waits for a keystroke after the each test.
--skeleton Generate skeleton UnitTest class for the unit in unit.php.
--help prints this usage information. --vErsion prints the version and exits.
--configuration <file> Read configuration from XML file. -D Key[=value] Sets a php.ini value.
Advanced Features
Are you tired of having to put a test in front of each test method name, or not to write multiple test cases because it's just a different parameter? My favorite advanced features, now grandly recommended to you, called Frame Builder
<?php
class Calculator
{public
function add ($a, $b)
{return
$a + $b;
}
? >
Command line start test case
PHPUnit--skeleton Calculator
phpunit 3.2.10 by Sebastian Bergmann.
Wrote test class skeleton for Calculator to calculatortest.php.
Simple. Simple, but it doesn't really make sense, because there's no test data, how to add data, oh oh oh, the play is coming
<?php
class Calculator
{
/**
* @assert (0, 0) = = 0
* @assert (0, 1) = = 1
* @assert (1, 0) = = 1
* @assert (1, 1) = = 2/Public
function Add ($a, $b)
{return
$a + $b;
}
}
?>
Each method in the original class is instrumented for @assert annotations. These are translated into Test code, like this
/**
* Generated from @assert (0, 0) = = 0.
*/
Public Function Testadd () {
$o = new Calculator;
$this->assertequals (0, $o->add (0, 0));
}
The following is the output of the test case class that runs the build.
PHPUnit calculatortest
phpunit 3.2.10 by Sebastian Bergmann.
....
time:0 seconds
OK (4 tests)