0

How can I force nosetests to run each unit test in a new Python instance?

I'm having a weird situation here, I have a simple plugin system based on enumerating __subclasses__() of a Plugin base class out of which plugins are derived. My problem arises from the fact that nosetests seems to reuse (?) the python instance to run all unit tests and, because of that, one of the unit tests ends up "seeing" a plugin defined in another.

For a proof of concept, I have the following directory structure:

.
|-- package
|   |-- __init__.py
|   `-- plugin.py
|-- unit_test_1.py
`-- unit_test_2.py

The contents of the files can be found here. Now, if I run unit_test_1.py or unit_test_2.py individually, they both succeed. However, if I use nosetests to run both of them, the second unit test will failed due to the length of __subclasses__() being two.

Unknown
  • 5,722
  • 5
  • 43
  • 64

1 Answers1

0

You should be using nosetests --with-isolation:

Enable plugin IsolationPlugin: Activate the isolation plugin to isolate changes to external modules to a single test module or package. The isolation plugin resets the contents of sys.modules after each test module or package runs to its state before the test. PLEASE NOTE that this plugin should not be used with the coverage plugin, or in any other case where module reloading may produce undesirable side-effects.

Your example will pass both tests.

Oleksiy
  • 6,337
  • 5
  • 41
  • 58
  • Uh. I tried --with-isolation before asking the question and I got tons of exceptions; and yeah, I'm using coverage too :( Any other way of doing this? (I'm gonna look tomorrow into --with-isolation some more, maybe I can make it work) – Unknown Feb 09 '14 at 09:40
  • it's going to be hard to get coverage and using multiple instances at the same time - you would have to deal with a merge of multiple runs. – Oleksiy Feb 10 '14 at 04:44