vendredi 31 juillet 2015

How do I import across submodules

We've got a test framework using nose and factory_boy up and going.

As recommended, we've put a /test directory inside of each module directory and that's where we put a factories.py file and the various test cases.

The challenge I've got is trying to import factories and functions between these test submodules. Full code branch is here:

http://ift.tt/1VRh0Fr

In case I haven't relayed enough detail to be useful.

Detailed problem

The summary of the relevant structure is:

/gbe
    __init__.py
    tests.py
    (many more files)
    /tests
         __init__.py
         factories.py
         functions.py
         (many files of tests, all tests_*)
/ticketing
    __init__.py
    tests.py
    /tests
         __init__.py
         factories.py
         test_edit_event.py

It's in that last folder that problem is happening.

I can't seem to import anything from gbe.tests and I want to reuse the mocks provided in factories.py and the functions in functions.py

Whatever I try, consistently results in the form of either:

Traceback (most recent call last):
File "/Users/bethlakshmi/Documents/mysite/GBE2/expo/ticketing/test/test_edit_event.py", line 27, in setUp
self.privileged_user = gbe_tests.factories.ProfileFactory.create().user_object
AttributeError: 'module' object has no attribute 'factories'

or something similar to:

Traceback (most recent call last):
    File "/Library/Python/2.7/site-packages/nose/loader.py", line 420, in loadTestsFromName
      addr.filename, addr.module)
    File "/Library/Python/2.7/site-packages/nose/importer.py", line 47, in importFromPath
      return self.importFromDir(dir_path, fqname)
    File "/Library/Python/2.7/site-packages/nose/importer.py", line 94, in importFromDir
      mod = load_module(part_fqname, fh, filename, desc)
    File "/Users/bethlakshmi/Documents/mysite/GBE2/expo/ticketing/test/test_edit_event.py", line 12, in <module>
      import gbe.tests.factories as gbe_fact

    ImportError: No module named factories

Things I've tried:

  • various variations on "from gbe.tests import functions, factories"
  • various forms of "import gbe.tests as gbe_test" and rename the calls to the factories and functions accordingly.
  • messing with the init.py file to import there, or to set the all to functions.py and factories.py

The command I'm trying to run is:

./manage.py test ticketing

As far as I can tell, to the code, the internal contents of gbe.tests just doesn't exist. I have no problem importing gbe.models, gbe.views, gbe.functions, etc. Nor do I have a problem with anything in /ticketing

I've read every question in stackoverflow that I can find with no success.

90% of the accepted answers seem to be "make sure you have an init.py" - which is already true in my case.

I also tried messing with the system path, with no success.

Note:

You'll see that I repeated the factories I want to mock in the ticketing/tests/factories.py - that's the only way I can get the tests in ticketing to run, and I refuse to have broken tests in a check-in if I can help it.

I've tried removing those classes, and the errors above prevail - when I CAN refactor to remove this redundant code, I will do so with great joy.

Final Question

What am I missing?

Aucun commentaire:

Enregistrer un commentaire