From 79e8b02e7a5a3142d9cb3b5d4e88b0b472d427c0 Mon Sep 17 00:00:00 2001 From: Naveen-Pratap <34825619+Naveen-Pratap@users.noreply.github.com> Date: Wed, 14 Jul 2021 19:57:26 +0530 Subject: [PATCH] Update error message for module level skip to include 'allow_module_level' (#8906) Co-authored-by: Naveen Co-authored-by: Bruno Oliveira --- changelog/8432.trivial.rst | 1 + src/_pytest/python.py | 8 ++++---- testing/test_skipping.py | 2 +- 3 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 changelog/8432.trivial.rst diff --git a/changelog/8432.trivial.rst b/changelog/8432.trivial.rst new file mode 100644 index 00000000000..af4c7a22617 --- /dev/null +++ b/changelog/8432.trivial.rst @@ -0,0 +1 @@ +Improve error message when :func:`pytest.skip` is used at module level without passing `allow_module_level=True`. diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 79dfb7320a8..8e8d2f38d83 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -608,10 +608,10 @@ def _importtestmodule(self): if e.allow_module_level: raise raise self.CollectError( - "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}." + "Using pytest.skip outside of a test will skip the entire module. " + "If that's your intention, pass `allow_module_level=True`. " + "If you want to skip a specific test or an entire class, " + "use the @pytest.mark.skip or @pytest.mark.skipif decorators." ) from e self.config.pluginmanager.consider_module(mod) return mod diff --git a/testing/test_skipping.py b/testing/test_skipping.py index 53bf953a823..d50a16c9078 100644 --- a/testing/test_skipping.py +++ b/testing/test_skipping.py @@ -1341,7 +1341,7 @@ def test_func(): ) result = pytester.runpytest() result.stdout.fnmatch_lines( - ["*Using pytest.skip outside of a test is not allowed*"] + ["*Using pytest.skip outside of a test will skip the entire module*"] )