1

I'm trying to write a test for my python program, and I've created a /tests directory, where / means the projects root, not system root.

I have all my source files in /myProjectName.

Both directories have a __init__.py file, but project root does not, and both files are empty (do I need a __init__.py in the tests directory ?)

I've tried importing /myProjectName/main.py in /tests/test_main.py, but it doesn't work.

What is the right way to either structure the project directories or import main.py in test_main.py?

francium
  • 2,384
  • 3
  • 20
  • 29

3 Answers3

1

I would suggest moving your tests directory to be inside your project directory. Then you can use the answers here to import from the parent directory when you're in tests: Importing modules from parent folder

Otherwise, you can simply set your $PYTHONPATH to point to your project directory when you run tests.

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

I would like to suggest nose here as it automatically detects the tests and runs them for you

To run the tests, simply do a

$ nosetests

in your project directory

Tasdik Rahman
  • 2,160
  • 1
  • 25
  • 37
1

Why not try this line in testmain.py:

import os
os.chdir("/myProjectName/main.py ")

Then execute the script.

Hope it works!

Vin
  • 729
  • 9
  • 15