简体   繁体   English

测试是否通过取决于PHP单元调用的方法

[英]Tests pass or fail depending on method of PHP Unit invocation

I have a large number of tests in a PHP Unit test suite. 我在PHP单元测试套件中进行了大量测试。

All tests will pass when running the complete suite, however when run individually, certain tests may pass or fail depending on how I call PHP Unit: 运行完整套件时,所有测试都会通过,但是,单独运行时,某些测试可能会通过或失败,具体取决于我如何称呼PHP单元:

$ php phpunit --configuration phpunit.xml --filter FooIntegrationTest

PHPUnit 5.7.19 by Sebastian Bergmann and contributors.

................                                                  16 / 16 (100%)

Time: 3.97 seconds, Memory: 109.50MB

OK (16 tests, 36 assertions)

vs.

$ php phpunit --configuration phpunit.xml tests/Integration/FooIntegrationTest.php

PHPUnit 5.7.21 by Sebastian Bergmann and contributors.

..........F.F...                                                  16 / 16 (100%)

Time: 3.73 seconds, Memory: 111.75MB

FAILURES!
Tests: 16, Assertions: 36, Failures: 2.

The failures in question are unexpected results (ie the code runs just fine), no PHP errors or Exceptions. 有问题的失败是意外的结果(即代码运行正常),没有PHP错误或异常。

The majority of tests will pass both ways, and there doesn't seem to be anything special about those tests that produce the above results. 大多数测试都会通过两种方式,并且产生上述结果的那些测试似乎没有什么特别之处。

Solved.... namespace conflict from an unrelated test class 解决了...来自不相关测试类的名称空间冲突

phpunit ... FooIntegrationTest.php only opens & runs the single class file specfied phpunit ... FooIntegrationTest.php仅打开并运行指定的单个类文件

whereas phpunit ... --filter FooIntegrationTest will open every file in the suite to look for matching tests; phpunit ... --filter FooIntegrationTest将打开套件中的每个文件以查找匹配的测试; which lead to a globally namespaced function in another file causing false pass. 这会导致另一个文件中的全局命名空间函数导致错误通过。

Lesson learned: don't declare globally namespaced functions in unit test classes! 经验教训:不要在单元测试类中声明全局命名空间的函数!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM