【问题标题】:Unittests running twice in parent class单元测试在父类中运行两次
【发布时间】:2014-09-03 20:44:38
【问题描述】:

出于许多很好的原因,我将一些测试放在父测试类中,并将一些测试放在继承类中。然后我得到了这个结构:

/bar/foo/foo.php
/bar.php

我用来启动 phpunit 的这个命令:phpunit --configuration unit.xml 我也试过这个,但结果相同:phpunit --configuration unit.xml --testsuite foo

所以现在我想在文件夹 foo 中运行所有测试。问题是,bar.php 中的测试将运行两次,我不知道为什么。有人对此有答案吗?它可能是phpunit的一个特性吗?我怎样才能告诉 phpunit 不要运行它们两次?

如果我运行 phpunit bar/foo/foo.php 一切正常,bar.php 中的测试功能只运行一次。

foo.php

require_once '/bar.php';

class foo_test extends bar_test {

    public function test_1_pass() {
        $this->assertEquals('a', 'a');
    }

}

bar.php

class bar_test extends PHPUnit_Framework_TestCase {

    public function test_2_fail() {
        $this->assertEquals('a', 'b');           
    }

}

单元测试响应

PHPUnit 3.7.29 by Sebastian Bergmann.

Configuration read from /unit.xml

F.F

Time: 104 ms, Memory: 3.25Mb

There were 2 failures:

1) bar_test::test_2_fail
Failed asserting that two strings are equal.

/bar.php:6

2) foo_test::test_2_fail
Failed asserting that two strings are equal.

/bar.php:6

FAILURES!
Tests: 3, Assertions: 3, Failures: 2.

unit.xml

<phpunit 
    backupGlobals="false" 
    backupStaticAttributes="false" 
    syntaxCheck="false">
  <testsuites>
    <testsuite name="foo">
      <directory suffix=".php">bar/foo</directory>
    </testsuite>
  </testsuites>
</phpunit>

【问题讨论】:

  • 你用什么命令来启动 phpunit?
  • 已将命令添加到描述@gontrollez
  • 如果只使用 /bar/foo/foo.php 测试更改文件节点的目录 XML 节点会发生什么?
  • 我用&lt;file&gt;bar/foo/foo.php&lt;/file&gt; 替换了目录行,但我的行为相同。
  • 我解决了这个问题。这是类命名,这是错误的。如果文件名相同,则一切正常。

标签: php unit-testing phpunit automated-tests


【解决方案1】:

如果您不打算自己执行父测试类,请将其抽象化,这样可以防止 PHPUnit 尝试实例化它并运行它的测试。

【讨论】:

    猜你喜欢
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    • 2017-11-03
    • 1970-01-01
    • 2013-03-25
    • 2014-09-07
    • 2012-08-17
    相关资源
    最近更新 更多