在父类中运行两次单元测试


Unittests running twice in parent class

出于许多充分的原因,我将一些测试放入父测试类中,并将一些测试置于固有类中。我得到了这样的结构:

/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尝试实例化它并运行它的测试。