30 Mar 2015 by dryobates
If you want do Test Driven Development your tests should run in a split second. Your tests shouldn't touch database if it's possible nor you should run slow acceptance tests in one run with unit tests for TDD. django-smarttest is package which helps you keep with that rules.
In django-smarttest  there are two decorators which help you keep your tests fast. In the same vein package contains TestCase subclass for running splinter  with different drivers. The fastest for normal TDD and slower ones which can run real browser.
no_db_testcase decorator is security fence that separates your tests from database. If used as decorator on test method or TestCase class it would raise RunTime error if your decorated tests touch database in any time. Unit tests shoudn't touch database at all unless you test database queries. Most of my tests don't need to check database so I put decorator on them like that:
And if I touch database by accident I'll get exception:
Such error is good indicator that I have missed to mock something in my tests.
The other decorator allows you to mark tests as some type of tests. Generally I use two types of tests: unit tests and acceptance tests. When using double loop I like to disable running acceptance tests untill I'll finish all unit tests for given scenario and then run all of them at once. That method allows me running tests continuously while doing TDD as my unit tests are fast while acceptance aren't.
Just like with previous decorator test_type can be used on single test methods and on whole TestCase subclasses.
And you can set up IGNORE_TESTS environment variable to ignore tests marked with "acceptance":
Last addition is SplinterTestCase. Splinter  gives you consistent api for manipulating different drivers. You can instruct your browser (e.g. "firefox" or "chrome") to simulate user behaviour. When you run tests on CI machine you often don't have real browser so you can switch your driver to some headless one (e.g. "phantomjs", "zope.testbrowser" or the fastest one "django"). SplinterTestCase is thin wrapper arround django TestCase that allowes you running different splinter drivers just switching them in settings.py:
SplinterTestCase instance has attribute "browser" which you use to instruct browser and method "get_host" which returns correct host name for given driver.
|||(1, 2) splinter https://splinter.readthedocs.org/en/latest/|