Pytest caplog (log output)

huangapple go评论61阅读模式
英文:

Pytest caplog (log output)

问题

I apologize, but I can't assist with that request.

英文:

Need to get logs output for unit-tests with Python pytest. It works if I pass caplog as an argument to regular test_... function. How to pass it to Test... class method?

This code works fine:

def test_foo(caplog):
    assert 'warning text' in caplog.text

This gives empty caplog.text:

class TestFoo:
    def test_foo(self, caplog):
        assert 'warning text' in caplog.text

Tried this as well (found here: https://stackoverflow.com/questions/50373916/pytest-to-insert-caplog-fixture-in-test-method) - still empty:

class TestFoo:
    @pytest.fixture(autouse=True)
    def inject_fixtures(self, caplog):
        self._caplog = caplog

    def test_foo(self):
        assert 'warning text' in self._caplog.responses[0]
        assert 'warning text' in self._caplog.messages[0]

Last one fails as responses and messages are empty lists. While caplog itself is an existing _pytest.logging.LogCaptureFixture object but with empty content.

EDITED:
The issue is that I capture logs not in the method itself but in other fixture that prepares data for the test method. So the question is - how to capture logs in other fixture?

class TestRotaion:
    @pytest.fixture
    def foo_fixture(self, caplog):
        ...
        self._caplog = caplog
        ...

    def test_foo(self, foo_fixture):
        assert 'text' in caplog.text

Tried this as well but same - empty caplog.

@pytest.fixture
@pytest.mark.usefixtures('caplog')
def foo_fixture(self, caplog):
    ...

答案1

得分: 0

使用 caplog fixture 与 Test 类一起使用时,需要使用 pytest.mark.usefixtures 标记来指示您要使用该 fixture。

import pytest

class TestFoo:
    @pytest.mark.usefixtures("caplog")
    def test_foo(self, caplog):
        assert 'warning text' in caplog.text
英文:

When using caplog fixture with a Test class, you need to use the pytest.mark.usefixtures marker to indicate that you want to use the fixture.

import pytest

class TestFoo:
    @pytest.mark.usefixtures("caplog")
    def test_foo(self, caplog):
        assert 'warning text' in caplog.text

答案2

得分: 0

问题在于在测试装置中,我保存的不是来自 caplog 的文本,而是 caplog 本身——在主测试函数开始时,它已经为空。

因此,正确的代码是:

class TestFoo:
    @pytest.fixture
    def foo_fixture(self, caplog):
        ...
        self._caplog_text = caplog.text
        ...
    
    def test_foo(self, foo_fixture):
        assert 'text' in self._caplog_text
英文:

The issues was that in fixture I saved not the text from caplog but caplog itself - it was already empty when main test func started.

So working code is:

class TestFoo:
    @pytest.fixture
    def foo_fixture(self, caplog):
        ...
        self._caplog_text = caplog.text
        ...
    
    def test_foo(self, foo_fixture):
        assert 'text' in self._caplog_text

huangapple
  • 本文由 发表于 2023年5月11日 05:10:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222580.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定