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?

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

Why not try this line in testmain.py:

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

Then execute the script.

Hope it works!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.