[英]Can I define functions other than fixtures in conftest.py
我可以在 conftest.py 中定義固定裝置以外的功能嗎
如果我有一個 function add(),在 conftest.py 中定義
我可以在 test_add.py 文件中使用 function 只需調用 add()
兩種可能的方式
您可以從 conftest.py 中導入 function 添加,或將其移至更合適的位置。 (例如 utils.py)
您可以創建返回 function 的夾具並在您的測試中使用它。 像這樣的東西。
conftest.py
import pytest
@pytest.fixture
def add():
def inner_add(x, y):
return x + y
return inner_add
test_all.py
def test_all(add):
assert add(1, 2) == 3
我無法想象什么時候它會是一個很好的解決方案。 但我確信它存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.