Skip to content

importorskip usage with "from" imports? #12894

Answered by The-Compiler
wimglenn asked this question in Ideas
Discussion options

You must be logged in to vote

You could do:

import importlib.metadata
import pytest

@pytest.mark.skipif(not hasattr(importlib.metadata, "packages_distributions"), reason="Needs packages_distributions")
def test_foo():
    ... = importlib.metadata.packages_distributions()

and possibly even move that into a small helper if you want to have a nice reason without the duplication. That would have the additional benefit that the test gets skipped at collection time without having to first run it (and possibly set up fixtures) before finding out it's actually skipped.

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by wimglenn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants