4

When I run tests, django runs 0 tests. This is my project structure:

manage.py
- app/
  - main_app/
    - test/
       - tests_v1.py
       - tests_v2.py

I use this command:

python manage.py test --pattern="tests_v*.py"

Django runs 0 tests in 0 seconds. What am I missing?

Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180

6 Answers6

9

Add __init__.py file inside test directory. Then import all inside it like

In __init__.py

from tests_v1 import *
from tests_v2 import *

Note

Name tests_v1 as tests_v1.py

itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
3

I faced this problem today. It turns out that my problem was two-fold.

  1. I forgot to remove the tests.py from my app folder after I've created my tests folder.
  2. I didn't add __init__.py which makes my directory a package.

It took me a lot of time so I am writing a response just in case someone else faced the same problem. I was able to make my test work once I did the following.

Solution:

  1. Remove tests.py under the app folder.
  2. Add __init__.py inside my test folder.
WJ Chua
  • 133
  • 1
  • 6
1

You can specify the folder where the tests are found. Try this:

python manage.py test app.mainapp.test
wobbily_col
  • 11,390
  • 12
  • 62
  • 86
1

Ensure to prefix all test cases (methods) with "test". e.g.

def test_create_cycles_with_valid_data(self):
1

Something similar happened to me and the answer has to do with __init__.py, however, it depends on how you have structured your project, in my case, I had something like this:

├── apps
│   ├── app1
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations/
│   │   ├── test/
│   │   │   ├── test_models.py
│   │   ├── models.py
│   │   ├── views.py
│   ├── app2
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations/
│   │   ├── test.py
│   │   ├── models.py
│   │   ├── views.py
    .
    .
    .

Getting:

Ran 0 tests in 0.000s

The solution to this was to add __init__.py inside the tests folder and inside the apps folder. The structure is as follows:

├── apps
│   ├── __init__.py **
│   ├── app1
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations/
│   │   ├── test/
│   │   │   ├── __init__.py **
│   │   │   ├── test_models.py
│   │   ├── models.py
│   │   ├── views.py
│   ├── app2
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations/
│   │   ├── test.py
│   │   ├── models.py
│   │   ├── views.py
│   ├── app2
    .
    .
    .

I hope this can help someone else.

Rogelio Carrera
  • 161
  • 1
  • 6
-2
1) `python ./manage.py test appname.tests_v1.classname.function_name`

2) Else 

`pip install nose pinocchio django_nose`

add below line in settings.py 

TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
NOSE_ARGS = ['--with-spec', '--spec-color']  

then run 

`python manage.py test`

ref : http://hentzia.com/blog/bdd-with-python.html

Anup Yadav
  • 2,825
  • 3
  • 21
  • 30
  • 1
    Nose might be a cool thing, but this just works around the problem and does not address it. Adding additional dependencies to a project is also something people should usually think through twice. – dahrens Jan 10 '18 at 12:32