场景

在编写PHPUnit单元测试代码时,其实很多都是对各个类的各个外部调用的函数进行测试验证,检测代码覆盖率,验证预期效果。为避免增加开发量,可以使用PHPUnit提供的phpunit-skelgen来生成测试骨架。只是一开始我不知道有这个脚本,就自己写了一个,大大地提高了开发效率,也不用为另外投入时间去编写测试代码而烦心。并且发现自定义的脚本比phpunit-skelgen更具人性化。所以在这里分享一下。

一个待测试的示例类

假如我们现在有一个简单的业务类,实现了加运算,为了验证其功能,下面将会就两种生成测试代码的方式进行说明。

1

2

3

4

5

6

7

8

9<?php

class Demo

{

public function inc($left,$right)

{

return $left +$right;

}

}

用phpunit-skelgen生成测试骨架

在安装了phpunit-skelgen后,可以使用以下命令来生成测试骨架。

1phpunit-skelgen --test -- Demo ./Demo.php

生成后,使用:

1vim ./DemoTest.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

29

30

31

32

33

34

35

36

37

38

39

40<?php

/**

* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-06-30 at 15:53:01.

*/

class DemoTestextends PHPUnit_Framework_TestCase

{

/**

* @var Demo

*/

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 Demo;

}

/**

* Tears down the fixture, for example, closes a network connection.

* This method is called after a test is executed.

*/

protected function tearDown()

{

}

/**

* @covers Demo::inc

* @todo   Implement testInc().

*/

public function testInc()

{

// Remove the following lines when you implement this test.

$this->markTestIncomplete(

'This test has not been implemented yet.'

);

}

}

试运行测试一下:

1

2

3

4[test ~/tests]$phpunit ./DemoTest.php

PHPUnit 3.7.29 by Sebastian Bergmann.

PHP Fatal error:  Class'Demo' not foundin ~/tests/DemoTest.php on line 18

可以看到没有将需要的测试类包括进来。当然还有其他一些需要手工改动的地方。

自定义的测试代码生成脚本

现在改用自定义的脚本 来生成,虽然也有需要手工改动的地方,但已经尽量将需要改动的代码最小化,让测试人员(很可能是开发人员自己)更关注业务的测试。

先看一下Usage.

1

2[test ~/tests]$php ./build_phpunit_test_tpl.php

Usage: php ./build_phpunit_test_tpl.php   [bootstrap] [author = dogstar]

然后可以使用:

1php ./build_phpunit_test_tpl.php ./Demo.php Demo

来预览看一下将要生成的测试代码,如果没有问题可以使用:

1php ./build_phpunit_test_tpl.php ./Demo.php Demo > ./Demo_Test.php

将生成的测试代码保存起来。注意:这里使用的是“_Test.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

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54<?php

/**

* PhpUnderControl_AppAds_Test

*

* 针对 ./Demo.php Demo 类的PHPUnit单元测试

*

* @author: dogstar 20140630

*/

if (!class_exists('Demo')) {

require dirname(__FILE__) .'/' .'./Demo.php';

}

class PhpUnderControl_Demo_Testextends PHPUnit_Framework_TestCase

{

public $demo;

protected function setUp()

{

parent::setUp();

$this->demo =new Demo();

}

protected function tearDown()

{

}

/**

* @group returnFormat

*/

public function testIncReturnFormat()

{

$left ='';

$right ='';

$rs =$this->demo->inc($left,$right);

}

/**

* @depends testIncReturnFormat

* @group businessData

*/

public function testIncBusinessData()

{

$left ='';

$right ='';

$rs =$this->demo->inc($left,$right);

}

}

随后,试运行一下:

1

2

3

4

5

6

7

8[test ~/tests]$phpunit ./Demo_Test.php

PHPUnit 3.7.29 by Sebastian Bergmann.

..

Time: 1 ms, Memory: 2.50Mb

OK (2 tests, 0 assertions)

测试通过了!!!

起码,我觉得生成的代码在大多数默认情况下是正常通过的话,可以给开发人员带上心理上的喜悦,从而很容易接受并乐意去进行下一步的测试用例完善。

现在,开发人员只须稍微改动测试代码就可以实现对业务的验证。如下示例:

1

2

3

4

5

6

7

8

9public function testIncBusinessData()

{

$left ='1';

$right ='8';

$rs =$this->demo->inc($left,$right);

$this->assertEquals(9,$rs);

}

