question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

importorskip decorator on one test skips entire module

See original GitHub issue

Bud Description

When using pytest.importorskip as a decorator on a specific test in a test file, the all of the the tests in the file, rather than just the decorated test, are skipped.

For example:

# contents of test_it,py
import pytest

@pytest.importorskip('a_module_that_isnt_installed')
def tests_one():
    assert 1

def test_two():
   assert 0

when running:

pytest test_it.py -rsx

I get the following output:

============================= test session starts ==============================
platform linux -- Python 3.10.2, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/derrick/python_scratch_space/importorskip
collected 0 items / 1 skipped                                                  

=========================== short test summary info ============================
SKIPPED [1] test_it.py:3: could not import 'a_module_that_isnt_installed': No module named 'a_module_that_isnt_installed'
============================== 1 skipped in 0.00s ==============================

However, I would expect test_two to still be collected and run.

Version Info

Package    Version
---------- -------
attrs      21.4.0
iniconfig  1.1.1
packaging  21.3
pip        21.3.1
pluggy     1.0.0
py         1.11.0
pyparsing  3.0.7
pytest     6.2.5
setuptools 60.5.0
toml       0.10.2
wheel      0.37.1

pytest 6.2.5, python 10.2 ubuntu 20.04

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
RonnyPfannschmidtcommented, Jan 26, 2022

personally i’d prefer just deprecating a easy to misuse api instead of hacking it up with magic to somehow hopefully be able to error

0reactions
nicoddemuscommented, Jan 26, 2022

Also, how would you feel about a built in importorskip mark for just this type of use?

Not sure, perhaps it would bring more confusion than help out? We see users confusing pytest.mark.skip and pytest.skip already… 🤔

Read more comments on GitHub >

github_iconTop Results From Across the Web

Pytest.importorskip test skipping
Skipping module imports is demonstrated in this example. Suppose “test_plots.py” has every function requiring Matplotlib. The whole file may be ...
Read more >
How to use skip and xfail to deal with tests that cannot succeed
You can skip tests on a missing import by using pytest.importorskip at module level, within a test, or test setup function.
Read more >
PyTest: skip entire module/file (python 2 and 3) - Stack Overflow
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...
Read more >
pytest.importorskip - pytest Quick Start Guide [Book] - O'Reilly
pytest.importorskip will import the module and return the module object, or skip the test entirely if the module could not be .
Read more >
Testing — Translate Toolkit 3.7.4 documentation
You can apply this decorator to classes as well as functions and methods. It is also possible to skip an entire test module...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found