Give warning when test function return other than None

This commit is contained in:
Cheukting 2022-05-13 18:54:33 +01:00
parent 56c266640e
commit 88634bd086
2 changed files with 17 additions and 0 deletions

View File

@ -77,10 +77,12 @@ from _pytest.pathlib import parts
from _pytest.pathlib import visit
from _pytest.scope import Scope
from _pytest.warning_types import PytestCollectionWarning
from _pytest.warning_types import PytestReturnNotNoneWarning
from _pytest.warning_types import PytestUnhandledCoroutineWarning
if TYPE_CHECKING:
from typing_extensions import Literal
from _pytest.scope import _ScopeName
@ -192,6 +194,14 @@ def pytest_pyfunc_call(pyfuncitem: "Function") -> Optional[object]:
result = testfunction(**testargs)
if hasattr(result, "__await__") or hasattr(result, "__aiter__"):
async_warn_and_skip(pyfuncitem.nodeid)
elif result is not None:
warnings.warn(
PytestReturnNotNoneWarning(
"Test function returning {result}, do you mean to use `assert` instead or `return`?".format(
result=result
)
)
)
return True

View File

@ -42,6 +42,13 @@ class PytestCollectionWarning(PytestWarning):
__module__ = "pytest"
@final
class PytestReturnNotNoneWarning(PytestWarning):
"""Warning emitted when a test function is returning value other than None."""
__module__ = "pytest"
class PytestDeprecationWarning(PytestWarning, DeprecationWarning):
"""Warning class for features that will be removed in a future version."""