然后再运行,依然通过。

脚本源代码

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

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132<?php

/**

* 单元测试骨架代码自动生成脚本

* 主要是针对当前项目系列生成相应的单元测试代码,提高开发效率

*

* 用法:

* Usage: php ./build_phpunit_test_tpl.php   [bootstrap] [author = dogstar]

*

* 1、针对全部public的函数进行单元测试

* 2、各个函数对应返回格式测试与业务数据测试

* 3、源文件加载(在没有自动加载的情况下)

*

* 备注:另可使用phpunit-skelgen进行骨架代码生成

*

* @author: dogstar 20140630

* @version: 2.0.0

*/

if ($argc

die("Usage: php $argv[0]   [bootstrap] [author = dogstar]\n");

}

$filePath =$argv[1];

$className =$argv[2];

$bootstrap = isset($argv[3]) ?$argv[3] : null;

$author = isset($argv[4]) ?$argv[4] :'dogstar';

if (!empty($bootstrap)) {

require $bootstrap;

}

require $filePath;

if (!class_exists($className)) {

die("Error: cannot find class($className). \n");

}

$reflector =new ReflectionClass($className);

$methods =$reflector->getMethods(ReflectionMethod::IS_PUBLIC);

date_default_timezone_set('Asia/Shanghai');

$objName = lcfirst(str_replace('_','',$className));

$code = "<?php

/**

* PhpUnderControl_AppAds_Test

*

* 针对 $filePath $className 类的PHPUnit单元测试

*

* @author: $author " . date('Ymd') . "

*/

";

if (file_exists(dirname(__FILE__) .'/test_env.php')) {

$code .= "require_once dirname(__FILE__) .'/test_env.php';

";

}

$code .= "

if (!class_exists('$className')) {

require dirname(__FILE__) .'/' .'$filePath';

}

class PhpUnderControl_" . str_replace('_', '', $className) . "_Testextends PHPUnit_Framework_TestCase

{

public \$$objName;

protected function setUp()

{

parent::setUp();

\$this->$objName =new $className();

}

protected function tearDown()

{

}

";

foreach ($methods as $method) {

if($method->class !=$className)continue;

$fun =$method->name;

$Fun = ucfirst($fun);

if (strlen($Fun) > 2 &&substr($Fun, 0, 2) =='__')continue;

$rMethod =new ReflectionMethod($className,$method->name);

$params =$rMethod->getParameters();

$isStatic =$rMethod->isStatic();

$isConstructor =$rMethod->isConstructor();

if($isConstructor)continue;

$initParamStr ='';

$callParamStr ='';

foreach ($params as $param) {

$initParamStr .= "

\$" . $param->name . " ='';";

$callParamStr .='$' .$param->name .', ';

}

$callParamStr =empty($callParamStr) ?$callParamStr :substr($callParamStr, 0, -2);

$code .= "

/**

* @group returnFormat

*/

public function test$Fun" . "ReturnFormat()

{" . (empty($initParamStr) ? '' : "$initParamStr\n") . '

' . ($isStatic "\$rs = $className::$fun($callParamStr);":"\$rs = \$this->$objName->$fun($callParamStr);") . "

}

";

$code .= "

/**

* @depends test$Fun" . "ReturnFormat

* @group businessData

*/

public function test$Fun" . "BusinessData()

{" . (empty($initParamStr) ? '' : "$initParamStr\n") . '

' . ($isStatic "\$rs = $className::$fun($callParamStr);":"\$rs = \$this->$objName->$fun($callParamStr);") . "

}

";

}

$code .= "

}";

echo $code;

echo "\n";

