python - 几个模块的 Pytest init 设置

标签 python unit-testing pytest

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

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

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

最佳答案

您可以使用自动装置:

# 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获取更多信息。

关于python - 几个模块的 Pytest init 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19225279/

相关文章:

unit-testing - 确定哪些测试覆盖一行代码

asp.net-mvc - 编写单元测试来验证所有 ASP.NET MVC Controller 都继承了一个基本 Controller

测试失败时 Docker 构建 : returned a non-zero code: 1,

python - pytest 不允许具有非静态方法的类

python - 使用两个不同版本的python,但sqlmap需要2.7

python - Django 重定向未按预期工作

java - EasyMock - 部分模拟将模拟分配给特定字段

匹配所有内容的python列表

python - 组合 CSV 文件集 python