File tree 3 files changed +31
-0
lines changed 3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ A warning is emitted when a ``async `` test function is encountered and is not handled by any
2
+ of the existing async frameworks (such as ``pytest-asyncio `` or ``pytest-trio ``).
Original file line number Diff line number Diff line change @@ -156,6 +156,15 @@ def pytest_configure(config):
156
156
@hookimpl (trylast = True )
157
157
def pytest_pyfunc_call (pyfuncitem ):
158
158
testfunction = pyfuncitem .obj
159
+ iscoroutinefunction = getattr (inspect , "iscoroutinefunction" , None )
160
+ if iscoroutinefunction is not None and iscoroutinefunction (testfunction ):
161
+ msg = "test function {} is a coroutine and not natively supported.\n "
162
+ msg += "you need to install a suitable plugin for your async framework, for example:\n "
163
+ msg += " - pytest-asyncio\n "
164
+ msg += " - pytest-trio\n "
165
+ msg += " - pytest-tornasync"
166
+ warnings .warn (PytestWarning (msg .format (pyfuncitem .nodeid )))
167
+ return None
159
168
funcargs = pyfuncitem .funcargs
160
169
testargs = {arg : funcargs [arg ] for arg in pyfuncitem ._fixtureinfo .argnames }
161
170
testfunction (** testargs )
Original file line number Diff line number Diff line change @@ -1179,3 +1179,23 @@ def test_fixture_mock_integration(testdir):
1179
1179
def test_usage_error_code (testdir ):
1180
1180
result = testdir .runpytest ("-unknown-option-" )
1181
1181
assert result .ret == EXIT_USAGEERROR
1182
+
1183
+
1184
+ @pytest .mark .skipif (
1185
+ sys .version_info [:2 ] < (3 , 5 ), reason = "async def syntax python 3.5+ only"
1186
+ )
1187
+ @pytest .mark .filterwarnings ("default" )
1188
+ def test_warn_on_async_function (testdir ):
1189
+ testdir .makepyfile (
1190
+ test_async = """
1191
+ async def test_1():
1192
+ pass
1193
+ """
1194
+ )
1195
+ result = testdir .runpytest ()
1196
+ result .stdout .fnmatch_lines (
1197
+ [
1198
+ "*test function test_async.py::test_1 is a coroutine*" ,
1199
+ "*1 passed, 1 warnings in*" ,
1200
+ ]
1201
+ )
You can’t perform that action at this time.
0 commit comments