Search code examples
pythonpackagenosenosetests

How to prevent nose from importing __init__.py files?


Can the nose testing framework be instructed to only run tests in test_*.py files?

In fact, doing nosetests A with the following directory structure:

A/
  test_A.py
  B/
    __init__.py

imports B, which I want to avoid.

The reason for this is that the B module starts with import numpy because it is only meant to be used when the user has the optional NumPy module installed. However, users who did not install NumPy do not want nosetests to process B/__init__.py, because it necessarily fails on import numpy even though NumPy is optional. How can this be achieved?


Solution

  • Nose can accept/reject tests at the directory, file, module, class and method levels. You need to reject the B directory. There is no way to ignore the __init__.py file; Python never sees it. It shows up when B is imported, so you need to ignore B.

    Try:

    nosetests --exclude=B