【问题标题】:How do I make sure a specific code snippet runs everytime a pytest testcase runs?如何确保每次运行 pytest 测试用例时都运行特定的代码片段?
【发布时间】:2016-02-04 14:36:28
【问题描述】:
@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 框架。

请帮忙。

提前致谢

【问题讨论】:

    标签: pytest pytest-django


    【解决方案1】:

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

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 2013-01-17
      • 2020-07-02
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      • 2014-12-12
      相关资源
      最近更新 更多