python - 如何在Python pytest中继承UnitTest类的类中使用fixture

标签 python python-3.x pytest python-unittest fixtures

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 新手。当我尝试在测试类中使用固定装置时,它失败了。 您能帮我解决一下吗? 提前致谢。 enter image description here

最佳答案

不幸的是,我认为没有办法做到这一点。在 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.

因此,您的 clientclient_1 函数可以在测试用例运行时调用,但您无法访问它们的返回值。

关于python - 如何在Python pytest中继承UnitTest类的类中使用fixture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64640230/

相关文章:

python - 为什么当我点击 Dictionary 变量时 Spyder 不能打开它?

python-3.x - 运行尝试微调 Wav2Wav 医学词汇模块的单元时出现混合精确 Cuda 错误

python - pytest:在线程中运行

python - 需要基本的Python重复打印建议

python - 如何在python 3中使用base64对字符串进行编码并删除新行

python - 忽略 ensurepip 失败 : pip 7. 1.2 需要 SSL/TLS - Python 3.x 和 OS X

Python 编码问题(可能是从 windows 到 linux 的问题)

python-3.x - 如何从 Pandas 数据框中的两列中提取匹配的关键字?

python - pytest:带参数的自定义标记