簡體   English   中英

如何在繼承了python pytest中的UnitTest類的類中使用fixtures

[英]How to use fixtures in a Class which is inherting UnitTest class in python pytest

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

測試運行.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 文檔中,指出:

unittest.TestCase 方法不能直接接收夾具參數作為實現,這可能會影響運行通用 unittest.TestCase 測試套件的能力。

因此,您的clientclient_1函數可以在測試用例運行時調用,但您無法訪問它們的返回值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM