mardi 1 septembre 2015

Skip unwanted packages importing during unit testing in Python

I have medium-sized project written in Python. The project structure looks like this:

foobar
    __init__.py  # app initialization code, including some func calls
    ...
    modABC
        __init__.py  # a little of code again...
        ...
        submodXYZ
            __init__.py  # ... and so on
            target.py
            ...
tests
    __init__.py
    fobar
        __init__.py  
        modABC
            __init__.py
            submodXYZ
                __init__.py
                test_target.py

And I want to test functionality from the target.py module.

# test_target.py
import unittest
from foobar.modABC.submodXYZ import TargetClass
...

Some of my __init__.py files contain (an application, or a subsystem) initialization code. And that code is not required to test plain python classes located in target.py. However, when I import classes from target.py in test_target.py this code from all the __init__.py files up to the top of the project hierarchy is executed. How can I avoid such behavior? Or maybe I should somehow restructurize my application?

Aucun commentaire:

Enregistrer un commentaire