Pytest should't import conftest if __init__ raises Skipped
See original GitHub issueI have a package that needs to support Python 3 and 2 code. To accommodate this I’ve created a test directory specifically aimed at testing my Python 3 code. Within that directory I have an __init__.py
with the following contents:
import sys
import pytest
if sys.version_info < (3, 0):
pytest.skip("skipping Python 3 only tests", allow_module_level=True)
If I place a conftest.py
file inside the directory containing this __init__.py
and running pytest using Python 2 I get the following output:
==================================== test session starts =====================================
platform darwin -- Python 2.7.15, pytest-4.6.9, py-1.8.1, pluggy-0.13.1
plugins: env-info-0.3.0, mock-2.0.0, cov-2.8.1
collected 0 items / 1 skipped
================================== short test summary info ===================================
SKIPPED [1] xxxxxxxxxxxxxxxxx.py: skipping Python 3 only tests
-- Docs: https://docs.pytest.org/en/latest/warnings.html
=========================== 1 skipped, 1 warnings in 0.12 seconds ============================
ERROR: InvocationError for command xxx/bin/python -m pytest tests (exited with code 5)
I’m not really sure what’s gone wrong, but this only occurs if I have a conftest.py
within the directory I’m trying to skip, removing the conftest.py
allows things to work again. If I use an explicit --ignore test_my_py_3_stuff
instead of the programatic skipping, everything seems to work even if I have a conftest.py
file.
Issue Analytics
- State:
- Created 3 years ago
- Comments:15 (7 by maintainers)
Top Results From Across the Web
Basic patterns and examples — pytest documentation
content of conftest.py import pytest def type_checker(value): msg = "cmdopt must specify a numeric type as typeNNN" if not value.startswith("type"): raise ...
Read more >conftest.py - pytest fixtures for testing
conftest.py - pytest fixtures for testing. Imports. Standard library ... action="store_true", help="Skip initialization of the test database.
Read more >How to skip a pytest using an external fixture? - Stack Overflow
The Solution from Bruno Oliveira is working, but for new pytest (>= 3.5.0) you need to add the pytest_configure: # conftest.py import pytest...
Read more >pytest Documentation - Read the Docs
Use the raises helper to assert that some code raises an exception: # content of test_sysexit.py import pytest def f(): raise SystemExit(1).
Read more >2009-March.txt - Python mailing list
__.test.outcome import Skipped class TestRaises: def test_raises(self): @@ -59 ... path): """ return a list of imported conftest modules for the given path....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
To be honest, EoL for Python 2 was announced over a decade ago, and we’re already passed it. There’s been almost 12 years worth of moments to implement backwards compatibility. That should really be a top priority at this point.
@rmorshea https://doc.pytest.org/en/latest/skipping.html#skipping-test-functions <- allow_module_level might help, i believe you would still need a conftest with collect_ignore in some way to make sure the files are really skipped