webino/nette-tester

Nette Tester:享受PHP单元测试,带有代码覆盖率报告工具。🍏🍏🍎🍏

v2.3.0 2019-06-24 03:59 UTC

README

Downloads this Month Build Status Build Status Windows Latest Stable Version License

简介

Nette Tester是一个高效且有趣的单元测试框架。它由Nette 框架使用,并能测试任何PHP代码。

文档可在Nette Tester网站找到。阅读博客获取最新信息。

安装

推荐使用Composer安装Nette Tester

composer require nette/tester --dev

或者,您可以下载tester.phar文件。

Nette Tester需要PHP 5.3.0,并支持PHP 7.2。收集和处理代码覆盖率信息依赖于Xdebug或PHPDBG。

编写测试

假设我们要测试这个简单的类

class Greeting
{
	function say($name)
	{
		if (!$name) {
			throw new InvalidArgumentException('Invalid name.');
		}
		return "Hello $name";
	}
}

因此我们创建一个名为greeting.test.phpt的测试文件

require 'src/bootstrap.php';

use Tester\Assert;

$h = new Greeting;

// use an assertion function to test say()
Assert::same( 'Hello John', $h->say('John') );

这就完成了!

现在,我们可以使用命令行中的tester命令运行测试

> tester
 _____ ___  ___ _____ ___  ___
|_   _/ __)( __/_   _/ __)| _ )
  |_| \___ /___) |_| \___ |_|_\  v1.7.2

PHP 5.3.16 | "php-cgi" -n | 8 threads
.
OK (1 tests, 0 skipped, 0.0 seconds)

Nette Tester对于成功的测试打印点,对于失败的测试打印F,对于跳过的测试打印S。

断言

此表显示了所有断言(类Assert表示Tester\Assert

  • Assert::same($expected, $actual) - 如果$expected和$actual不相同,则报告错误。
  • Assert::notSame($expected, $actual) - 如果$expected和$actual相同,则报告错误。
  • Assert::equal($expected, $actual) - 与same()类似,但忽略了对象的身份和数组键的顺序。
  • Assert::notEqual($expected, $actual) - 与notSame()类似,但忽略了对象和数组的顺序。
  • Assert::contains($needle, array $haystack) - 如果$needle不是$haystack的元素,则报告错误。
  • Assert::contains($needle, string $haystack) - 如果$needle不是$haystack的子字符串,则报告错误。
  • Assert::notContains($needle, array $haystack) - 如果$needle是$haystack的元素,则报告错误。
  • Assert::notContains($needle, string $haystack) - 如果$needle是$haystack的子字符串,则报告错误。
  • Assert::true($value) - 如果$value不是TRUE,则报告错误。
  • Assert::false($value) - 如果$value不是FALSE,则报告错误。
  • Assert::truthy($value) - 如果$value不是truthy,则报告错误。
  • Assert::falsey($value) - 如果$value不是falsey,则报告错误。
  • Assert::null($value) - 如果$value不是NULL,则报告错误。
  • Assert::nan($value) - 如果$value不是NAN,则报告错误。
  • Assert::type($type, $value) - 如果变量$value不是PHP或类类型$type,则报告错误。
  • Assert::exception($closure, $class, $message = NULL, $code = NULL) - 检查函数是否抛出异常。
  • Assert::error($closure, $level, $message = NULL) - 检查函数$closure是否抛出PHP警告/注意/错误。
  • Assert::noError($closure) - 检查函数$closure是否未抛出PHP警告/注意/错误或异常。
  • Assert::match($pattern, $value) - 使用正则表达式或掩码比较结果。
  • Assert::matchFile($file, $value) - 使用正则表达式或掩码在文件中排序比较结果。
  • Assert::count($count, $value) - 如果$value中的项数不是$count,则报告错误。

测试异常

Assert::exception(function() {
	$h = new Greeting;
	$h->say(NULL);
}, 'InvalidArgumentException', 'Invalid name.');

测试PHP错误、警告或通知

Assert::error(function() {
	$h = new Greeting;
	echo $h->abc;
}, E_NOTICE, 'Undefined property: Greeting::$abc');

技巧和功能

手动运行单元测试很麻烦,所以让Nette Tester监视你的代码文件夹,并在代码更改时自动重新运行测试。

tester -w /my/source/codes

并行运行测试速度非常快,Nette Tester默认使用8个线程。如果你想按顺序运行测试,请使用

tester -j 1

如何找到尚未测试的代码?使用代码覆盖率分析。此功能要求你在php.ini中安装Xdebug。这将生成一个漂亮的HTML报告,保存在coverage.html中。

tester . -c php.ini --coverage coverage.html --coverage-src /my/source/codes

我们可以使用Composer的自动加载器加载Nette Tester。在这种情况下,设置Nette Tester环境非常重要。

require 'vendor/autoload.php';

Tester\Environment::setup();

我们还可以测试HTML页面。让模板引擎生成HTML代码或将其下载到$html变量中。我们将检查页面是否包含用户名和密码表单字段。语法与CSS选择器相同。

$dom = Tester\DomQuery::fromHtml($html);

Assert::true( $dom->has('input[name="username"]') );
Assert::true( $dom->has('input[name="password"]') );

更多灵感,请查看Nette Tester如何测试自己

运行测试

可以通过tester命令(或php tester.php)调用命令行测试运行器。查看命令行选项。

> tester

Usage:
    tester.php [options] [<test file> | <directory>]...

Options:
    -p <path>                    Specify PHP executable to run (default: php-cgi).
    -c <path>                    Look for php.ini file (or look in directory) <path>.
    -l | --log <path>            Write log to file <path>.
    -d <key=value>...            Define INI entry 'key' with value 'val'.
    -s                           Show information about skipped tests.
    --stop-on-fail               Stop execution upon the first failure.
    -j <num>                     Run <num> jobs in parallel (default: 8).
    -o <console|tap|junit|none>  Specify output format.
    -w | --watch <path>          Watch directory.
    -i | --info                  Show tests environment info and exit.
    --setup <path>               Script for runner setup.
    --colors [1|0]               Enable or disable colors.
    --coverage <path>            Generate code coverage report to file.
    --coverage-src <path>        Path to source code.
    -h | --help                  This help.