1.window Installation Pear Tutorial: http://jingyan.baidu.com/article/ca41422fd8cf3d1eae99ed3e.html
2. In the working directory, put two files:
1) calculator.php
1<?PHP2 3 classcalculator{4 5 public functionAdd$a,$b){6 return $a+$b;7 }8 9 public functionSub$a,$b){Ten return $a-$b; one } a}
View Code
2)calculatortest.php
1<?PHP2 3 //pear installed in the system4 require"phpunit/testcase.php";5 require"calculator.php";6 7 classCalculatortestextendsphpunit_framework_testcase{8 Private $calculator;9 Ten functionsetUp () one { aParent::setup ();//Todo:change the autogenerated stub - $this->calculator =NewCalculator (); - } the - functionTearDown () - { -Parent::teardown ();//Todo:change the autogenerated stub + unset($this-calculator); - } + a public functiontestaddbothpositive () { at $result=$this->calculator->add (3,4); - $this->assertequals (8,$result); - } - - public functiontestaddpositiveandnegative () { - $result=$this->calculator->add (3,-4); in $this->assertequals (-1,$result); - } to + public functiontestaddnegativeandpositive () { - $result=$this->calculator->add ( -4,3); the $this->assertequals (-1,$result); * } $ Panax Notoginseng public functionTestaddpositiveandzero () { - $result=$this->calculator->add (5,0); the $this->assertequals (5,$result); + } a the public functionTestaddnegativeandzero () { + $result=$this->calculator->add ( -5,0); - $this->assertequals (-5,$result); $ } $ - public functiontestaddnegativeandnegative () { - $result=$this->calculator->add ( -5,-5); the $this->assertequals (-10,$result); - }Wuyi}
View Code
Current working directory, running in console: phpunit calculatortest
Note: if the PHPUnit related header file is not found, it can be viewed with the results of the related file output Get_include_path (). In php.ini you can find the "include_path" keyword and locate the Reason.
3. Under the premise of installing xdebug, can run: phpunit--coverage-html "output_path" calculatortest, generate a report, HTML format, you can understand the coverage of the test Code.
Simple use of PHP phpunit