PHPUnit严格模式做什么?

时间:2022-10-15 20:10:13

I am wondering about what "strict mode is in PHPUnit" ?

我想知道PHPUnit的“strict mode”是什么意思?

eg:

例如:

phpunit --strict

or in phpunit.xml

或者在phpunit.xml

<phpunit strict="true">

I turned it on just to try it and my tests started failing with

我打开它只是为了尝试它,我的测试开始失败

PHP_Invoker_TimeoutException: Execution aborted after 1 second

2 个解决方案

#1


32  

Please note that PHPUnit swallows all output that is emitted during the execution of a test. In strict mode, a test that emits output will fail.

请注意,PHPUnit接收在执行测试期间发出的所有输出。在严格模式下,释放输出的测试将失败。

That's all I could find in documentation, but I'd also checked the sources and I found that in strict mode:

这就是我能在文件中找到的,但我也检查了资料来源,我发现在严格的模式下:

  1. Tests with no assertions might be marked as incomplete/failures.
  2. 没有断言的测试可能被标记为不完整/失败。
  3. Each test might be run with execution time limit depending on it's size and the presence of the pcntl extension and PHP_Invoker library. There are three timeouts values:

    每个测试都可以根据它的大小和pcntl扩展名和PHP_Invoker库的存在来运行执行时间限制。有三个超时的值:

    • timeoutForSmallTests (default value: 1)
    • timeoutForSmallTests(默认值:1)
    • timeoutForMediumTests (default value: 10)
    • timeoutForMediumTests(默认值:10)
    • timeoutForLargeTests (default value: 60)
    • timeoutForLargeTests(默认值:60)

    The test size (small, medium or large) is determined by the PHPUnit_Util_Test::getSize() method:

    测试大小(小、中、大)由PHPUnit_Util_Test::getSize()方法确定:

    • Test cases descending from PHPUnit_Extensions_Database_TestCase or PHPUnit_Extensions_SeleniumTestCase are large.
    • 来自PHPUnit_Extensions_Database_TestCase或PHPUnit_Extensions_SeleniumTestCase的测试用例很大。
    • Test cases and individual tests can also be made large or medium by adding them to the 'large' or 'medum' groups, respectively.
    • 测试用例和单独的测试也可以通过将它们添加到“大”或“medum”组中来实现。
    • Otherwise, the test is small.
    • 否则,测试就很小。

It's seems that strict mode does only the above three changes, but I'm not absolutely sure about that. I have never studied PHPUnit's sources before, nor used strict mode.

严格模式似乎只做了以上三种改变,但我不是很确定。我以前从来没有研究过PHPUnit的来源,也没有使用过严格的模式。

#2


20  

Short answer: for long running tests use an annotation to increase the allowed run time:

简短的回答:对于长时间运行的测试,使用注释来增加允许的运行时间:

@large // 10 seconds
@medium // 5 seconds
@small // 1 second max <-- Default, so no point using

Long Answer:

长一点的回答:

Here is an updated set of info that was derived with the help of @Crozin.

这是在@Crozin的帮助下得到的一组更新的信息。

In my case the error was that a test was taking too long (>1 second.) (Doctrine ORM schema drop + create can slow things down, see this ZendCast for what I was doing). This was causing an issue (and some output) from PHP_Invoker. Strict mode doesnt allow any output.

在我的例子中,错误在于测试花费的时间太长(>1秒)(Doctrine ORM schema drop + create可以降低速度,请参见这个ZendCast了解我正在做什么)。这导致了PHP_Invoker的一个问题(和一些输出)。严格模式不允许输出。

By Reading / Reverse engineering /usr/share/php/pear/share/pear/PHPUnit/Util/Test.php::getSize() (and getGroups() on the same class) .. I figured out there are 3 undocumented annotations we can use:

php: getSize()(和getGroups() ()我发现我们可以使用3个无文档注释:

@large  // 10 seconds
@medium // 5 seconds
@small // 1 second max run time

They can be specified on the class level or on the method level. Issue #490 on the PHPUnit github hints at issues with supplying both class level and method level so YMMV if you mix them. As crozin said, the allotted time outs are 10,5,1 seconds respectively.

它们可以在类级别或方法级别指定。PHPUnit github上的第490期提示了提供类级和方法级的问题,如果您混合它们,那么YMMV。正如克罗津所说,规定的出局时间分别为10,5,1秒。

A alternate solution was to increase how long an invoked function is allowed to run (on my slow computer).

另一种解决方案是增加调用函数的运行时间(在我的慢速计算机上)。

sudo vi /usr/share/php/pear/share/pear/PHP/Invoker.php

Increase line 1 "declare(ticks = 1);" to
    "declare(ticks = 10);" // or any higher int that meets your needs 

Here is a bunch of information about strict mode that helped me find the solution:

以下是一些关于严格模式的信息,帮助我找到了解决方案:

PHP_Invoker
A utility class for invoking callables with a timeout. This package is required to enforce test timeouts in strict mode. [PHPUnit Install Instructions]

PHP_Invoker一个实用程序类,用于在超时时调用callables。这个包被要求严格执行测试超时。(PHPUnit)安装说明)

Strict Mode Tests that do not assert anything are marked as incomplete Test that are incomplete (or skipped) yield no code coverage Slideshare by Sebastian Bergmann (slide 10)

没有断言任何东西的严格模式测试被标记为不完整的(或跳过的)测试,不会产生塞巴斯蒂安·伯格曼的代码覆盖率幻灯片(幻灯片10)

