File tree 2 files changed +22
-1
lines changed
2 files changed +22
-1
lines changed Original file line number Diff line number Diff line change 69
69
from _pytest .scope import _ScopeName
70
70
from _pytest .scope import Scope
71
71
from _pytest .stash import StashKey
72
- from _pytest .warning_types import PytestCollectionWarning
72
+ from _pytest .warning_types import PytestCollectionWarning , PytestDefaultArgumentWarning , warn_explicit_for
73
73
74
74
75
75
if TYPE_CHECKING :
@@ -143,9 +143,23 @@ def async_fail(nodeid: str) -> None:
143
143
fail (msg , pytrace = False )
144
144
145
145
146
+ def async_default_arg_warn (nodeid : str , function_name , param ) -> None :
147
+ msg = (
148
+ "Test function '" + function_name + "' has a default argument '" + param .name + "=" + str (param .default ) + "'.\n "
149
+ )
150
+ warnings .simplefilter ("always" , PytestDefaultArgumentWarning )
151
+ warnings .warn (PytestDefaultArgumentWarning (msg ))
152
+
153
+
146
154
@hookimpl (trylast = True )
147
155
def pytest_pyfunc_call (pyfuncitem : Function ) -> object | None :
148
156
testfunction = pyfuncitem .obj
157
+ sig = inspect .signature (testfunction )
158
+ for param in sig .parameters .values ():
159
+ if param .default is not param .empty :
160
+ function_name = testfunction .__name__
161
+ async_default_arg_warn (pyfuncitem .nodeid , function_name , param )
162
+
149
163
if is_async_function (testfunction ):
150
164
async_fail (pyfuncitem .nodeid )
151
165
funcargs = pyfuncitem .funcargs
Original file line number Diff line number Diff line change @@ -44,6 +44,13 @@ class PytestCollectionWarning(PytestWarning):
44
44
__module__ = "pytest"
45
45
46
46
47
+ @final
48
+ class PytestDefaultArgumentWarning (PytestWarning ):
49
+ """Warning emitted when a test function has default arguments."""
50
+
51
+ __module__ = "pytest"
52
+
53
+
47
54
class PytestDeprecationWarning (PytestWarning , DeprecationWarning ):
48
55
"""Warning class for features that will be removed in a future version."""
49
56
You can’t perform that action at this time.
0 commit comments