dimanche 24 janvier 2016

Python unittest failing to resolve import statements

I have a file structure that looks like the following

project
    src
        __init__.py
        main.py
        module.py
        secondary.py
    test
        test_module.py        

where module.py is

import secondary
x = False

where secondary.py is

pass

and test_module.py is

from unittest import TestCase

from src import module

 class ModuleTest(TestCase):
    def test_module(self):
        self.assertTrue(module.x)

Invoking python3 -m unittest discover in /project/

gives an error

 File "/Users/Me/Code/project/test/test_module.py", line 6, in <module>
  from src import module
 File "/Users/Me/Code/project/src/module.py", line 1, in <module>
  import secondary
ImportError: No module named 'secondary'

What can I do so that secondary.py is imported without error?

Aucun commentaire:

Enregistrer un commentaire