Eric O. Lebigot
Eric O. Lebigot

Reputation: 94595

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?

Upvotes: 2

Views: 2145

Answers (4)

sjbrown
sjbrown

Reputation: 570

I think the nose-exclude plugin may help. If you install this plugin and then run:

    nosetests --exclude-dir B

It may work for you. I have the same problem and have achieved some passable results. Now my next issue is that the exclude-dir doesn't seem to be a usable option when creating a nosetests config file.

Upvotes: 0

dbn
dbn

Reputation: 13940

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

Upvotes: 1

Michael Kent
Michael Kent

Reputation: 1734

Sure, just use the --match and the --exclude command line options to limit what nose will discover as a test program.

Upvotes: 2

ThiefMaster
ThiefMaster

Reputation: 318658

Simply wrap the import with a try:..except ImportError:... block. In that case you could even set a variable letting you know if numpy is available or not.

Upvotes: 4

Related Questions