Pytest - 在一次或多次测试后运行测试foo任意次数

时间:2015-07-27 17:34:04

标签: pytest

我有这样的测试 -

class TestMyUsefulTest:
    # Calling test_foo for the FIRST time
    def test_foo_1(self, my_fixture):
        # do something in the test method
        assert 0 == 0  # I am asserting something in my test

    def test_bar(self, my_fixture):
        # do something in the test method
        assert 0 == 0  # I am asserting something in my test

    # Calling test_foo for the SECOND time
    def test_foo_2(self, my_fixture):
        # do something in the test method
        assert 0 == 0  # I am asserting something in my test

第二次调用test_foo时,我做的与第一次完全相同。

有没有办法调用test_foo,而不必再重复一次代码?

这是基于@ DJanssens'的实现。评论 -

class TestMyUsefulTest:
    def foo(self, a_fixture):
        # do stuff with a_fixture
        assert 0 == 0

    def test_foo_1(self, a_fixture):
        self.foo(a_fixture)

    def test_bar(self, a_fixture):
        assert 0 == 0

    def test_foo_2(self, a_fixture):
        self.foo(a_fixture)

0 个答案:

没有答案