Note
Please note that PHPUnit swallows all output that is emitted during the execution of a test. In strict mode, a test that emits output will fail. Testing output section of PHPUnit Manual

请注意,PHPUnit允许在执行测试期间发出的所有输出。在严格模式下,释放输出的测试将失败。PHPUnit手册的测试输出部分

#1


32  

Please note that PHPUnit swallows all output that is emitted during the execution of a test. In strict mode, a test that emits output will fail.

请注意,PHPUnit接收在执行测试期间发出的所有输出。在严格模式下,释放输出的测试将失败。

That's all I could find in documentation, but I'd also checked the sources and I found that in strict mode:

这就是我能在文件中找到的,但我也检查了资料来源,我发现在严格的模式下:

  1. Tests with no assertions might be marked as incomplete/failures.
  2. 没有断言的测试可能被标记为不完整/失败。
  3. Each test might be run with execution time limit depending on it's size and the presence of the pcntl extension and PHP_Invoker library. There are three timeouts values:

    每个测试都可以根据它的大小和pcntl扩展名和PHP_Invoker库的存在来运行执行时间限制。有三个超时的值:

    • timeoutForSmallTests (default value: 1)
    • timeoutForSmallTests(默认值:1)
    • timeoutForMediumTests (default value: 10)
    • timeoutForMediumTests(默认值:10)
    • timeoutForLargeTests (default value: 60)
    • timeoutForLargeTests(默认值:60)

    The test size (small, medium or large) is determined by the PHPUnit_Util_Test::getSize() method:

    测试大小(小、中、大)由PHPUnit_Util_Test::getSize()方法确定:

    • Test cases descending from PHPUnit_Extensions_Database_TestCase or PHPUnit_Extensions_SeleniumTestCase are large.
    • 来自PHPUnit_Extensions_Database_TestCase或PHPUnit_Extensions_SeleniumTestCase的测试用例很大。
    • Test cases and individual tests can also be made large or medium by adding them to the 'large' or 'medum' groups, respectively.
    • 测试用例和单独的测试也可以通过将它们添加到“大”或“medum”组中来实现。
    • Otherwise, the test is small.
    • 否则,测试就很小。

It's seems that strict mode does only the above three changes, but I'm not absolutely sure about that. I have never studied PHPUnit's sources before, nor used strict mode.

严格模式似乎只做了以上三种改变,但我不是很确定。我以前从来没有研究过PHPUnit的来源,也没有使用过严格的模式。

#2


20  

Short answer: for long running tests use an annotation to increase the allowed run time:

简短的回答:对于长时间运行的测试,使用注释来增加允许的运行时间:

@large // 10 seconds
@medium // 5 seconds
@small // 1 second max <-- Default, so no point using

Long Answer:

长一点的回答:

Here is an updated set of info that was derived with the help of @Crozin.

这是在@Crozin的帮助下得到的一组更新的信息。

In my case the error was that a test was taking too long (>1 second.) (Doctrine ORM schema drop + create can slow things down, see this ZendCast for what I was doing). This was causing an issue (and some output) from PHP_Invoker. Strict mode doesnt allow any output.

在我的例子中,错误在于测试花费的时间太长(>1秒)(Doctrine ORM schema drop + create可以降低速度,请参见这个ZendCast了解我正在做什么)。这导致了PHP_Invoker的一个问题(和一些输出)。严格模式不允许输出。

By Reading / Reverse engineering /usr/share/php/pear/share/pear/PHPUnit/Util/Test.php::getSize() (and getGroups() on the same class) .. I figured out there are 3 undocumented annotations we can use:

php: getSize()(和getGroups() ()我发现我们可以使用3个无文档注释:

@large  // 10 seconds
@medium // 5 seconds
@small // 1 second max run time

They can be specified on the class level or on the method level. Issue #490 on the PHPUnit github hints at issues with supplying both class level and method level so YMMV if you mix them. As crozin said, the allotted time outs are 10,5,1 seconds respectively.

它们可以在类级别或方法级别指定。PHPUnit github上的第490期提示了提供类级和方法级的问题,如果您混合它们,那么YMMV。正如克罗津所说,规定的出局时间分别为10,5,1秒。

A alternate solution was to increase how long an invoked function is allowed to run (on my slow computer).

另一种解决方案是增加调用函数的运行时间(在我的慢速计算机上)。

sudo vi /usr/share/php/pear/share/pear/PHP/Invoker.php

Increase line 1 "declare(ticks = 1);" to
    "declare(ticks = 10);" // or any higher int that meets your needs 

Here is a bunch of information about strict mode that helped me find the solution:

以下是一些关于严格模式的信息,帮助我找到了解决方案:

PHP_Invoker
A utility class for invoking callables with a timeout. This package is required to enforce test timeouts in strict mode. [PHPUnit Install Instructions]

PHP_Invoker一个实用程序类,用于在超时时调用callables。这个包被要求严格执行测试超时。(PHPUnit)安装说明)

Strict Mode Tests that do not assert anything are marked as incomplete Test that are incomplete (or skipped) yield no code coverage Slideshare by Sebastian Bergmann (slide 10)

没有断言任何东西的严格模式测试被标记为不完整的(或跳过的)测试,不会产生塞巴斯蒂安·伯格曼的代码覆盖率幻灯片(幻灯片10)

Note
Please note that PHPUnit swallows all output that is emitted during the execution of a test. In strict mode, a test that emits output will fail. Testing output section of PHPUnit Manual

请注意,PHPUnit允许在执行测试期间发出的所有输出。在严格模式下,释放输出的测试将失败。PHPUnit手册的测试输出部分