fixture assert is getting overwritten in pytest_collection_modifyitems
See original GitHub issuePlease create 2 files in any folder with below codes:
- test_py_issue.py
import pytest
def idfn(fixture_value):
return 0
def create_test():
import somerandomPACKAGE # inserting error in fixture
return ["a", "b", "c"]
@pytest.fixture(scope="function", params=create_test(), ids=idfn)
def op_test_config(request):
return request.param
def test_py_collect_issue(op_test_config):
print(f"\nHello I am a debug test! and config: {op_test_config}")
- conftest.py
def pytest_collection_modifyitems(config, items):
if len(items) !=3:
print(len(items))
assert 0, "expected 3 tests to be created!" # commenting this line will give actual error
Here the output is
INTERNALERROR> assert 0, "expected 3 tests to be created!" # commenting this line will give actual error
INTERNALERROR> AssertionError: expected 3 tests to be created!
INTERNALERROR> assert 0`
But the issue raised in fixture , which is import issue. so pytest should print first raised issue.
`Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.8/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
test_py_issue.py:11: in <module>
???
test_py_issue.py:7: in create_test
import somerandomPACKAGE # inserting error in fixture
E ModuleNotFoundError: No module named 'somerandomPACKAGE'`
pip list:
Pillow 9.2.0
pip 20.0.2
platformdirs 2.5.2
pluggy 0.13.1
psutil 5.4.2
py 1.11.0
pyasn1 0.4.8
pycodestyle 2.9.1
pyflakes 2.5.0
pylint 1.9.3
pymongo 3.7.2
pyparsing 3.0.9
pytest 6.2.2
pytest-forked 1.3.0
pytest-html 3.1.1
pytest-logger 0.5.1
pytest-metadata 2.0.2
pytest-random-order 1.0.4
pytest-repeat 0.9.1
pytest-timeout 2.0.2
pytest-xdist 2.4.0
python-dateutil 2.8.2
Issue Analytics
- State:
- Created a year ago
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Conflicting fixtures · Issue #7053 · pytest-dev/pytest - GitHub
If you have a common setup with only some variables being changed, and all the tests are the same, then fixture parameterization is...
Read more >API Reference — pytest documentation
This function will make sure that this module or all modules inside the package will get their assert statements rewritten. Thus you should...
Read more >Using a command-line option in a pytest skip-if condition
I take in the command line option and set various API urls (from a config.ini file) in a fixture in conftest.py. @pytest.fixture(scope='session' ...
Read more >pytest Documentation - Read the Docs
How to parametrize fixtures and test functions . ... Use the raises helper to assert that some code raises an exception:.
Read more >pytest Documentation
Scope: sharing a fixture instance across tests in a class, module or session . ... Note: You can use the assert statement to...
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

The error is stored as a report to be printed in the summary, your assertion in the place where you should not asset breaks pytest before it gets to the printing
Hey, I just tested your code and it works for me. Here is what I have In
test_py_issue.pyIn
conftest.py