php自动生成phpunit,[PHPUnit]自动生成PHPUnit测试骨架脚本相关推荐

  1. [PHPUnit]自动生成PHPUnit测试骨架脚本-提供您的开发效率【2015升级版】

    2019独角兽企业重金招聘Python工程师标准>>> 场景 在编写PHPUnit单元测试代码时,其实很多都是对各个类的各个外部调用的函数进行测试验证,检测代码覆盖率,验证预期效果. ...

  2. linux安装phpunit,linux下安装phpunit

    如果要全局安装 PHAR: $ wget https://phar.phpunit.de/phpunit.phar $ chmod +x phpunit.phar $ sudo mv phpunit. ...

  3. 自动生成文章的html,文章自动更新工具|自动生成文件|自动伪原创|文章自动插入关键词工具...

    概念网络发布一款自动更新文章的工具, 该套工具可用概念的文章站程序, 概念的发布站程序, 概念的企业站程序, 只要在服务器一直开着这个工具, 工具会更具配置文件的配置, 每天定时的更新网站的文章, 文 ...

  4. 软件测试作业8:分析自动售货机软件例子生成的判定表图例

    作业8 1.分析 Chap.5 (Lec.19) 自动售货机软件例子生成的判定表图例的第6列和第23列,分别给出: (1).输入条件的自然语义陈述: (2).输出结果的自然语义陈述: (3).用命题逻 ...

  5. javamail发送html正文文件_Python实现-生成测试报告amp;自动邮件发送

    之前单独介绍了生成测试报告和自动发送邮件,那么现在把两者整合到一起:生成测试报告后然后自动发送邮件,这里只是简单的整合实现功能,其实还可以优化的,先用吧,后面再慢慢优化 先看下目录,其实目录还是一样, ...

  6. Art Blocks:生成艺术的自动售货机

    撰文:程天一 来源:海外独角兽 代码和软件已经占据了我们的生活,但是使用代码创作出的生成艺术在过去很多年中始终被低估.2021 年夏天开始的 NFT 和 Art Blocks 热潮改变了这一点. Ar ...

  7. 基于.Net Core Web MVC的图书查询系统——第四章,添加模型并使用EF Core生成基架自动生成控制器和视图

    基于.Net Core Web MVC的图书查询系统 第一章,.Net Core Web MVC配置身份验证和注册登录功能并修改默认页面 第二章,.Net Core Web MVC配置邮件发送服务 第 ...

  8. python怎么写excel数据透视自动报表_使用Python生成自动报表(E

    使用Python生成自动报表(Excel)以邮件发送 数据分析师肯定每天都被各种各样的数据数据报表搞得焦头烂额,老板的,运营的.产品的等等.而且大部分报表都是重复性的工作,这篇文章就是帮助大家如何用P ...

  9. 输入关键词自动生成文章-免费自动输入关键词自动生成文章器

    输入关键词自动生成文章,什么是输入关键词自动生成文章?例:你输入什么关键词 '装修'免费工具会自动生成一篇跟装修相关的文章,该免费工具还支持:自动关键词文章生成+文章自动采集+伪原创+自动发布+自动推 ...

最新文章

  1. python【蓝桥杯vip练习题库】BASIC-9特殊回文数
  2. 10.23cron10.24chkconfig工具10.25systemd管理服务10.26unit
  3. C语言进行离散傅里叶DFT变换~MATLAB验证
  4. 视频数据复用光端机故障排除方法
  5. 仅坚持了9天:京东今日宣布暂停火车票代购业务
  6. slf4j的简单用法以及与log4j的区别
  7. 一次非常有意思的SQL优化经历:从30248.271s到0.001s
  8. 小程序真机调试访问不了接口_24小时从0到1开发阴阳师小程序
  9. linux中的echo%3e文件,Linux学习笔记-shell脚本中${}的使用方法
  10. 东方通TongWeb部署应用中文件不下载而在页面打开
  11. 实现微信小程序开发的基本步骤
  12. 如何旋转PDF的页面方向?教你2种方法
  13. 【学习笔记】无限极分类学习
  14. 小程序获取上一个页面或者某个页面内的值
  15. DCB value for SVN 77 not found on dcb.dat
  16. Python 爬虫:把廖雪峰的教程转换成 PDF 电子书
  17. 拉格朗日插值法的Matlab实现
  18. nodejs简单学习
  19. IT知识图谱(只是从CSDN中把图片,一个个下载了)
  20. 小米官宣停服后,“米聊“再次上线

热门文章

  1. 任天堂红白机 ( NES ) 文档
  2. 以智慧城市为标杆打造立体防控,咫尺之间华丽转身
  3. 计算机视觉学习资料汇总
  4. Ubuntu Server 个人影音服务器,实现NAS、远程下载、私人云盘等
  5. AMD 双核驱动、补丁、优化工具下载地址和安装方法
  6. 总结大佬经验,如何学习STM32?(入门、进阶)
  7. MyEclipse10 + Axis2 开发webservice
  8. 光盘加密软件测试自学,SecurDisc光盘加密功能实战
  9. 如何处理linux恶意程序
  10. Python爬全国邮政编码的程序