测试之间的依赖性

时间:2017-03-23 08:53:38

标签: php phpunit

我尝试使用phpunit进行一系列测试,并依赖于几个类。我有3个类,A,B和C,每个类都有测试。 A有5个测试,B 3测试和C 3测试。

B对A有依赖性测试,C对A有依赖性测试,A最终测试依赖于B和C测试。

class A extends TestCase {

    public function test1(){}

    // @depends test1
    public function test2(){}

    // @depends test2
    public function test3(){}

    // @depends test3
    public function test4(){}

    // @depends test3
    // @depends B::test3
    // @depends C::test3
    public function test5(){}

}

class B extends TestCase {

    // @depends A::test1
    public function test1(){}

    // @depends B::test1
    public function test2(){}

    // @depends B::test2
    // @depends A::test4
    public function test3(){}

}

class C extends TestCase {

    // @depends A::test1
    public function test1(){}

    // @depends C::test1
    public function test2(){}

    // @depends C::test2
    // @depends A::test4
    public function test3(){}

}

我在这种情况下的问题是跳过A :: test5,B :: test3和C :: test3。

1 个答案:

答案 0 :(得分:0)

你所拥有的是一个非常糟糕的测试设计。

您应该能够彼此独立地运行所有测试。

如果您需要预先设置某些数据,请使用setUp()方法或测试方法本身进行设置。

正如已经指出的那样,如果你有外部依赖,那就嘲笑它们。

如果您有一组所有需要相同或类似数据的测试,那么将该设置代码放在基类中,然后使用您的测试扩展它是完全可以接受的。这样的事情。

class AppTestCase extends TestCase
{
    public function setUp()
    {

        // this will be run before every test class that extends this class.  So make your test user/product here.

        // you can invoke all your mocking in here to if thats easier, 
        // just make sure the resulting mock objects are all in visible 
        // properties to be accessed elsewhere
    }

    public function tearDown()
    {
        // if youre functional testing, remove test data in here. This will fire after each test has finished in any class that extends this one.

    }
}


class A extends AppTestCase
{
    public function setUp()
    {
        parent::setUp();

        // do other, class specific setup stuff here.

    }

    public function test1()
    {
        // your test data will be setup for you by the time you get here.
        // if you only have 1 test and it needs adapted base data, do it here,
        // if you have lots of tests in here, override and extend the `parent::setUp()` method.
    }
}

然后为所有测试重复该模式。

相关问题