简体   繁体   English

当成组运行时,PHPunit测试失败

[英]PHPunit tests fail when running as a group

When running a full test suite in PHPUnit that is defined in my phpunit.xml all of my unit tests run and pass. 当在我的phpunit.xml中定义的PHPUnit中运行完整的测试套件时,我所有的单元测试都将运行并通过。

If I then run a particular group of tests I get a Fatal error as classes I am trying to mock can't be found. 如果然后运行特定的一组测试,则会出现致命错误,因为找不到我要模拟的类。

I have a Bootstrap.php file that set's up an autoloader and from what I can see the Bootstrap is used in both cases. 我有一个Bootstrap.php文件,该文件设置了一个自动加载器,从我可以看到,在两种情况下都使用Bootstrap。

Anyone experienced this before, or have any suggestions? 任何人以前都经历过,或者有什么建议吗?

I have had that error happen on occasion. 我有时会发生该错误。 Without know more about your autoloader or your setup, you can fix the problem with your mocks by using disableAutoload on the classes. 在不了解有关自动加载器或设置的更多信息的情况下,可以通过在类上使用disableAutoload来解决disableAutoload问题。

Using the MockBuilder interface: 使用MockBuilder界面:

$mock = $this->getMockBuilder('SomeClass')->disableAutoload()->getMock();

Or 要么

$mock = $this->getMock('SomeClass', array(), null, null, true, true, true)
                                                disables Autoload ----^

http://phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubs.examples.StubTest.php http://phpunit.de/manual/current/en/test-doubles.html#test-doubles.stubs.examples.StubTest.php

  • By default, all methods of the given class are replaced with a test double that just returns NULL unless a return value is configured using will($this->returnValue()), for instance. 默认情况下,给定类的所有方法都将替换为只返回NULL的测试双精度,除非例如使用will($ this-> returnValue())配置了返回值。

  • When the second (optional) parameter is provided, only the methods whose names are in the array are replaced with a configurable test double. 当提供第二个(可选)参数时,只有名称在数组中的方法才被可配置的测试双精度代替。 The behavior of the other methods is not changed. 其他方法的行为不会改变。 Providing NULL as the parameter means that no methods will be replaced. 提供NULL作为参数意味着不会替换任何方法。

  • The third (optional) parameter may hold a parameter array that is passed to the original class' constructor (which is not replaced with a dummy implementation by default). 第三个(可选)参数可以包含一个传递给原始类的构造函数的参数数组(默认情况下,该数组不替换为虚拟实现)。

  • The fourth (optional) parameter can be used to specify a class name for the generated test double class. 第四个(可选)参数可用于为生成的测试double类指定类名。

  • The fifth (optional) parameter can be used to disable the call to the original class' constructor. 第五个(可选)参数可用于禁用对原始类的构造函数的调用。

  • The sixth (optional) parameter can be used to disable the call to the original class' clone constructor. 第六个(可选)参数可用于禁用对原始类的克隆构造函数的调用。

  • The seventh (optional) parameter can be used to disable __autoload() during the generation of the test double class. 第七个(可选)参数可用于在生成测试double类的过程中禁用__autoload()。

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

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