Debian Codegraph @

tests/markers/test_invalid_arguments.py

raw
1 from __future__ import annotations
2
3 from textwrap import dedent
4
5 import pytest
6
7
8 def test_no_error_when_scope_passed_as_sole_keyword_argument(
9 pytester: pytest.Pytester,
10 ):
11 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
12 pytester.makepyfile(dedent("""\
13 import pytest
14
15 @pytest.mark.asyncio(loop_scope="session")
16 async def test_anything():
17 pass
18 """))
19 result = pytester.runpytest("--assert=plain")
20 result.assert_outcomes(passed=1)
21 result.stdout.no_fnmatch_line("*ValueError*")
22
23
24 def test_error_when_scope_passed_as_positional_argument(
25 pytester: pytest.Pytester,
26 ):
27 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
28 pytester.makepyfile(dedent("""\
29 import pytest
30
31 @pytest.mark.asyncio("session")
32 async def test_anything():
33 pass
34 """))
35 result = pytester.runpytest("--assert=plain")
36 result.assert_outcomes(errors=1)
37 result.stdout.fnmatch_lines(
38 ["*ValueError: mark.asyncio accepts only keyword arguments*"]
39 )
40
41
42 def test_error_when_wrong_keyword_argument_is_passed(
43 pytester: pytest.Pytester,
44 ):
45 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
46 pytester.makepyfile(dedent("""\
47 import pytest
48
49 @pytest.mark.asyncio(cope="session")
50 async def test_anything():
51 pass
52 """))
53 result = pytester.runpytest("--assert=plain")
54 result.assert_outcomes(errors=1)
55 result.stdout.fnmatch_lines(
56 ["*ValueError: mark.asyncio accepts only keyword arguments*"]
57 )
58
59
60 def test_error_when_additional_keyword_arguments_are_passed(
61 pytester: pytest.Pytester,
62 ):
63 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
64 pytester.makepyfile(dedent("""\
65 import pytest
66
67 @pytest.mark.asyncio(loop_scope="session", more="stuff")
68 async def test_anything():
69 pass
70 """))
71 result = pytester.runpytest("--assert=plain")
72 result.assert_outcomes(errors=1)
73 result.stdout.fnmatch_lines(
74 ["*ValueError: mark.asyncio accepts only keyword arguments*"]
75 )
76
77
78 @pytest.mark.parametrize(
79 "loop_factories_value",
80 ('"custom"', "[]", '[""]', "[1]"),
81 )
82 def test_error_when_loop_factories_marker_value_is_invalid(
83 pytester: pytest.Pytester, loop_factories_value: str
84 ):
85 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
86 pytester.makeconftest(dedent("""\
87 import asyncio
88
89 class CustomEventLoop(asyncio.SelectorEventLoop):
90 pass
91
92 def pytest_asyncio_loop_factories(config, item):
93 return {"custom": CustomEventLoop}
94 """))
95 pytester.makepyfile(dedent(f"""\
96 import pytest
97
98 pytest_plugins = "pytest_asyncio"
99
100 @pytest.mark.asyncio(loop_factories={loop_factories_value})
101 async def test_anything():
102 pass
103 """))
104 result = pytester.runpytest("--assert=plain")
105 result.assert_outcomes(errors=1)
106 result.stdout.fnmatch_lines(
107 ["*ValueError: mark.asyncio 'loop_factories' must be a non-empty sequence*"]
108 )