Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest began spuriously rejecting pytest.skip in modules in 3.x #2338

Closed
4 tasks done
riastradh-probcomp opened this issue Mar 29, 2017 · 6 comments
Closed
4 tasks done
Labels
topic: selection related to test selection from the command line type: enhancement new feature or API change, should be merged into features branch

Comments

@riastradh-probcomp
Copy link

In pytest 2.x, I used pytest.skip('...') to skip entire modules of tests that are not enabled by a command-line flag --integration, which have additional import dependencies and take longer to load and run.

This useful functionality was broken in pytest 3.x with commit d81f230, which makes pytest.skip simply crash in this case.

I can't use pytestmark = pytest.mark.skipif(...) because the ensuing code contains imports that must be skipped as well or else pytest will fail trying to load the module -- in other words, I really do want to abort the rest of the module.

How do I restore this useful functionality?

Example. In conftest.py:

def pytest_addoption(parser):
    parser.addoption('--integration', action='store_true',
        help='run integration tests')

In test_foo.py:

import pytest

if not pytest.config.getoption('--integration'):
    pytest.skip('specify --integration to run integration tests')

import nonexistent

def test_nonexistent():
    assert nonexistent.wotsit() == nonexistent.warbleglobber

Output:

% python -m pytest
============================= test session starts ==============================
platform linux2 -- Python 2.7.6, pytest-3.0.7, py-1.4.33, pluggy-0.4.0
rootdir: /tmp/user/1000/20170329/venv/test, inifile:
collected 0 items / 1 errors 

==================================== ERRORS ====================================
_________________________ ERROR collecting test_foo.py _________________________
Using pytest.skip outside of a test is not allowed. If you are trying to decorate a test function, use the @pytest.mark.skip or @pytest.mark.skipif decorators instead.
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.09 seconds ============================
% pip freeze
argparse==1.2.1
py==1.4.33
pytest==3.0.7
wsgiref==0.1.2
% uname -a
Linux ubuntu 3.13.0-66-generic #108-Ubuntu SMP Wed Oct 7 15:20:27 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

Thanks for submitting an issue!

Here's a quick checklist in what to include:

  • Include a detailed description of the bug or suggestion
  • pip list of the virtual environment you are using
  • pytest and operating system versions
  • Minimal example if possible
riastradh-probcomp added a commit to probcomp/cgpm that referenced this issue Mar 29, 2017
- pytest 3.x broke the mechanism we used to implement them:
  pytest-dev/pytest#2338

- The `--integration' command-line argument doesn't seem to work with
  `python -m pytest --pyargs cgpm'.  (This problem is fixable.)
@RonnyPfannschmidt
Copy link
Member

we made this change after people again and again would use pytest.skip instead of the pytest.mark.skip mark to mark test functions thus skipping whole test modules instead of just a function

however the use-case you present is also valid and i wonder if there is a reasonable way to compromise

@The-Compiler
Copy link
Member

The Skipped exception already has allow_module_level=True as argument which is used by pytest.importorskip internally - so you should already be able to do raise pytest.skip.Exception("...", allow_module_level=True).

I think we should just expose that via pytest.skip(...) too.

@RonnyPfannschmidt
Copy link
Member

seems like this is a 2 line change + a 30 line test

@RonnyPfannschmidt RonnyPfannschmidt self-assigned this Mar 30, 2017
@riastradh-probcomp
Copy link
Author

It would be nice if there were some way to skip the tests that works both in 2.x and 3.x, because I would like to ship our software to users who can run py.test --pyargs xyz to run the tests once installed and who are not necessarily experienced Python developers working in virtualenvs with bleeding-edge versions of everything. So requiring an extra argument allow_module_level=True to pytest.skip won't help for that purpose.

@nicoddemus
Copy link
Member

I see, but we don't plan to release a 2.10 release with that support, sorry. You can follow up the discussion that lead to this decision in #607.

As a solution, you can easily provide this function for your users to use:

import pytest

if pytest.__version__ > '3':
    def skip_module(reason):
        raise pytest.skip.Exception(reason, allow_module_level=True)        
else:
    def skip_module(reason):
        pytest.skip(reason=reason)

And then use that instead of the previous pytest.skip(reason='reasons') at the module level.

@riastradh-probcomp would you like to contribute the allow_module_level change? As @RonnyPfannschmidt said should be simple, and it would ship with pytest 3.1 which we plan to release soon.

@RonnyPfannschmidt RonnyPfannschmidt removed their assignment Mar 30, 2017
@nicoddemus nicoddemus added topic: selection related to test selection from the command line type: enhancement new feature or API change, should be merged into features branch labels Sep 28, 2017
@nicoddemus
Copy link
Member

Closing in favor of #2805

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: selection related to test selection from the command line type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

No branches or pull requests

4 participants