PHPUnit Strict模式有什么作用? [英] What does PHPUnit Strict mode do?

查看:61
本文介绍了PHPUnit Strict模式有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道"PHPUnit中的严格模式"是什么?

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

例如:

phpunit --strict

或在phpunit.xml中

or in 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

推荐答案

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

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

详细答案:

这里是@Crozin的帮助下更新的一组信息.

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

在我的情况下,错误是测试花费的时间太长(> 1秒.)(Doctrine ORM模式删除+创建会减慢速度,

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.

通过阅读/逆向工程/usr/share/php/pear/share/pear/PHPUnit/Util/Test.php::getSize()(和同一个类上的getGroups())..我发现这里有我们可以使用3个未公开的注释:

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:

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

可以在类级别或方法级别上指定它们. PHPUnit github上的问题#490 提示了同时提供类级别和方法级别的问题所以YMMV如果您将它们混合在一起.正如克罗钦所说,分配的超时分别为10、5、1秒.

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.

另一种解决方案是增加(在运行缓慢的计算机上)允许调用的函数运行多长时间.

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
带有超时调用可调用对象的实用程序类.需要此软件包才能在严格模式下强制执行测试超时. [ PHPUnit安装说明]

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]

严格模式 没有断言任何内容的测试被标记为未完成 未完成(或跳过)的测试不会产生代码覆盖率塞巴斯蒂安·伯格曼(Sebastian Bergmann)的幻灯片共享(幻灯片10)

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)

注意
请注意,PHPUnit会吞下测试执行期间发出的所有输出.在严格模式下,发出输出的测试将失败. 测试以下内容的输出部分PHPUnit手册

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 Strict模式有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