Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle -s flag the same between Fixture and TestCase #141

Open
mkmoisen opened this issue Jul 13, 2024 · 0 comments
Open

Handle -s flag the same between Fixture and TestCase #141

mkmoisen opened this issue Jul 13, 2024 · 0 comments

Comments

@mkmoisen
Copy link

mkmoisen commented Jul 13, 2024

When writing a plugin that attempts to capture the subtest report, it was discovered that the subtest report will not be printed unless the -s flag is used while subclassing unittest.TestCase, however it works fine when using pytest fixtures instead.

Would you please make the behavior consistent?

You can reproduce via the following:

conftest.py

def pytest_runtest_logreport(report):
    print(type(report), report)

test_foo.py

from unittest import TestCase


class TestFoo(TestCase):
    def test_all(self):
        with self.subTest('test_foo'):
            raise Exception('foo')
        with self.subTest('test_bar'):
            raise Exception('bar')


def test_baz(subtests):
    with subtests.test('test_baz_first'):
        raise Exception('first')
    with subtests.test('test_baz_second'):
        raise Exception('second')

pytest test_foo.py

sh-4.4$ pytest test_foo.py 
===================================================================================== test session starts ======================================================================================
platform linux -- Python 3.12.4, pytest-8.2.1, pluggy-1.5.0
rootdir: /opt/app/src/foo
plugins: cov-5.0.0, html-4.1.1, metadata-3.1.1, rerunfailures-14.0, subtests-0.13.0, timer-1.0.0, xdist-3.5.0
collected 2 items                                                                                                                                                                              

test_foo.py <class '_pytest.reports.TestReport'> <TestReport 'test_foo.py::TestFoo::test_all' when='setup' outcome='passed'>
.<class '_pytest.reports.TestReport'> <TestReport 'test_foo.py::TestFoo::test_all' when='call' outcome='passed'>
<class '_pytest.reports.TestReport'> <TestReport 'test_foo.py::TestFoo::test_all' when='teardown' outcome='passed'>
<class '_pytest.reports.TestReport'> <TestReport 'test_foo.py::test_baz' when='setup' outcome='passed'>
u<class 'pytest_subtests.plugin.SubTestReport'> SubTestReport(context=SubTestContext(msg='test_baz_first', kwargs={}))
u<class 'pytest_subtests.plugin.SubTestReport'> SubTestReport(context=SubTestContext(msg='test_baz_second', kwargs={}))
.<class '_pytest.reports.TestReport'> <TestReport 'test_foo.py::test_baz' when='call' outcome='passed'>
<class '_pytest.reports.TestReport'> <TestReport 'test_foo.py::test_baz' when='teardown' outcome='passed'>

pytest test_foo.py -s

sh-4.4$ pytest test_foo.py -s
===================================================================================== test session starts ======================================================================================
platform linux -- Python 3.12.4, pytest-8.2.1, pluggy-1.5.0
rootdir: /opt/app/src/foo
plugins: cov-5.0.0, html-4.1.1, metadata-3.1.1, rerunfailures-14.0, subtests-0.13.0, timer-1.0.0, xdist-3.5.0
collected 2 items                                                                                                                                                                              

test_foo.py <class '_pytest.reports.TestReport'> <TestReport 'test_foo.py::TestFoo::test_all' when='setup' outcome='passed'>
u<class 'pytest_subtests.plugin.SubTestReport'> SubTestReport(context=SubTestContext(msg='test_foo', kwargs={}))
u<class 'pytest_subtests.plugin.SubTestReport'> SubTestReport(context=SubTestContext(msg='test_bar', kwargs={}))
.<class '_pytest.reports.TestReport'> <TestReport 'test_foo.py::TestFoo::test_all' when='call' outcome='passed'>
<class '_pytest.reports.TestReport'> <TestReport 'test_foo.py::TestFoo::test_all' when='teardown' outcome='passed'>
<class '_pytest.reports.TestReport'> <TestReport 'test_foo.py::test_baz' when='setup' outcome='passed'>
u<class 'pytest_subtests.plugin.SubTestReport'> SubTestReport(context=SubTestContext(msg='test_baz_first', kwargs={}))
u<class 'pytest_subtests.plugin.SubTestReport'> SubTestReport(context=SubTestContext(msg='test_baz_second', kwargs={}))
.<class '_pytest.reports.TestReport'> <TestReport 'test_foo.py::test_baz' when='call' outcome='passed'>
<class '_pytest.reports.TestReport'> <TestReport 'test_foo.py::test_baz' when='teardown' outcome='passed'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant