Jacob Harding
Jacob Harding

Reputation: 661

Why won't unittest find my tests.py?

I was happily writing and testing modules for an API I'm working and using the command:

python -m unittest myAPI.tests.APITests.test_mod_to_test 

As expected my tests live in tests.py in the module myAPI. This was working great and I found that I had to refactor some code in my api.py file that holds the functions I'm testing. I changed what I needed to change and went back to my command line and ran the exact same command by pressing up on the keyboard to save typing. I got this error:

AttributeError: 'module' object has no attribute 'tests' 

when I didn't even change anything in the directory or tests.py. I didn't move the file or change the command, and I can import the tests.py file in the python shell using:

from myAPI import tests

After this I enter:

print tests

and the output is what is expected:

<module 'myAPI.tests' from '/path/to/myAPI/tests.pyc'>

I'm using python 2.7 in a virtualenv with nothing really installed in it. The app is for a django 1.2.1 application but is not relevant being I'm not touching django during my tests(other than it is installed in my env).

This is stumping me and I figure how it went from working great to not without me changing some in the directory or the tests.py. Any help is great thanks.

Upvotes: 3

Views: 325

Answers (1)

Namey
Namey

Reputation: 1212

I can suggest two possible reasons for this error, but it's impossible to know without more information.

  1. You are running the test from the wrong place. You did not state where you were running "python -m unittest myAPI.tests.APITests.test_mod_to_test". If you are running it from inside the directory myAPI, you can end up with weird failures because you don't have access to the __init__.py file in the directory myAPI. So, you want to make sure you run that call from the directory that contains myAPI tests.

  2. You don't have an __init__.py file in your 'tests' directory. It's easy to forget to put one in when you make a new directory. Without it, you won't be able to import anything from inside the directory.

Upvotes: 1

Related Questions