Вопрос

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.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top