phpunit 单元测试

简介:

1、ubuntu12.04安装

1
2
3
wget https: //phar .phpunit.de /phpunit .phar
chmod  +x phpunit.phar
mv  phpunit.phar  /usr/local/bin/phpunit

2、测试案例phpunit1.php(测试的依赖关系)

展示如何用@depends标注来表达测试方法之间的依赖关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
class  StackTest  extends  PHPUnit_Framework_TestCase
{
public  function  testEmpty()
{
$stack  array ();
$this ->assertEmpty( $stack );
 
return  $stack ;
}
 
/**
* @depends testEmpty
*/
public  function  testPush( array  $stack )
{
array_push ( $stack 'foo' );
$this ->assertEquals( 'foo' $stack [ count ( $stack )-1]);
$this ->assertNotEmpty( $stack );
 
return  $stack ;
}
 
/**
* @depends testPush
*/
public  function  testPop( array  $stack )
{
$this ->assertEquals( 'foo' array_pop ( $stack ));
$this ->assertEmpty( $stack );
}
}

3、测试结果

1
2
3
4
5
phpunit phpunit1.php
PHPUnit 3.7.27 by Sebastian Bergmann.
...
Time: 4 ms, Memory: 4.25Mb
OK (3 tests, 5 assertions)

4、为了快速定位缺陷,我们希望把注意力集中于相关的失败测试上。这就是为什么当某个测试所依赖的测试失败时,PHPUnit会跳过这个测试。通过利用测试之间的依赖关系,缺陷定位得到了改进。

如下案例phpunit2.php:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
class  DependencyFailureTest  extends  PHPUnit_Framework_TestCase
{
public  function  testOne()
{
$this ->assertTrue(FALSE);
}
 
/**
* @depends testOne
*/
public  function  testTwo()
{
}
}
?>

5、执行结果

1
2
3
4
5
6
7
8
9
10
phpunit phpunit2.php
PHPUnit 3.7.27 by Sebastian Bergmann.
FS
Time: 2 ms, Memory: 4.00Mb
There was 1 failure:
1) DependencyFailureTest::testOne
Failed asserting that  false  is  true .
/home/wwwroot/local .guazi.com /webroot/phpunit2 .php:6
FAILURES!
Tests: 1, Assertions: 1, Failures: 1, Skipped: 1.

6、测试可以使用多于一个@depends标注。PHPUnit不会更改测试的运行顺序,因此你需要自行保证某个测试所依赖的所有测试均出现于这个测试之前。

拥有多个@depends标注的测试,其第一个参数是第一个生产者提供的基境,第二个参数是第二个生产者提供的基境,以此类推

案例phpunit3.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
class  MultipleDependenciesTest  extends  PHPUnit_Framework_TestCase
{
public  function  testProducerFirst()
{
$this ->assertTrue(true);
return  'first' ;
}
 
public  function  testProducerSecond()
{
$this ->assertTrue(true);
return  'second' ;
}
 
/**
* @depends testProducerFirst
* @depends testProducerSecond
*/
public  function  testConsumer()
{
$this ->assertEquals(
array ( 'first' 'second' ),
func_get_args()
);
}
}
?>

7、执行结果

1
2
3
4
5
phpunit phpunit3.php
PHPUnit 3.7.27 by Sebastian Bergmann.
...
Time: 4 ms, Memory: 4.25Mb
OK (3 tests, 3 assertions)






















本文转自shayang8851CTO博客,原文链接:http://blog.51cto.com/janephp/1298842 ,如需转载请自行联系原作者
相关文章
|
测试技术 PHP 数据库
ThinkPHP6.0使用PHPUnit进行单元测试
ThinkPHP6.0使用PHPUnit进行单元测试
358 0
ThinkPHP6.0使用PHPUnit进行单元测试
|
9月前
|
IDE Java 测试技术
单元测试PHPUnit入门三板斧
什么,你是程序员?什么你从来没写过单元测试用例?嗯,不要惊讶,这在国内正常的啦。有的觉得写这个玩意太耗时间了,有的干脆就不知道单元测试用例是怎么玩的。说来惭愧,小马也是在CICD的“胁迫”下开始正式涉足。
73 0
单元测试PHPUnit入门三板斧
|
9月前
|
敏捷开发 IDE 测试技术
单元测试PHPUnit初体验之安装与示例
啥?你是程序员没写过单元测试?呃,好吧。小马这次也是需要支持CI/CD才刚开始强制自我实行。
94 0
单元测试PHPUnit初体验之安装与示例
|
11月前
|
测试技术 PHP
laravle phpunit单元测试登录自动获取token
laravle phpunit单元测试登录自动获取token
77 0
|
测试技术 PHP
PHP: PhpStorm单元测试报错:PHPUnit Cannot open file
PHP: PhpStorm单元测试报错:PHPUnit Cannot open file
72 0
|
测试技术 PHP
phpunit PHP单元测试的利器
PHPUnit是PHP的单元测试框架。单元测试在软件开发中越来越受到重视,测试先行编程、极限编程和测试驱动开发在实践中被广泛。利用单元测试,也可以实现契约式设计。
116 0
phpunit PHP单元测试的利器