【问题标题】:Is there a way to detect if a PHPUnit test is running in background?有没有办法检测 PHPUnit 测试是否在后台运行?
【发布时间】:2018-08-18 00:47:32
【问题描述】:

PHPUnit 有two special annotations 表示测试应该在后台进程中运行:一个类注释@runTestsInSeparateProcesses 影响当前类中的所有测试,一个测试注释@runInSeparateProcess 影响当前测试。

这在某些情况下很有用,当被测类需要打印一些输出时,它不会与 PHPUnit 自己的输出混淆。

所以,这是我的问题:TestCase 类是否知道这一事实?它知道什么时候在后台运行吗?如果是这样,有没有方法可以检测到它?

我正在寻找类似$this->isRunningOnBackground() 的东西。

我在文档中没有找到任何关于此的内容。

【问题讨论】:

    标签: php unit-testing phpunit tdd


    【解决方案1】:

    浏览 PHPUnit 的源代码,我在 TestCase 类 $runTestInSeparateProcess 中发现了一个布尔保护属性​​,当注释存在时它设置为 true。还有一个$runClassInSeparateProcess,但它被设置为私有,所以你不会在子类上下文中访问它。

    我们可以通过在 setUp() 或 tearDown() 中检查 if ($this->runTestInSeparateProcess) {...} 来检测测试是否在后台运行。

    【讨论】:

      猜你喜欢
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 2012-09-18
      相关资源
      最近更新 更多