php+class的集成,PHP 项目集成 PHPUnit

Composer 方式下载集成 PHPUnit

Composer 下载 PHPUnit1composer require --dev phpunit/phpunit ^8

如下图

php-integration-composer-phpunit-installation.png

下载完成后,输入以下命令1

2.vendorbinphpunit --version

PHPUnit 8.0.0 by Sebastian Bergmann and contributors.

php-integration-composer-phpunit-version.png

项目目录

php-integration-composer-phpunit-dirs.png

编辑 tests/UtilsTest.php 文件The tests for a class Class go into a class ClassTest.

对一个类的测试代码编写在类 ClassTest 代码块中

ClassTest inherits (most of the time) from PHPUnitFrameworkTestCase.

ClassTest 大多数情况下继承 PHPUnitFrameworkTestCase

The tests are public methods that are named test.

测试方法都是公有方法,以 test 开头

Alternatively, you can use the @test annotation in a method’s docblock to mark it as a test method. 同样的,你也可以在方法的文档注释块使用 @test 标记,使该方法成为一个测试方法

Inside the test methods, assertion methods such as assertSame() (see Assertions) are used to assert that an actual value matches an expected value.

在测试方法中,类似于 assertSame() 之类的断言方法用于断言真实值和所希望得到的值是否一致1

2

3

4

5

6

7

8

9

10

11

12

13

14

15require_once __DIR__ .'/../vendor/autoload.php';

use FrameworkTestCase;

use A2htrayDateFNSUtils;

class UtilsTest extends TestCase{

public function testAddDays(){

$dateTime = new DateTime('2019-04-02');

$actualDateTime = Utils::addDays($dateTime, 8);

$this->assertSame('2019-04-10', $actualDateTime->format('Y-m-d'));

return $actualDateTime;

}

}

使用 phpunit 命令验证1

2

3

4

5

6

7

8.vendorbinphpunit testsUtilsTest.php

PHPUnit 8.0.0 by Sebastian Bergmann and contributors.

. 1 / 1 (100%)

Time: 42 ms, Memory: 4.00 MB

OK (1 test, 1 assertion)

使用 @depends 注释进行管道式输入输出

如将 testAddDays 方法的返回值作为别一个测试方法的输入

在 UtilsTest 类中加入如下内容1

2

3

4

5

6

7

8* @depends testAddDays

*/

public function testAddHours(DateTime $dateTime){

// 加上 8 个小时

$actualDateTime = Utils::addHours($dateTime, 8);

$this->assertSame('2019-04-10 08', $actualDateTime->format('Y-m-d H'));

}

两次运行 phpunit 命令

php-integration-composer-phpunit-run-again.png

Composer 集成 PHPUnit

Scripts

A script, in Composer’s terms, can either be a PHP callback (defined as a static method) or any command-line executable command. Composer 中术语 脚本,可以是 PHP 代码的一个回调(定义为静态方法)或者其他任何命令行可执行程序

编辑 composer.json

在根下,加入以下内容1

2

3"scripts": {

"phpunit": ".\vendor\bin\phpunit tests\UtilsTest.php"

}

使用 Composer run-script 选项执行1composer run-script phpunit

php-integration-composer-phpunit-run-script.png

代码覆盖率

PHPUnit’s code coverage functionality provides an insight into what parts of the production code are executed when the tests are run. It makes use of the php-code-coverage component, which in turn leverages the code coverage functionality provided by the Xdebug extension for PHP. PHPUnit 代码覆盖率功能提供一个新的视角,用于检查当运行测试用例时有多少的代码量是被执行了。该功能使用到 php-code-coverage 组件,而该组件则利用了由 Xdebug 提供的功能。

Xdebug 的安装:

在 Windows 下,找到 CLI PHP 的 php.ini 文件,加入以下内容(具体的目录、文件名须与本机对应)1

2

3

4

5

6

7

8

9[xdebug]

zend_extension ="c:/wamp/bin/php/php7.2.4/zend_ext/php_xdebug-2.6.0-7.2-vc15.dll"

xdebug.remote_enable = off

xdebug.profiler_enable = off

xdebug.profiler_enable_trigger = Off

xdebug.profiler_output_name = cachegrind.out.%t.%p

xdebug.profiler_output_dir ="c:/wamp/tmp"

xdebug.show_local_vars=0

运行以下命令验证:1

2

3

4

5php -v

PHP 7.2.4 (cli) (built: Mar 28 2018 04:46:46) ( ZTS MSVC15 (Visual C++ 2017) x86 )

Copyright (c) 1997-2018 The PHP Group

Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

with Xdebug v2.6.0, Copyright (c) 2002-2018, by Derick Rethans

生成 phpunit.xml 配置文件1

2

3

4

5

6

7

8

9

10.vendorbinphpunit --generate-configuration

PHPUnit 8.0.0 by Sebastian Bergmann and contributors.

Generating phpunit.xml in D:workspacea2htraycp-date-fns

Bootstrap script (relative to path shown above; default: vendor/autoload.php):

Tests directory (relative to path shown above; default: tests):

Source directory (relative to path shown above; default: src):

Generated phpunit.xml in D:workspacea2htraycp-date-fns

新建构建目录 build,用于保存覆盖率报告1mkdir build

运行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.vendorbinphpunit testsUtilsTest.php --coverage-html build

PHPUnit 8.0.0 by Sebastian Bergmann and contributors.

Runtime: PHP 7.2.4 with Xdebug 2.6.0

Configuration: D:workspacea2htraycp-date-fnsphpunit.xml

RS 2 / 2 (100%)

Time: 138 ms, Memory: 6.00 MB

There was 1 risky test:

1) UtilsTest::testAddDays

This test does not have a @covers annotation but is expected to have one

--

There was 1 skipped test:

1) UtilsTest::testAddHours

This test depends on "UtilsTest::testAddDays" to pass.

OK, but incomplete, skipped, or risky tests!

Tests: 2, Assertions: 1, Skipped: 1, Risky: 1.

Generating code coverage report in HTML format ... done

build 目录结构1

2

3

4

5

6

7

8

9

10tree ./build

Folder PATH listing for volume w

Volume serial number is D604-FCB0

D:WORKSPACEA2HTRAYCP-DATE-FNSBUILD

├───Abs

├───Lib

│ └───Formatter

├───_css

├───_icons

└───_js

查看效果

php-integration-composer-phpunit-code-coverage.png

php-integration-composer-phpunit-code-coverage-dashboard.png

小结Composer 安装 PHPUnit 8

编写测试用例、运行测试用例

Composer scripts 字段集成 PHPUnit 8 测试命令

Xdebug 的安装

生成 phpunit.xml 配置文件

生成覆盖率报告

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值