Faster Django Testing

I then created a pytest.ini file with the following contents: [pytest] python_files=tests.py DJANGO_SETTINGS_MODULE=fast_tests.settings And then ran py.test with 3 parallel processes: $ py.test -n 3 =================================== test session starts ==================================== platform linux2 — Python 2.7.6 — py-1.4.26 — pytest-2.6.4 plugins: xdist, django gw0 [12] / gw1 [12] / gw2 [12] scheduling tests via LoadScheduling ………..F ========================================= FAILURES ========================================= ____________________________________ ModelTests.test_7 _____________________________________ [gw1] linux2 — Python 2.7.6 /home/mark/.virtualenvs/fast_tests/bin/python self = <example.tests.ModelTests testMethod=test_7> def test_7(self): self.test_1() > self.assertTrue(False, 'False positive') E AssertionError: False positive example/tests.py:36: AssertionError =========================== 1 failed, 11 passed in 2.82 seconds ============================ It ran the same set of tests and reported the failure 3.4 times faster than the regular Django test runner..Coverage py.test has a pytest-cov plugin which adds support for running tests with the coverage tool..The coverage tool generates a .coverage file which holds statistics on how many lines of your code base (which were seen by coverage) were hit at least one when running the testing suite..You can also also ask for .py,cover files to be generated along side your source code files..These are annotated files showing which lines have and have not been hit by your tests and lines which were deemed to be non-statement lines and were skipped..$ py.test -n3 –cov..–cov-report annotate coverage supports returning an error code if a certain percentage of lines were not hit..Below I check to see if less than 95% percent of the lines where hit: $ coverage report –fail-under=95 Name Stmts Miss Cover —————————————————– example/__init__ 0 0 100% example/admin 1 0 100% example/migrations/0001_initial 5 0 100% example/migrations/__init__ 0 0 100% example/models 4 0 100% example/tests 33 0 100% example/views 1 1 0% fast_tests/__init__ 0 0 100% fast_tests/settings 17 0 100% fast_tests/urls 3 3 0% fast_tests/wsgi 4 4 0% manage 6 6 0% —————————————————– TOTAL 74 14 81% Only 81% were hit so the exit code will be 127: $ echo $?.127 If I lower the threshold to 80% exit code 0 is returned: $ coverage report –fail-under=80 ….$ echo $?. More details

Leave a Reply