Have you ever encountered a scenario where you were testing a Python app in a new environment, only to find that there was a missing package that you couldn’t install without help from IT? Recently, I ran into this issue myself and wanted to share my experience.
When faced with a missing package that can only be installed by IT, you may still want to migrate and run pytest on ported code. However, you may find that pytest fails during the collections stage, preventing you from proceeding with your testing. This is certainly not ideal, but fortunately, there’s an easy solution to this problem.
To avoid aborting test collection on import errors (or any other errors), simply use the “–continue-on-collection-errors” flag. This flag tells pytest to continue running even if it encounters errors during the collection phase.
Additionally, you can skip tests on a missing import by using “pytest.importorskip” at the module level, within a test, or test setup function. For example, you can skip a test that relies on a missing “docutils” package with the following line of code:
docutils = pytest.importorskip("docutils")
This allows you to skip tests that depend on a package that cannot be installed or imported, without affecting the rest of your test suite.
In conclusion, encountering a missing package during testing can be frustrating, but it doesn’t have to derail your progress. By using the right flags and skipping tests as necessary, you can continue testing your Python app in a new environment even if there are missing dependencies.