如何从PHPUnit测试中导致的致命错误中获取测试文件名和行号?

时间:2018-02-19 05:16:36

标签: php unit-testing phpunit

我目前正在加入一个刚加入的项目的PHPUnit。运行PHP单元给了我以下结果:

PHPUnit 4.6.10 by Sebastian Bergmann and contributors.

Configuration read from /home/notalentgeek/notalentgeek/PHPProject/phpunit.xml

EEEEEEEEEEEEEEEEEEEEEEEEE..EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE   61 / 2244 (  2%)
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE  122 / 2244 (  5%)
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE...  183 / 2244 (  8%)
...........EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE  244 / 2244 ( 10%)
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE  305 / 2244 ( 13%)
EEEEEEEEEEEEEEEEEEEEEEEEE..EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE  366 / 2244 ( 16%)
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE  427 / 2244 ( 19%)
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE  488 / 2244 ( 21%)
EEEEEEEEEEE.EEEEEEEEEEEEEEEEEEEEEEEEE.EEEEEEEEEEEEEEEEEEEEEEE  549 / 2244 ( 24%)
EEE.EEEEEEEEEEEEEEEEEEEEEEEEEE.EEEEEEEEEEEEE.EEEEEEEEEEEEEEEE  610 / 2244 ( 27%)
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE  671 / 2244 ( 29%)
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE  732 / 2244 ( 32%)
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE....  793 / 2244 ( 35%)
..EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE............EEEEEEEEEEE  854 / 2244 ( 38%)
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE  915 / 2244 ( 40%)
EEEEEEEEEEEEE................................................  976 / 2244 ( 43%)
............E................................................ 1037 / 2244 ( 46%)
........................................................EEEEE 1098 / 2244 ( 48%)
EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE................. 1159 / 2244 ( 51%)
...........................................................E. 1220 / 2244 ( 54%)
E............................................................ 1281 / 2244 ( 57%)
..........................EE...............E................. 1342 / 2244 ( 59%)
............................................................. 1403 / 2244 ( 62%)
............................................................. 1464 / 2244 ( 65%)
............................................................. 1525 / 2244 ( 67%)
..................................PHP Fatal error:  Uncaught Error: Call to undefined function mcrypt_module_open() in MyOutdatedPHPFile.php:73
Stack trace:
#0 NotUnitTest.php(272): MyTool::Something('Hello World');
#1 NotUnitTest.php(40): cafe->makeMeATea('chinese tea')
#2 NotUnitTest.php(99): Something->hello('world')
#3 NotUnitTest.php(168): Other->test('3')
#4 NotUnitTest.php(628): MyClass::something()
#5 NotUnitTest.php(633): MyClass in NotUnitTest.php on line 73

存在致命错误,但事实是它没有显示它出现在哪个测试文件中。我现在想要的是排除导致致命错误的测试。因此,我想找到哪个测试文件和哪个行导致错误。我怎么能实现这样的目标呢?

1 个答案:

答案 0 :(得分:0)

首先,您应更新PHPUnit。目前版本为6.5。

堆栈跟踪

您的堆栈跟踪看起来很奇怪。通常,它包含完整路径,例如(我降级PHPUnit以获得可比较的结果):

PHPUnit 4.6.10 by Sebastian Bergmann and contributors.

PHP Fatal error:  Uncaught Error: Call to undefined function doIt() in /home/path/to/project/tests/unit/bug.php:6
Stack trace:
#0 /home/path/to/project/tests/unit/FooTest.php(8): Foo::buggy()
#1 [internal function]: FooTest->testFoo()
#2 /home/path/to/project/vendor/phpunit/phpunit/src/Framework/TestCase.php(865): ReflectionMethod->invokeArgs(Object(FooTest), Array)
#3 /home/path/to/project/vendor/phpunit/phpunit/src/Framework/TestCase.php(743): PHPUnit_Framework_TestCase->runTest()
#4 /home/path/to/project/vendor/phpunit/phpunit/src/Framework/TestResult.php(609): PHPUnit_Framework_TestCase->runBare()
#5 /home/path/to/project/vendor/phpunit/phpunit/src/Framework/TestCase.php(699): PHPUnit_Framework_TestResult->run(Object(FooTest))
#6 /home/path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php(722): PHPUnit_Framework_TestCase->run(Object(PHPUnit_Framework_TestResult))
#7 /home/path/to/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php(7 in /home/path/to/project/tests/unit/bug.php on line 6

在这里,您可以清楚地看到错误发生在bug.php的第6行,该FooTest.php来自Foo::buggy()中第8行,并且调用了NotUnitTest.php

在此之后,您正在寻找的电话位于第272行的MyTool::Something('Hello World'),并在其中调用MyOutdatedPHPFile.php

使用IDE的强大功能

如果您无法解决堆栈跟踪中的奇怪输出(缺少的路径),您仍然可以使用IDE查找相关的测试文件。

->methodName(中找到包含第73行的方法。然后让IDE搜索该方法的用法。如果您的IDE不支持使用率搜索,请更改IDE或使用users的文本搜索。在任何情况下,您都会获得一个包含相关方法调用的文件列表 - 其中一个是您正在寻找的文件。