当其中一个测试失败时,您可以使用--stop-on-failure
标志来打破单元测试。
有没有办法快速告诉PHPUnit重新运行此失败的测试,而是手动提供完整路径?
答案 0 :(得分:9)
查看--filter
cli选项。您可以在organisation docs
和CLI Docs
中找到示例。
- 过滤器
仅运行名称与给定模式匹配的测试。模式可以是单个测试的名称,也可以是与多个测试名称匹配的正则表达式。
假设您的广告phpunit Tests/
和Tests/Stuff/ThatOneTestClassAgain::testThisWorks
失败:
您的选择是:
phpunit --filter ThatOneTestClassAgain
和
phpunit --filter testThisWorks
或大多数其他有意义的字符串
答案 1 :(得分:2)
我发现实现它的方式相当简单,但需要实现日志记录。您设置phpunit以登录到json文件。然后你将phpunit命令改为类似于:
cd /home/vagrant/tests && php -d auto_prepend_file=./tests-prepend.php /usr/local/bin/phpunit
这样做是在执行phpunit之前auto_prepend一个php文件。这样我们就可以捕获$ argsv并自动向phpunit提供所需的过滤命令。
<强>测试-prepend.php 强> (确保修改json日志的文件路径)
<?php
global $argv, $argc;
if(empty($argv) === false) {
// are we re-running?
$has_rerun = false;
foreach ($argv as $key => $value) {
if($value === '--rerun-failures') {
$has_rerun = true;
unset($argv[$key]);
break;
}
}
if($has_rerun === true) {
// validate the path exists and if so then capture the json data.
$path = realpath(dirname(__FILE__).'/../logs/report.json');
if(is_file($path) === true) {
// special consideration taken here as phpunit does not store the report as a json array.
$data = json_decode('['.str_replace('}{'.PHP_EOL, '},{'.PHP_EOL, file_get_contents($path).']'), true);
$failed = array();
// capture the failures as well as errors but taking care not to capture skipped tests.
foreach ($data as $event) {
if($event['event'] === 'test') {
if($event['status'] === 'fail') {
$failed[] = array($event['test'], 'failed');
}
elseif($event['status'] === 'error' && $event['trace'][0]['function'] !== 'markTestIncomplete') {
$failed[] = array($event['test'], 'error\'d');
}
}
}
if(empty($failed) === true) {
echo 'There are no failed tests to re-run.'.PHP_EOL.PHP_EOL;
exit;
}
else{
echo '--------------------------------------------------------------------'.PHP_EOL;
echo 'Re-running the following tests: '.PHP_EOL;
foreach ($failed as $key => $test_data) {
echo ' - '.$test_data[0].' ('.$test_data[1].')'.PHP_EOL;
// important to escapre the namespace backslashes.
$failed[$key] = addslashes($test_data[0]);
}
echo '--------------------------------------------------------------------'.PHP_EOL.PHP_EOL;
}
$argv[] = '--filter';
$argv[] = '/('.implode('|', $failed).')/';
// important to update the globals in every location.
$_SERVER['argv'] = $GLOBALS['_SERVER']['argv'] = $GLOBALS['argv'] = $argv = array_values($argv);
$_SERVER['argc'] = $GLOBALS['_SERVER']['argc'] = $GLOBALS['argc'] = $argc = count($argv);
}
else{
echo 'The last run report log at '.$path.' does not exist so it is not possible to re-run the failed tests. Please re-run the test suite without the --rerun-failures command.'.PHP_EOL.PHP_EOL;
exit;
}
}
}
答案 2 :(得分:0)
Since PhpUnit 7.3,您可以缓存测试结果,然后按缺陷排序测试。
在phpunit.xml中,启用cacheResults
:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit cacheResult="true"
...>
如果您不想编辑phpunit.xml,也可以使用--cache-results
标志运行测试。
缓存结果时,PhpUnit将在运行测试后创建一个.phpunit.result.cache
文件(请确保将此文件添加到您的(全局)gitignore文件中)。
您可以像这样运行测试以首先运行先前失败的测试:
phpunit --order-by=defects --stop-on-failure