Skip to content

Commit d45e39f

Browse files
authored
Raise TypeError on bare test decorator usage and add regression tests (#3649)
* Raise TypeError on bare @skip_if_windows, @requires_crt, @skip_if_crt usage and add regression tests * Rename test_test_decorators.py to test_decorators.py * Add license to the new file tset_decorators.py
1 parent 6552428 commit d45e39f

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

tests/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ def skip_if_windows(reason):
7272
def test_some_non_windows_stuff(self):
7373
self.assertEqual(...)
7474
"""
75+
if callable(reason):
76+
raise TypeError(
77+
"Use @skip_if_windows('reason') with parentheses, "
78+
"not bare @skip_if_windows"
79+
)
7580

7681
def decorator(func):
7782
return unittest.skipIf(
@@ -82,6 +87,10 @@ def decorator(func):
8287

8388

8489
def requires_crt(reason=None):
90+
if callable(reason):
91+
raise TypeError(
92+
"Use @requires_crt() with parentheses, not bare @requires_crt"
93+
)
8594
if reason is None:
8695
reason = "Test requires awscrt to be installed"
8796

@@ -92,6 +101,10 @@ def decorator(func):
92101

93102

94103
def skip_if_crt(reason=None):
104+
if callable(reason):
105+
raise TypeError(
106+
"Use @skip_if_crt() with parentheses, not bare @skip_if_crt"
107+
)
95108
if reason is None:
96109
reason = "Test requires awscrt to NOT be installed"
97110

tests/unit/test_decorators.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License").
4+
# You may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from unittest import mock
15+
16+
import pytest
17+
18+
from tests import requires_crt, skip_if_crt, skip_if_windows
19+
20+
21+
class TestSkipIfWindows:
22+
def test_bare_skip_if_windows_fails_immediately(self):
23+
with pytest.raises(TypeError):
24+
25+
@skip_if_windows
26+
def my_test():
27+
pass
28+
29+
def test_skip_if_windows_skips_on_windows(self):
30+
with mock.patch('tests.platform') as mock_platform:
31+
mock_platform.system.return_value = 'Windows'
32+
33+
@skip_if_windows('Not supported on Windows')
34+
def my_test():
35+
assert False
36+
37+
assert getattr(my_test, '__unittest_skip__', False) is True
38+
39+
def test_skip_if_windows_runs_on_non_windows(self):
40+
with mock.patch('tests.platform') as mock_platform:
41+
mock_platform.system.return_value = 'Linux'
42+
43+
@skip_if_windows('Not supported on Windows')
44+
def my_test():
45+
pass
46+
47+
assert getattr(my_test, '__unittest_skip__', False) is False
48+
49+
50+
class TestRequiresCrt:
51+
def test_bare_requires_crt_fails_immediately(self):
52+
with pytest.raises(TypeError):
53+
54+
@requires_crt
55+
def my_test():
56+
pass
57+
58+
def test_requires_crt_skips_when_no_crt(self):
59+
with mock.patch('tests.HAS_CRT', False):
60+
61+
@requires_crt()
62+
def my_test():
63+
assert False
64+
65+
assert getattr(my_test, '__unittest_skip__', False) is True
66+
67+
def test_requires_crt_runs_when_crt_available(self):
68+
with mock.patch('tests.HAS_CRT', True):
69+
70+
@requires_crt()
71+
def my_test():
72+
pass
73+
74+
assert getattr(my_test, '__unittest_skip__', False) is False
75+
76+
77+
class TestSkipIfCrt:
78+
def test_bare_skip_if_crt_fails_immediately(self):
79+
with pytest.raises(TypeError):
80+
81+
@skip_if_crt
82+
def my_test():
83+
pass
84+
85+
def test_skip_if_crt_skips_when_crt_available(self):
86+
with mock.patch('tests.HAS_CRT', True):
87+
88+
@skip_if_crt()
89+
def my_test():
90+
assert False
91+
92+
assert getattr(my_test, '__unittest_skip__', False) is True
93+
94+
def test_skip_if_crt_runs_when_no_crt(self):
95+
with mock.patch('tests.HAS_CRT', False):
96+
97+
@skip_if_crt()
98+
def my_test():
99+
pass
100+
101+
assert getattr(my_test, '__unittest_skip__', False) is False

0 commit comments

Comments
 (0)