Skip to content

CollectError instead of ImportError pytest 3.0.x #2010

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

Closed
sashkab opened this issue Oct 18, 2016 · 4 comments
Closed

CollectError instead of ImportError pytest 3.0.x #2010

sashkab opened this issue Oct 18, 2016 · 4 comments

Comments

@sashkab
Copy link

sashkab commented Oct 18, 2016

I have multi-platform package, which has subset of modules which are required to run on one platform and they can be skipped on another platform. I've come up with the code which worked perfectly fine when I used 2.9.2:

import pytest

try:
    from module import a, b, c
except ImportError:
    pytest.skip('Requires module.')

I would get an error that module is required and tests will be skipped. Now, in pytest 3.0.3 I can't use this, because #1520 made ImportError no longer work and this code becomes

import pytest
from _pytest.main import Collector

try:
    from module import a, b, c
except (ImportError, Collector.CollectError):
    pytestmark = pytest.mark.skip('Requires module.')

This is import error, and I have everything to handle it.

What I propose:

Raise ImportError instead of CollectError, or simplify use of the CollectError by importing it into pytest (i.e. pytest.CollectError).

NB: I know I can use pytest.importorskip('module'), but it doesn't work from pytest.importorskip('module') import a, b, c. Another reason why I don't use importorskip -- I can't provide reason.

@nicoddemus
Copy link
Member

Hi @sashkab,

Thanks for the feedback!

What I propose:
Raise ImportError instead of CollectError

You mean pytest.skip raise an ImportError? That's not really possible because that's how pytest knows a pytest.skip was used inside a test.

or simplify use of the CollectError by importing it into pytest (i.e. pytest.CollectError).

That's certainly a possibility, although I'm not sure why you need it in the code snipped you posted:

import pytest
from _pytest.main import Collector

try:
    from module import a, b, c
except (ImportError, Collector.CollectError):
    pytestmark = pytest.mark.skip('Requires module.')

Why not only catching ImportError? Or is it possible for module to also raise CollectError?

@RonnyPfannschmidt
Copy link
Member

i suppose we should just add a reason argument to importorskip

then it can be called before the normal imports just fine

@sashkab
Copy link
Author

sashkab commented Oct 18, 2016

Why not only catching ImportError?

It seems pytest catched ImportError and then raises CollectError exception during collection.
I'd like to continue catchingImportError, but can't.

You mean pytest.skip raise an ImportError?

No, I meant test collector should not raise CollectError, but push down the line original error message, in my case it is ImportError.

@nicoddemus
Copy link
Member

It seems pytest catched ImportError and then raises CollectError exception during collection.
I'd like to continue catchingImportError, but can't.

This seems really unlikely... unless some of the modules in there are executing pytest.skip(...) themselves? That would explain it.

We are hoping to introduce a flag to allow pytest.skip to work again at module level in #2805 which I think would help you here and allow you to revert back to the old code.

I'm closing this for now, feel free to follow up with more questions if you have any.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants