2014-09-03 103 views
1

由於很多好的原因,我將一些測試放入了父測試類和一些測試類。我有這個結構,那麼:在父類中運行兩次的單元測試

/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> 
+0

你用什麼命令啓動phpunit? – gontrollez 2014-09-04 13:10:45

+0

已將說明添加到說明@gontrollez – Vladislav 2014-09-04 18:12:41

+0

如果僅使用/bar/foo/foo.php測試更改文件節點的目錄XML節點,會發生什麼情況? – gontrollez 2014-09-05 08:12:18

回答

0

如果您不打算執行父測試類自身,使其抽象,這樣可以防止從PHPUnit的嘗試實例並運行它的測試。