55import pytest
66from packaging import version
77
8+ PYTEST_VERSION = version .parse (pytest .__version__ )
89pytest_plugins = "pytester"
910
1011
1112# result.stderr.no_fnmatch_line() is added to testdir on pytest 5.3.0
1213# https://docs.pytest.org/en/stable/changelog.html#pytest-5-3-0-2019-11-19
1314def no_fnmatch_line (result , pattern ):
14- if version .parse (pytest .__version__ ) >= version .parse ("5.3.0" ):
15- result .stderr .no_fnmatch_line (pattern + "*" ,)
15+ if version .parse ("5.3.0" ) <= PYTEST_VERSION :
16+ result .stderr .no_fnmatch_line (
17+ pattern + "*" ,
18+ )
1619 else :
1720 assert pattern not in result .stderr .str ()
1821
@@ -68,7 +71,9 @@ def test_fail():
6871 testdir .monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
6972 result = testdir .runpytest_subprocess ()
7073 result .stderr .fnmatch_lines (
71- ["::error file=test_annotation_fail.py,line=5::test_fail*assert 0*" ,]
74+ [
75+ "::error file=test_annotation_fail.py,line=5::test_fail*assert 0*" ,
76+ ]
7277 )
7378
7479
@@ -86,8 +91,56 @@ def test_fail():
8691 testdir .monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
8792 result = testdir .runpytest_subprocess ()
8893 result .stderr .fnmatch_lines (
89- ["::error file=test_annotation_exception.py,line=5::test_fail*oops*" ,]
94+ [
95+ "::error file=test_annotation_exception.py,line=5::test_fail*oops*" ,
96+ ]
97+ )
98+
99+
100+ @pytest .mark .skipif (
101+ version .parse ("6.0.0" ) > PYTEST_VERSION ,
102+ reason = "requires pytest 6.0.0" ,
103+ )
104+ def test_annotation_warning (testdir ):
105+ testdir .makepyfile (
106+ """
107+ import warnings
108+ import pytest
109+ pytest_plugins = 'pytest_github_actions_annotate_failures'
110+
111+ def test_warning():
112+ warnings.warn('beware', Warning)
113+ assert 1
114+ """
90115 )
116+ testdir .monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
117+ result = testdir .runpytest_subprocess ()
118+ result .stderr .fnmatch_lines (
119+ [
120+ "::warning file=test_annotation_warning.py,line=6::beware" ,
121+ ]
122+ )
123+
124+
125+ @pytest .mark .skipif (
126+ version .parse ("6.0.0" ) > PYTEST_VERSION ,
127+ reason = "requires pytest 6.0.0" ,
128+ )
129+ def test_annotation_exclude_warnings (testdir ):
130+ testdir .makepyfile (
131+ """
132+ import warnings
133+ import pytest
134+ pytest_plugins = 'pytest_github_actions_annotate_failures'
135+
136+ def test_warning():
137+ warnings.warn('beware', Warning)
138+ assert 1
139+ """
140+ )
141+ testdir .monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
142+ result = testdir .runpytest_subprocess ("--exclude-warnings" )
143+ assert not result .stderr .lines
91144
92145
93146def test_annotation_third_party_exception (testdir ):
@@ -111,7 +164,43 @@ def test_fail():
111164 testdir .monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
112165 result = testdir .runpytest_subprocess ()
113166 result .stderr .fnmatch_lines (
114- ["::error file=test_annotation_third_party_exception.py,line=6::test_fail*oops*" ,]
167+ [
168+ "::error file=test_annotation_third_party_exception.py,line=6::test_fail*oops*" ,
169+ ]
170+ )
171+
172+
173+ @pytest .mark .skipif (
174+ version .parse ("6.0.0" ) > PYTEST_VERSION ,
175+ reason = "requires pytest 6.0.0" ,
176+ )
177+ def test_annotation_third_party_warning (testdir ):
178+ testdir .makepyfile (
179+ my_module = """
180+ import warnings
181+
182+ def fn():
183+ warnings.warn('beware', Warning)
184+ """
185+ )
186+
187+ testdir .makepyfile (
188+ """
189+ import pytest
190+ from my_module import fn
191+ pytest_plugins = 'pytest_github_actions_annotate_failures'
192+
193+ def test_warning():
194+ fn()
195+ """
196+ )
197+ testdir .monkeypatch .setenv ("GITHUB_ACTIONS" , "true" )
198+ result = testdir .runpytest_subprocess ()
199+ result .stderr .fnmatch_lines (
200+ # ["::warning file=test_annotation_third_party_warning.py,line=6::beware",]
201+ [
202+ "::warning file=my_module.py,line=4::beware" ,
203+ ]
115204 )
116205
117206
@@ -148,7 +237,9 @@ def test_fail():
148237 testdir .makefile (".ini" , pytest = "[pytest]\n testpaths=.." )
149238 result = testdir .runpytest_subprocess ("--rootdir=foo" )
150239 result .stderr .fnmatch_lines (
151- ["::error file=test_annotation_fail_cwd.py,line=5::test_fail*assert 0*" ,]
240+ [
241+ "::error file=test_annotation_fail_cwd.py,line=5::test_fail*assert 0*" ,
242+ ]
152243 )
153244
154245
@@ -166,7 +257,9 @@ def test_fail():
166257 testdir .monkeypatch .setenv ("PYTEST_RUN_PATH" , "some_path" )
167258 result = testdir .runpytest_subprocess ()
168259 result .stderr .fnmatch_lines (
169- ["::error file=some_path/test_annotation_fail_runpath.py,line=5::test_fail*assert 0*" ,]
260+ [
261+ "::error file=some_path/test_annotation_fail_runpath.py,line=5::test_fail*assert 0*" ,
262+ ]
170263 )
171264
172265
0 commit comments