Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I do have some more complex projects where if you run pytest --collect-only you will endup with lots of import failures caused by discovered test files that have imports of stuff that were not installed yet.

I want to change these test files in such way that they would not fail on collection.

This is because the user may want to run tests using a specific pattern like pytest -k foo, but he would not be able to do this if collection fails on unrelated tests.

I know that I can define a pytest_configure method that would be called durind collection but if I move the imports into it I would still get failures later when the interpreter is reaching code trying to use the missing imports.

def pytest_configure(config):

    try:

        import foo

    except:

        pytest.skip("skipping when foo is not installed")

def test_foo()

  assert foo.is_enabled()  # <-- foo is undefined here

My example is clearly over similified as we all know that I could add the import again in the test method but I do not want to do this in tens of methods. I am looking for a cleaner solution.

1 Answer

0 votes
by (25.1k points)

You can use the use the --continue-on-collection-errors flag with pytest command

pytest --continue-on-collection-errors -v

Browse Categories

...