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

expose warnings= to pytester assert_outcomes() #8952

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/_pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ def assert_outcomes(
errors: int = 0,
xpassed: int = 0,
xfailed: int = 0,
warnings: int = 0,
) -> None:
"""Assert that the specified outcomes appear with the respective
numbers (0 means it didn't occur) in the text output from a test run."""
Expand All @@ -603,6 +604,7 @@ def assert_outcomes(
errors=errors,
xpassed=xpassed,
xfailed=xfailed,
warnings=warnings,
)


Expand Down
3 changes: 3 additions & 0 deletions src/_pytest/pytester_assertions.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def assert_outcomes(
errors: int = 0,
xpassed: int = 0,
xfailed: int = 0,
warnings: int = 0,
) -> None:
"""Assert that the specified outcomes appear with the respective
numbers (0 means it didn't occur) in the text output from a test run."""
Expand All @@ -54,6 +55,7 @@ def assert_outcomes(
"errors": outcomes.get("errors", 0),
"xpassed": outcomes.get("xpassed", 0),
"xfailed": outcomes.get("xfailed", 0),
"warnings": outcomes.get("warnings", 0),
}
expected = {
"passed": passed,
Expand All @@ -62,5 +64,6 @@ def assert_outcomes(
"errors": errors,
"xpassed": xpassed,
"xfailed": xfailed,
"warnings": warnings,
}
assert obtained == expected
2 changes: 1 addition & 1 deletion testing/test_nose.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def test_failing():
"""
)
result = pytester.runpytest(p)
result.assert_outcomes(skipped=1)
result.assert_outcomes(skipped=1, warnings=1)


def test_SkipTest_in_test(pytester: Pytester) -> None:
Expand Down
11 changes: 11 additions & 0 deletions testing/test_pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -847,3 +847,14 @@ def test_testdir_makefile_ext_empty_string_makes_file(testdir) -> None:
"""For backwards compat #8192"""
p1 = testdir.makefile("", "")
assert "test_testdir_makefile" in str(p1)


def test_pytester_assert_outcomes_warnings(pytester: Pytester) -> None:
p = pytester.makepyfile(
"""
def test_with_warning():
pass
"""
)
result = pytester.runpytest(p, "--strict")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm let's improve this by making the test itself generate the warning:

import warnings

def test_with_warning():
    warnings.warn(UserWarning("some custom warning"))

Let's not rely on --strict being deprecated, because then this test will break once we remove --strict and will need to adapt it anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @nicoddemus see my other comment, you read my mind :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh we posted the comments at the same time, it seems. 😁

Copy link
Member Author

@symonk symonk Jul 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when i use your approach with pytester, the test fails w/o warning (failed=1) only , is there a way to get the outcome from running a test w/o running pytester.runpytest() ?

edit: -W ::UserWarning I think can sort it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh right, what happens is that we change all warnings into errors when running the pytest suite.

You can override that applying @pytest.mark.filterwarnings("default") to test_pytester_assert_outcomes_warnings:

@pytest.mark.filterwarnings("default")
def test_pytester_assert_outcomes_warnings(pytester: Pytester) -> None:
    p = pytester.makepyfile(
        """
        import warnings
        def test_with_warning():
            warnings.warn(UserWarning("some warning"))
        """
    )
    result = pytester.runpytest(p)
    result.assert_outcomes(passed=1, warnings=1)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every day is a school day, thanks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any preferred way to generate the warnings here? using a deprecated feature will create additional work later for someone when erroring it I guess, would like to minimise that if the test can

result.assert_outcomes(passed=1, warnings=1)