Running a specific test case in Django when your app has a tests directory
Running a specific test case in Django when your app has a tests directory
--
Become part of the top 3% of the developers by applying to Toptal
https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 3 Looping
--
Chapters
00:00 Question
01:32 Accepted answer (Score 187)
01:55 Answer 2 (Score 233)
03:10 Answer 3 (Score 25)
03:22 Answer 4 (Score 11)
04:41 Thank you
--
Full question
https://stackoverflow.com/questions/5875...
Question links:
[http://docs.djangoproject.com/en/1.3/top...]: http://docs.djangoproject.com/en/1.3/top...
Accepted answer links:
[django-nose]: https://github.com/jbalogh/django-nose
Answer 2 links:
[See the Django documentation for more information]: https://docs.djangoproject.com/en/stable.../
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #unittesting
ANSWER 1
Score 268
Since Django 1.6 you can run a complete test case, or single test, using the complete dot notation for the element you want to run.
Automatic test discovery will now find tests in any file that starts with test under the working directory, so addressing the question you would have to rename your files, but you can now keep them inside the directory you want. If you want to use custom file names you can specify a pattern (default Django test runner) with the option flag --pattern="my_pattern_*.py".
So if you are in your manage.py directory and want to run the test test_a inside TestCase subclass A inside a file tests.py under the app/module example you would do:
python manage.py test example.tests.A.test_a
If you don't want to include a dependency and are in Django 1.6 or later that's how you do it.
ACCEPTED ANSWER
Score 195
Check out django-nose. This allows you to specify tests to run like:
python manage.py test another.test:TestCase.test_method
or as noted in comments, use the syntax:
python manage.py test another.test.TestCase.test_method
ANSWER 3
Score 29
This should work-
python manage.py test my_app.tests.storage_tests
ANSWER 4
Score 11
I was having this problem myself and found this question, in case anyone else comes along, here was what I dug up. The DjangoTestSuiteRuner uses a method called build_test(label) that figures out what test cases to run based on the label. Looking into this method it turns out they're doing a getattr() on either the "models" or "test" module. This means if you return a suite the test runner isn't looking for your test cases in that suite, it only looks in one of those modules.
A quick work-around is to use __init__.py to import your tests directly instead of defining a suite. The makes them part of the "test" module and so build_test(label) can find them.
For your example above, tests/__init__.py should simply contain:
from field_tests import *
from storage_tests import *
This isn't very elegant and of course if you're trying to do something more complicated with your suite then this won't work, but it would for this case.