简体   繁体   中英

Python: Nosetests with multiple files

This is a broad question because no one seems to have found a solution to it as yet so I think asking to see a working example might prove more useful. So here goes:

Has anyone run a nosetests on a python project using imports of multiple files/packages?

What I mean is, do you have a directory listing such as:

project/
    |
    |____app/
          |___main.py
          |___2ndFile.py
          |___3rdFile.py
     |____tests/
          |____main_tests.py

Where your main.py imports multiple files and you perform a nosetests from the project file of utilizing a test script in the main_tests.py file? If so please can you screen shot your import section both of all your main files and your main_tests.py file?

This seems to be a major issue in nosetests, with no apparent solution:

  1. You can't have python modules starting with a digit, so 2ndFile.py , 3rdFile.py won't actually work (rename them).
  2. You'll need an __init__.py inside the app directory, for it to be considered a package, so add that (it can be empty file).
  3. You don't need an __init__.py in the tests directory!
  4. The import statements in main_tests.py should look like from app.main import blah
  5. The absolute path of the project directory needs to be in your sys.path . To achieve this, set an environment variable: export PYTHONPATH=/path/to/project

Now running nosetests should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM