python - Nose not discovering package level tests in Django

拟墨画扇 提交于 2019-12-11 01:45:52

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/18150766/python-nose-not-discovering-package-level-tests-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!