How to organize test files on django 3.x and run single file tests?

Viewed 160

I've been learning about django testing and would like to have several tests files that I would like to rename and place then in to a folder. How is the proper way to do it?

In a single file test.py I used to run my test with the "python.manage.py tests appname" command.

I tried to organize my tests files this way.

app/tests/__init__py
app/tests/test_something.py
app/tests/test_something_else.py

The problem is that when I organize them in several files I can't run a single test file. At this point all I can do is run then all with the command "python manage.py tests app test_something_else.py", but this it will also runs test_something.py.

I've already try the django documentation, but it hasn't help much because I think this example is for a single test.py file.

(for an example app named animals)
Run just one test case
$ ./manage.py test animals.tests.AnimalTestCase

Run just one test method
$ ./manage.py test animals.tests.AnimalTestCase.test_animals_can_speak

What am I doing wrong?

1 Answers

You still can, modify your command to python manage.py test app.tests.test_something_else to run tests in a single file. You can also run a specific test like python manage.py test app.tests.test_something_else.TestCaseClassName.test_the_test. The problem with your last command is that you are trying to access the class before the module that contains the class test case.

Related