conftest.py
import pytest
@pytest.fixture(scope="session")
def client():
env_name = 'FLASK_ENV'
return env_name
@pytest.fixture(scope="session")
def client_1():
env_name = 'FLASK_ENV_1'
return env_name
test_run.py
import pytest
import unittest
@pytest.mark.usefixtures("client")
@pytest.mark.usefixtures("client_1")
class TestStaticPages(unittest.TestCase):
@classmethod
def setUpClass(self):
self.value = "ABC"
def test_base_route(self, client, client_1):
response = client
assert response == 'FLASK_ENV'
assert client_1 == 'FLASK_ENV_1'
我是 python 中的 UnitTest 新手。当我尝试在测试类中使用固定装置时,它失败了。 您能帮我解决一下吗? 提前致谢。
最佳答案
不幸的是,我认为没有办法做到这一点。在 pytest documentation ,指出:
unittest.TestCase methods cannot directly receive fixture arguments as implementing that is likely to inflict on the ability to run general unittest.TestCase test suites.
因此,您的 client
和 client_1
函数可以在测试用例运行时调用,但您无法访问它们的返回值。
关于python - 如何在Python pytest中继承UnitTest类的类中使用fixture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64640230/