每次运行pytest测试用例时,如何确保特定的代码段运行?

时间:2016-02-04 14:36:29

标签: pytest pytest-django

@pytest.mark.django_db
class TestClass():

    def do_setup(self):
        # do setup

    def test_a(self):
        # do something

    def test_b(self):
        # do something

在test_a和test_b测试用例运行之前,我需要调用do_setup()。我正在使用pytest-django框架。

请帮助。

提前致谢

1 个答案:

答案 0 :(得分:0)

使用方法/功能级别设置拆解

def setup_method(self, method):
    """ setup any state tied to the execution of the given method in a
    class.  setup_method is invoked for every test method of a class.
    """

def teardown_method(self, method):
    """ teardown any state that was previously setup with a setup_method
    call.
    """

参考http://pytest.org/latest/xunit_setup.html#method-and-function-level-setup-teardown