55import pytest
66from packaging import version
77
8+ PYTEST_VERSION = version .parse (pytest .__version__ )
89pytest_plugins = "pytester"
910
1011
11- # result.stderr.no_fnmatch_line() is added to testdir on pytest 5.3.0
12+ # result.stderr.no_fnmatch_line() was added to testdir on pytest 5.3.0
1213# https://docs.pytest.org/en/stable/changelog.html#pytest-5-3-0-2019-11-19
13- def no_fnmatch_line (result , pattern ):
14- if version .parse (pytest .__version__ ) >= version .parse ("5.3.0" ):
15- result .stderr .no_fnmatch_line (pattern + "*" ,)
16- else :
17- assert pattern not in result .stderr .str ()
14+ def no_fnmatch_line (result : pytest .RunResult , pattern : str ):
15+ result .stderr .no_fnmatch_line (pattern + "*" )
1816
1917
20- def test_annotation_succeed_no_output (testdir ):
18+ def test_annotation_succeed_no_output (testdir : pytest . Testdir ):
2119 testdir .makepyfile (
2220 """
2321 import pytest
@@ -33,7 +31,7 @@ def test_success():
3331 no_fnmatch_line (result , "::error file=test_annotation_succeed_no_output.py" )
3432
3533
36- def test_annotation_pytest_error (testdir ):
34+ def test_annotation_pytest_error (testdir : pytest . Testdir ):
3735 testdir .makepyfile (
3836 """
3937 import pytest
@@ -55,7 +53,7 @@ def test_error():
5553 )
5654
5755
58- def test_annotation_fail (testdir ):
56+ def test_annotation_fail (testdir : pytest . Testdir ):
5957 testdir .makepyfile (
6058 """
6159 import pytest
@@ -68,11 +66,13 @@ def test_fail():
6866 testdir .monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
6967 result = testdir .runpytest_subprocess ()
7068 result .stderr .fnmatch_lines (
71- ["::error file=test_annotation_fail.py,line=5::test_fail*assert 0*" ,]
69+ [
70+ "::error file=test_annotation_fail.py,line=5::test_fail*assert 0*" ,
71+ ]
7272 )
7373
7474
75- def test_annotation_exception (testdir ):
75+ def test_annotation_exception (testdir : pytest . Testdir ):
7676 testdir .makepyfile (
7777 """
7878 import pytest
@@ -86,11 +86,51 @@ def test_fail():
8686 testdir .monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
8787 result = testdir .runpytest_subprocess ()
8888 result .stderr .fnmatch_lines (
89- ["::error file=test_annotation_exception.py,line=5::test_fail*oops*" ,]
89+ [
90+ "::error file=test_annotation_exception.py,line=5::test_fail*oops*" ,
91+ ]
92+ )
93+
94+
95+ def test_annotation_warning (testdir : pytest .Testdir ):
96+ testdir .makepyfile (
97+ """
98+ import warnings
99+ import pytest
100+ pytest_plugins = 'pytest_github_actions_annotate_failures'
101+
102+ def test_warning():
103+ warnings.warn('beware', Warning)
104+ assert 1
105+ """
90106 )
107+ testdir .monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
108+ result = testdir .runpytest_subprocess ()
109+ result .stderr .fnmatch_lines (
110+ [
111+ "::warning file=test_annotation_warning.py,line=6::beware" ,
112+ ]
113+ )
114+
115+
116+ def test_annotation_exclude_warnings (testdir : pytest .Testdir ):
117+ testdir .makepyfile (
118+ """
119+ import warnings
120+ import pytest
121+ pytest_plugins = 'pytest_github_actions_annotate_failures'
122+
123+ def test_warning():
124+ warnings.warn('beware', Warning)
125+ assert 1
126+ """
127+ )
128+ testdir .monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
129+ result = testdir .runpytest_subprocess ("--exclude-warning-annotations" )
130+ assert not result .stderr .lines
91131
92132
93- def test_annotation_third_party_exception (testdir ):
133+ def test_annotation_third_party_exception (testdir : pytest . Testdir ):
94134 testdir .makepyfile (
95135 my_module = """
96136 def fn():
@@ -111,11 +151,43 @@ def test_fail():
111151 testdir .monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
112152 result = testdir .runpytest_subprocess ()
113153 result .stderr .fnmatch_lines (
114- ["::error file=test_annotation_third_party_exception.py,line=6::test_fail*oops*" ,]
154+ [
155+ "::error file=test_annotation_third_party_exception.py,line=6::test_fail*oops*" ,
156+ ]
157+ )
158+
159+
160+ def test_annotation_third_party_warning (testdir : pytest .Testdir ):
161+ testdir .makepyfile (
162+ my_module = """
163+ import warnings
164+
165+ def fn():
166+ warnings.warn('beware', Warning)
167+ """
168+ )
169+
170+ testdir .makepyfile (
171+ """
172+ import pytest
173+ from my_module import fn
174+ pytest_plugins = 'pytest_github_actions_annotate_failures'
175+
176+ def test_warning():
177+ fn()
178+ """
179+ )
180+ testdir .monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
181+ result = testdir .runpytest_subprocess ()
182+ result .stderr .fnmatch_lines (
183+ # ["::warning file=test_annotation_third_party_warning.py,line=6::beware",]
184+ [
185+ "::warning file=my_module.py,line=4::beware" ,
186+ ]
115187 )
116188
117189
118- def test_annotation_fail_disabled_outside_workflow (testdir ):
190+ def test_annotation_fail_disabled_outside_workflow (testdir : pytest . Testdir ):
119191 testdir .makepyfile (
120192 """
121193 import pytest
@@ -132,7 +204,7 @@ def test_fail():
132204 )
133205
134206
135- def test_annotation_fail_cwd (testdir ):
207+ def test_annotation_fail_cwd (testdir : pytest . Testdir ):
136208 testdir .makepyfile (
137209 """
138210 import pytest
@@ -148,11 +220,13 @@ def test_fail():
148220 testdir .makefile (".ini" , pytest = "[pytest]\n testpaths=.." )
149221 result = testdir .runpytest_subprocess ("--rootdir=foo" )
150222 result .stderr .fnmatch_lines (
151- ["::error file=test_annotation_fail_cwd.py,line=5::test_fail*assert 0*" ,]
223+ [
224+ "::error file=test_annotation_fail_cwd.py,line=5::test_fail*assert 0*" ,
225+ ]
152226 )
153227
154228
155- def test_annotation_fail_runpath (testdir ):
229+ def test_annotation_fail_runpath (testdir : pytest . Testdir ):
156230 testdir .makepyfile (
157231 """
158232 import pytest
@@ -166,11 +240,13 @@ def test_fail():
166240 testdir .monkeypatch .setenv ("PYTEST_RUN_PATH" , "some_path" )
167241 result = testdir .runpytest_subprocess ()
168242 result .stderr .fnmatch_lines (
169- ["::error file=some_path/test_annotation_fail_runpath.py,line=5::test_fail*assert 0*" ,]
243+ [
244+ "::error file=some_path/test_annotation_fail_runpath.py,line=5::test_fail*assert 0*" ,
245+ ]
170246 )
171247
172248
173- def test_annotation_long (testdir ):
249+ def test_annotation_long (testdir : pytest . Testdir ):
174250 testdir .makepyfile (
175251 """
176252 import pytest
@@ -202,7 +278,7 @@ def test_fail():
202278 no_fnmatch_line (result , "::*assert x += 1*" )
203279
204280
205- def test_class_method (testdir ):
281+ def test_class_method (testdir : pytest . Testdir ):
206282 testdir .makepyfile (
207283 """
208284 import pytest
@@ -224,7 +300,7 @@ def test_method(self):
224300 no_fnmatch_line (result , "::*x = 1*" )
225301
226302
227- def test_annotation_param (testdir ):
303+ def test_annotation_param (testdir : pytest . Testdir ):
228304 testdir .makepyfile (
229305 """
230306 import pytest
0 commit comments