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.

fixture assert is getting overwritten in pytest_collection_modifyitems

See original GitHub issue

Please create 2 files in any folder with below codes:

  1. 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}")
  1. 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

Downloads.zip

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
RonnyPfannschmidtcommented, Sep 25, 2022

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

1reaction
JosiasAurelcommented, Sep 9, 2022

Hey, I just tested your code and it works for me. Here is what I have In test_py_issue.py

import pytest

def idfn(fixture_value):
    return 0

def create_test():
    import numpy as np # 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}")

In 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

Screenshot from 2022-09-09 20-39-50

Read more comments on GitHub >

github_iconTop 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 >

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