Elffuss BraidHub

kikocis/auto-code-rover public

main · 1.9 KiBRawPermalink
1Improve handling of skip for module level
2This is potentially about updating docs, updating error messages or introducing a new API.
3
4Consider the following scenario:
5
6`pos_only.py` is using Python 3,8 syntax:
7```python
8def foo(a, /, b):
9 return a + b
10```
11
12It should not be tested under Python 3.6 and 3.7.
13This is a proper way to skip the test in Python older than 3.8:
14```python
15from pytest import raises, skip
16import sys
17if sys.version_info < (3, 8):
18 skip(msg="Requires Python >= 3.8", allow_module_level=True)
19
20# import must be after the module level skip:
21from pos_only import *
22
23def test_foo():
24 assert foo(10, 20) == 30
25 assert foo(10, b=20) == 30
26 with raises(TypeError):
27 assert foo(a=10, b=20)
28```
29
30My actual test involves parameterize and a 3.8 only class, so skipping the test itself is not sufficient because the 3.8 class was used in the parameterization.
31
32A naive user will try to initially skip the module like:
33
34```python
35if sys.version_info < (3, 8):
36 skip(msg="Requires Python >= 3.8")
37```
38This issues this error:
39
40>Using pytest.skip outside of a test is not allowed. To decorate a test function, use the @pytest.mark.skip or @pytest.mark.skipif decorators instead, and to skip a module use `pytestmark = pytest.mark.{skip,skipif}.
41
42The proposed solution `pytestmark = pytest.mark.{skip,skipif}`, does not work in my case: pytest continues to process the file and fail when it hits the 3.8 syntax (when running with an older version of Python).
43
44The correct solution, to use skip as a function is actively discouraged by the error message.
45
46This area feels a bit unpolished.
47A few ideas to improve:
48
491. Explain skip with `allow_module_level` in the error message. this seems in conflict with the spirit of the message.
502. Create an alternative API to skip a module to make things easier: `skip_module("reason")`, which can call `_skip(msg=msg, allow_module_level=True)`.
51
52