几个模块的Pytest init设置

时间:2013-10-07 12:43:59

标签: python unit-testing pytest

假设我有下一个测试结构:

test/
  module1/   
    test1.py
  module2/
    test2.py
  module3/
    test3.py

如何在所有这些测试之前设置一些只调用一次的方法?

2 个答案:

答案 0 :(得分:13)

你可以使用autouse灯具:

# content of test/conftest.py 

import pytest
@pytest.fixture(scope="session", autouse=True)
def execute_before_any_test():
    # your setup code goes here, executed ahead of first test

有关详细信息,请参阅pytest fixture docs

答案 1 :(得分:0)

如果您的意思是每次运行测试服仅一次,则需要的是设置和拆卸。

def setup_module(module):
    print ("This will at start of module")

def teardown_module(module):
    print ("This will run at end of module")

类似地,您可以具有 setup_function teardown_function ,它们将分别在每个测试功能的开始和结束处运行。您还可以在测试类中添加 setup teardown 成员函数,以在测试类运行的开始和结束时运行。