python - Nose not discovering package level tests in Django
Asked Answered
R

1

2

I'm setting up a directory structure for my Django app to separate functional and unit tests. I am using nose as the test runner for my Django project.

At the root of the Django project, I have a folder called "tests" that has this structure:

tests
├── __init__.py
├── functional
│   ├── __init__.py
└── unit
    ├── __init__.py
    ├── data.py
    ├── tests.py

If I want to run just the unit tests, should I not be able to use the following from the project root:

$ nosetests tests.unit

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

As you can see, this doesn't find the tests in the tests.py file.

However, when I run using the directory structure, the tests are found as they should be:

$ nosetests tests/unit/
E
# .. Some errors I expected because settings are not initialized when called this way
-----------------
Ran 1 test in 0.001s

FAILED (errors=1)

What am I missing? My main issue is that I have a setup function in tests.unit.__init__.py that should be called for creating the data in the test DB for the upcoming tests.

Thanks

Roundly answered 9/8, 2013 at 15:50 Comment(3)
might you need to include import unit in the first init? (just throwing this out there)Satire
@Satire don't think so, as I can import the unit.tests module from the same directory using the python command-line python -c 'import tests.unit'Roundly
Did you try to run nose in verbose mode? try using -v and -vv to see if they give any hint about the test discovery.Hercegovina
D
3

This all depends on what kind of code is in tests/unit/__init__.py

When you say

nosetests tests.unit

You are pointing to unit/__init__.py not the directory unit/ thus if you had no tests in your __init__.py module then nothing would be run. So it is understandable when you say you used the directory path and then your tests started working.

You mention

What am I missing? My main issue is that I have a setup function in tests.unit.init.py that should be called for creating the data in the test DB for the upcoming tests.

It is likely that although you have a setup function in __init__.py you may have not ever imported your test functions into __init__.py

One quick fix to this would be to add this line in __init__.py

from tests.unit.tests import *

That said it is really not very wise to be putting any code in __init__.py at all and if you have code that returns some kind of configuration data I would recommend creating a new library module with functions that will return configuration data to your tests

Dianadiandra answered 10/8, 2013 at 8:48 Comment(2)
Thanks for the answer! My understanding of nose's discovery feature is that it would like for all modules that matched the pattern. So when I point it to tests.unit, that it would find tests.py, and all the test functions inside. But now I added from .tests import *, which seems to do the trick. Is there no way to let nose find the tests automatically, instead of importing them manually?Roundly
Yeah just do nosetests unit/ if you want all the tests found automatically within the directory. As I mentioned, when you say nosetests tests.unit, you are telling nose to find all tests in a specific module. If that module has no tests, well then you're out of luck. I don't use much module notation though so I can't speak much more in depth. Maybe look at the source code at github.com/nose-devs/nose to find out moreDianadiandra

© 2022 - 2024 — McMap. All rights reserved.