首页 文章

pytest运行方法两次

提问于
浏览
1

在两个不同的模块上运行测试方法时遇到问题 . 我在不同的模块中创建了套件功能,并将其定义为夹具 .

在两个测试类中,我已经进入了创建的fixture,只将它用作设置功能一次 .

对于每种测试方法,我都创建了setup和teardown方法 . 当运行第一个模块的测试方法时,来自另一个类(第二个模块)的测试从第一个类开始,然后在第二个类中再次运行 . 因此,第二类中的测试方法运行两次,一次来自第一个模块中的第一个类,然后是第二个模块中的第二个类 .

我想每个类或模块运行一次测试方法,而不是运行两次 .

有人可以帮助我找到解决方案吗?

PS:我用于套件夹具参数scope ='session'(尝试与模块相同)!

示例(conftest.py):

@pytest.fixture(scope="session")
def suite():
    print "start application"
    open_app()

示例(test.py):

def setup_method(self, method):
        if method.__name__ == 'test_1':
            pass
        elif method.__name__ == 'test_2':
            pass
        else:
            print "test method not found"

    def teardown_method(self, method):
        print "teardown methods"



    def test_1(self):
        pass

    def test_2(self):
        pass

    def test_3(self):
        pass

    def setup_test_3(self, testcase):
        print "this is only for the test methd: test_3"

    def teardown_test_3(self):
        print "cleanup state after running test method test_3"

1 回答

  • 0

    您可以将参数传递给setup和teardown函数,并根据类或方法名称或模块名称放置条件并相应地执行操作 .

    例如: -

    def setup_method(value):
        if value.__name__ == '<name of method from the module1>':
            pass
        elif value.__name__ == '<name of method from the module2>':
            pass
    

相关问题