简体   繁体   English

pytest:在测试文件中添加夹具,而不是conftest.py

[英]pytest: Adding fixture in the test file instead of conftest.py

I am new to Python and I have a doubt in pytest 我是Python的新手,我对pytest有疑问

test_client.py test_client.py

# Simple Example tests

import pytest

def test_one():
   assert False == False

def test_two():
   assert True == True

def cleanup():
   # do some cleanup stuff

conftest.py conftest.py

import pytest
import test_client

@pytest.fixture(scope="session", autouse=True)
def do_clean_up(request):
    request.addfinalizer(test_client.cleanup)

Is it possible to move the fixture defined in conftest.py to the test_client.py thereby eliminating having conftest.py 是否可以将conftest.py中定义的灯具移动到test_client.py从而消除conftest.py

Yes. 是。 Why didn't you simply try? 您为什么不简单尝试? ;) ;)

Fixtures are put in conftest.py files to be able to use them in multiple test files. 灯具放在conftest.py文件中,以便可以在多个测试文件中使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pytest html - 将图像从测试文件传递给 conftest.py 中的钩子 - pytest html - pass images to the hook in conftest.py from a test file 如何在 conftest.py 中设置会话详细信息以获取收益以及 pytest 中的夹具? - How can I set session details in conftest.py for yield along with fixture in pytest? 在Robot Framework上是否有conftest.py pytest等价物 - Is there a conftest.py pytest equivalent on Robot Framework PyTest - 在 conftest.py 中指定清理测试 - PyTest - Specify cleanup tests in conftest.py 在pytest中,conftest.py文件有什么用? - In pytest, what is the use of conftest.py files? 从conftest.py访问测试文件名 - Accessing test file name from conftest.py 我的所有测试函数都在加载conftest.py中的一个fixture,即使他们不需要它 - All my test functions are loading a fixture that is in the conftest.py, even when they don't need it Pytest - 将测试运行生成的数据提取到 conftest 中的文件/夹具? - Pytest - Extract data generated by test run to a file/ fixture in conftest? 如何仅将 conftest.py 中的夹具应用到内部文件夹 - How to apply fixture from conftest.py to inner folders only 使用 Pytest 和 conftest.py 文件时未显示断言错误的预期结果 - Expected results of Assertion Errors are not showing up when using Pytest and conftest.py file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM