【问题标题】:How to use fixtures in a Class which is inherting UnitTest class in python pytest如何在继承 Python pytest 中的 UnitTest 类的类中使用固定装置
【发布时间】:2020-11-02 05:28:52
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    不幸的是,我认为没有办法做到这一点。在pytest documentation中声明:

    unittest.TestCase 方法不能直接接收fixture 参数作为实现,这可能会影响运行一般 unittest.TestCase 测试套件的能力。

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

    【讨论】:

      猜你喜欢
      • 2020-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-08
      • 1970-01-01
      • 2020-10-04
      相关资源
      最近更新 更多