I'd like to measure the (wall?) time each individual test case takes to run.
I suppose wrapping the test_runner in a timeit will do the job, but before I dive down that rabbit hole perhaps there is a smarter way to do it?
This already gives me a cProfile to pore over, but nothing really jumps out as horrendously bad. I'm thinking perhaps my time can be focused on the ones that run the longest.
time python -m cProfile -o keep-p4-serialize.profile manage.py test -v 3 -k --parallel 4
eg:
test_dependencies (api.tests.TestMetricClasses) ... ok (4.003s)
test_metrics (api.tests.TestMetricClasses) ... ok (8.329s)
test_parameters (api.tests.TestMetricClasses) ... ok (0.001s)
Update:
Original post was in 2019 and it seems that the package discussed below (django-slowtest
) is no longer maintained. As for now I don't see any maintained forks either.
--
Old question but I came across it and noticed that django-slowtest
is not mentioned, which I think is absolutely worth mentioning. I've been using it for quite a while now and I'm very satisfied with the results. Only downside, it does not yet state that it supports Django >= 2 officially, but I have not encountered any issues and since it is in development only for me I feel safe using it.
--parallel
slow
https://github.com/realpython/django-slow-tests
from docs: Install:
$ pip install django-slowtests
Add the following settings:
TEST_RUNNER = 'django_slowtests.testrunner.DiscoverSlowestTestsRunner'
NUM_SLOW_TESTS = 10
# (Optional)
SLOW_TEST_THRESHOLD_MS = 200 # Only show tests slower than 200ms
# (Optional)
ALWAYS_GENERATE_SLOW_REPORT = False # Generate report only when requested using --slowreport flag
finally, run your tests
$ python manage.py test
Creating test database for alias 'default'...
..........
----------------------------------------------------------------------
Ran 10 tests in 0.413s
OK
Destroying test database for alias 'default'...
Ten slowest tests:
0.3597s test_detail_view_with_a_future_poll (polls.tests.PollIndexDetailTests)
0.0284s test_detail_view_with_a_past_poll (polls.tests.PollIndexDetailTests)
0.0068s test_index_view_with_a_future_poll (polls.tests.PollViewTests)
0.0047s test_index_view_with_a_past_poll (polls.tests.PollViewTests)
0.0045s test_index_view_with_two_past_polls (polls.tests.PollViewTests)
0.0041s test_index_view_with_future_poll_and_past_poll (polls.tests.PollViewTests)
0.0036s test_index_view_with_no_polls (polls.tests.PollViewTests)
0.0003s test_was_published_recently_with_future_poll (polls.tests.PollMethodTests)
0.0002s test_was_published_recently_with_recent_poll (polls.tests.PollMethodTests)
0.0002s test_was_published_recently_with_old_poll (polls.tests.PollMethodTests)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With