python - 更改在 pytest 中调用固定装置的方式
问题描述
我在 conftest.py 中有一个固定装置
@pytest.fixture(scope="function", autouse=True)
@pytest.mark.usefixtures
def pause_on_assert():
yield
if hasattr(sys, 'last_value') and isinstance(sys.last_value, AssertionError):
tkinter.messagebox.showinfo(sys.last_value)
同样,conftest.py 中还有许多其他修复程序,其范围为session
,module
我的测试用例看起来像这样
test.py
@pytest.fixture(scope="function", autouse=True)
def _wrapper:
print("pre condition")
yield
print("post condition")
def test_abc():
assert 1==0
问题是我希望 conftest.py 中yield
的夹具在我的测试用例中的夹具之前运行
如何更改夹具执行方式的顺序
解决方案
这是在打印“B”的测试函数之前运行打印“A”的 conftest.py 函数的示例。
cd 到父目录,对于这个例子,它是 py_tests 并运行。
pytest -s -v
输出是:
A
setting up
B
PASSED
目录结构:
py_tests
-conftest.py
-tests
--tests.py
文件:
conftest.py
import pytest
@pytest.fixture(scope="function")
def print_one():
print("\n")
print("A")
test.py
import pytest
class Testonething:
@pytest.fixture(scope="function", autouse=True)
def setup(self, print_one):
print("setting up")
def test_one_thing(self):
print("B")
assert True
推荐阅读
- reactjs - 无法在 React 中设置属性“不确定”的 null
- python - 在函数中打印返回值,Python
- c# - 如何使用asp.net通过gridview中的复选框删除多条记录
- javascript - 如何使用基本代码找到三角形的面积,例如:
- php - 如何使用 Laravel 默认重置密码链接附加电子邮件
- philips-hue - 来自 philips-hue 桥的“未经授权的用户”响应
- ios - iOS Twin Push SDK Lib showNotification 未触发
- sparql - DBpedia 中等效的 Wikidata 查询
- angular - 如何连接两种形式(一种是另一个内的数组)
- powershell - 在 Active Directory 中按真实姓名查找用户