简体   繁体   English

使用来自普通测试 function 的 pytest 夹具

[英]Using pytest fixture from common test function

I have some unittest -based code that currently looks like this :我有一些基于unittest的代码, 目前看起来像这样

class TestMain(TestCase):
    def run_prog(self, args):
        with TemporaryFile() as stdout:
            old_stdout = sys.stdout
            try:
                main.main()
                stdout.seek(0)
                stdout_data = stdout.read()
            finally:
                sys.stdout = old_stdout
        return stdout_data

    def test_one(self):
        out = self.run_prog(...)

    def test_two(self):
        out = self.run_prog(...)

    def test_three(self):
        out = self.run_prog(...)

run_prog invokes the "main" program under test and manually captures its stdout. run_prog调用被测“主”程序并手动捕获其标准输出。

I'm in the process of converting this project to pytest , but this last piece has pushed the limits of my understanding of pytest fixtures.我正在将此项目转换为pytest的过程中,但最后一块已经突破了我对 pytest 灯具的理解的极限。

I understand that pytest has full support for capturing stdout/stderr and I would like to leverage this.我了解 pytest 完全支持捕获标准输出/标准错误,我想利用它。

The problem is, their examples work on a test-function level:问题是,他们的示例在测试功能级别上工作:

def test_myoutput(capfd):
    do_something
    captured = capsys.readouterr()
    assert captured.out == "hello\n"
    assert captured.err == "world\n"

In my case, run_prog is used 42 times, so I'm trying to use the fixture starting at run_prog -- the calling functions ideally don't need to bother with capsys / capfd .就我而言, run_prog被使用了 42 次,所以我尝试使用从run_prog开始的夹具——理想情况下,调用函数不需要使用capsys / capfd

Is there a way to "invoke" the fixture from my run_prog helper?有没有办法从我的run_prog助手“调用”夹具? Or do I need to add capfd to all 42 tests and pass it to run_prog ?还是我需要将capfd添加到所有 42 个测试并将其传递给run_prog

You can define an autouse fixture that will store the CaptureFixture object (returned by the capsys fixture) as an instance property:您可以定义一个自动使用夹具,它将CaptureFixture object(由capsys夹具返回)存储为实例属性:

class TestMain(TestCase):

    @pytest.fixture(autouse=True)
    def inject_capsys(self, capsys):
        self._capsys = capsys

    def run_prog(self, args):
        main.main()
        return self._capsys.out

    def test_out(self):
        assert self.run_prog('spam') == 'eggs'

The TestMain.inject_capsys fixture will be rerun for each test, guaranteeing the test isolation (no output from test_one will be leaked in test_two etc). TestMain.inject_capsys夹具将为每个测试重新运行,以保证测试隔离(没有来自 test_one 的test_one将在test_two等中泄漏)。

Here's a slight variation on hoefling's answer that gives a little more control over the scope of the capsys fixture.这是hoefling 答案的一个细微变化,可以对capsys夹具的 scope 进行更多控制。

It uses request.getfixturevalue() to retrieve the fixture at function invocation time:它使用request.getfixturevalue()在 function 调用时间检索夹具:

import pytest
import sys

class TestMain:
    @pytest.fixture(autouse=True)
    def inject_request(self, request):
        self.request = request

    def run_prog(self, message):
        capfd = self.request.getfixturevalue('capfd')

        sys.stdout.write(message)

        captured = capfd.readouterr()
        assert captured.out == message

    def test_one(self):
        self.run_prog("Hello world!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM