簡體   English   中英

使用 PyTest 固定裝置而不通過它們

[英]Using PyTest fixture without passing them

我正在使用 PyTest 框架來編寫和運行我的測試。
我已經實現了一個具體的記錄器:

class Logger(object):

    class LogFormats:
        ...

    def __init__(self, testname ,setup ,silent=True):
        """
        creating concrete logger for pytest.
        the logger will create a file for the test in specific test directory in quali FS and will
        write to this file all test log output (colored).
        :param: testname: test name - recieved from pytest fixtures (command line parameters)
        :param: setup: test setup - recieved from pytest fixtures (command line parameters)
        :param: silent: log test in silent mode (info only) or not silent mode (everything is logged)
        :param: root_password: password for root user
        """
    ....

...

在 conftest.py 文件中,我編寫了將在請求此記錄器時調用的函數(創建記錄器裝置)

@pytest.fixture(scope="module",autouse=True)
def logger(request):
    setup = request.config.getoption('--setupname')
    logger = Logger(testname=request.node.name, setup=setup)
    return logger

現在,我的問題是如何使用 pytest 使這個具體的記錄器全球化?
這意味着我不想將它作為參數傳遞給測試函數,如下所示:

def test_logger(other_fixture,logger):

但仍然可以在test_logger測試函數中使用它(如全局變量)

你可以做

@pytest.mark.usefixtures("logger")
def test_logger(other_fixture):

暫無
暫無

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

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