几个模块的Pytest init设置

Ost*_*huk 9 python unit-testing pytest

说我有下一个测试结构:

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

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

hpk*_*k42 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
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅pytest fixture文档.

  • 我只是想强调,在'conftest.py`中是解决这个问题的'全局性'的关键点.非常感谢! (5认同)