| 1 | Improve handling of skip for module level |
| 2 | This is potentially about updating docs, updating error messages or introducing a new API. |
| 3 | |
| 4 | Consider the following scenario: |
| 5 | |
| 6 | `pos_only.py` is using Python 3,8 syntax: |
| 7 | ```python |
| 8 | def foo(a, /, b): |
| 9 | return a + b |
| 10 | ``` |
| 11 | |
| 12 | It should not be tested under Python 3.6 and 3.7. |
| 13 | This is a proper way to skip the test in Python older than 3.8: |
| 14 | ```python |
| 15 | from pytest import raises, skip |
| 16 | import sys |
| 17 | if 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: |
| 21 | from pos_only import * |
| 22 | |
| 23 | def 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 | |
| 30 | My 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 | |
| 32 | A naive user will try to initially skip the module like: |
| 33 | |
| 34 | ```python |
| 35 | if sys.version_info < (3, 8): |
| 36 | skip(msg="Requires Python >= 3.8") |
| 37 | ``` |
| 38 | This 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 | |
| 42 | The 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 | |
| 44 | The correct solution, to use skip as a function is actively discouraged by the error message. |
| 45 | |
| 46 | This area feels a bit unpolished. |
| 47 | A few ideas to improve: |
| 48 | |
| 49 | 1. Explain skip with `allow_module_level` in the error message. this seems in conflict with the spirit of the message. |
| 50 | 2. 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 | |