tutorial05.txt 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. =====================================
  2. Writing your first Django app, part 5
  3. =====================================
  4. This tutorial begins where :doc:`Tutorial 4 </intro/tutorial04>` left off.
  5. We've built a Web-poll application, and we'll now create some automated tests
  6. for it.
  7. Introducing automated testing
  8. =============================
  9. What are automated tests?
  10. -------------------------
  11. Tests are simple routines that check the operation of your code.
  12. Testing operates at different levels. Some tests might apply to a tiny detail
  13. - *does a particular model method return values as expected?*, while others
  14. examine the overall operation of the software - *does a sequence of user inputs
  15. on the site produce the desired result?* That's no different from the kind of
  16. testing you did earlier in :doc:`Tutorial 1 </intro/tutorial01>`, using the
  17. shell to examine the behavior of a method, or running the application and
  18. entering data to check how it behaves.
  19. What's different in *automated* tests is that the testing work is done for
  20. you by the system. You create a set of tests once, and then as you make changes
  21. to your app, you can check that your code still works as you originally
  22. intended, without having to perform time consuming manual testing.
  23. Why you need to create tests
  24. ----------------------------
  25. So why create tests, and why now?
  26. You may feel that you have quite enough on your plate just learning
  27. Python/Django, and having yet another thing to learn and do may seem
  28. overwhelming and perhaps unnecessary. After all, our polls application is
  29. working quite happily now; going through the trouble of creating automated
  30. tests is not going to make it work any better. If creating the polls
  31. application is the last bit of Django programming you will ever do, then true,
  32. you don't need to know how to create automated tests. But, if that's not the
  33. case, now is an excellent time to learn.
  34. Tests will save you time
  35. ~~~~~~~~~~~~~~~~~~~~~~~~
  36. Up to a certain point, 'checking that it seems to work' will be a satisfactory
  37. test. In a more sophisticated application, you might have dozens of complex
  38. interactions between components.
  39. A change in any of those components could have unexpected consequences on the
  40. application's behavior. Checking that it still 'seems to work' could mean
  41. running through your code's functionality with twenty different variations of
  42. your test data just to make sure you haven't broken something - not a good use
  43. of your time.
  44. That's especially true when automated tests could do this for you in seconds.
  45. If something's gone wrong, tests will also assist in identifying the code
  46. that's causing the unexpected behavior.
  47. Sometimes it may seem a chore to tear yourself away from your productive,
  48. creative programming work to face the unglamorous and unexciting business
  49. of writing tests, particularly when you know your code is working properly.
  50. However, the task of writing tests is a lot more fulfilling than spending hours
  51. testing your application manually or trying to identify the cause of a
  52. newly-introduced problem.
  53. Tests don't just identify problems, they prevent them
  54. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  55. It's a mistake to think of tests merely as a negative aspect of development.
  56. Without tests, the purpose or intended behavior of an application might be
  57. rather opaque. Even when it's your own code, you will sometimes find yourself
  58. poking around in it trying to find out what exactly it's doing.
  59. Tests change that; they light up your code from the inside, and when something
  60. goes wrong, they focus light on the part that has gone wrong - *even if you
  61. hadn't even realized it had gone wrong*.
  62. Tests make your code more attractive
  63. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  64. You might have created a brilliant piece of software, but you will find that
  65. many other developers will simply refuse to look at it because it lacks tests;
  66. without tests, they won't trust it. Jacob Kaplan-Moss, one of Django's
  67. original developers, says "Code without tests is broken by design."
  68. That other developers want to see tests in your software before they take it
  69. seriously is yet another reason for you to start writing tests.
  70. Tests help teams work together
  71. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  72. The previous points are written from the point of view of a single developer
  73. maintaining an application. Complex applications will be maintained by teams.
  74. Tests guarantee that colleagues don't inadvertently break your code (and that
  75. you don't break theirs without knowing). If you want to make a living as a
  76. Django programmer, you must be good at writing tests!
  77. Basic testing strategies
  78. ========================
  79. There are many ways to approach writing tests.
  80. Some programmers follow a discipline called "`test-driven development`_"; they
  81. actually write their tests before they write their code. This might seem
  82. counter-intuitive, but in fact it's similar to what most people will often do
  83. anyway: they describe a problem, then create some code to solve it. Test-driven
  84. development simply formalizes the problem in a Python test case.
  85. More often, a newcomer to testing will create some code and later decide that
  86. it should have some tests. Perhaps it would have been better to write some
  87. tests earlier, but it's never too late to get started.
  88. Sometimes it's difficult to figure out where to get started with writing tests.
  89. If you have written several thousand lines of Python, choosing something to
  90. test might not be easy. In such a case, it's fruitful to write your first test
  91. the next time you make a change, either when you add a new feature or fix a bug.
  92. So let's do that right away.
  93. .. _test-driven development: http://en.wikipedia.org/wiki/Test-driven_development/
  94. Writing our first test
  95. ======================
  96. We identify a bug
  97. -----------------
  98. Fortunately, there's a little bug in the ``polls`` application for us to fix
  99. right away: the ``Poll.was_published_recently()`` method returns ``True`` if
  100. the ``Poll`` was published within the last day (which is correct) but also if
  101. the ``Poll``'s ``pub_date`` field is in the future (which certainly isn't).
  102. You can see this in the Admin; create a poll whose date lies in the future;
  103. you'll see that the ``Poll`` change list claims it was published recently.
  104. You can also see this using the shell::
  105. >>> import datetime
  106. >>> from django.utils import timezone
  107. >>> from polls.models import Poll
  108. >>> # create a Poll instance with pub_date 30 days in the future
  109. >>> future_poll = Poll(pub_date=timezone.now() + datetime.timedelta(days=30))
  110. >>> # was it published recently?
  111. >>> future_poll.was_published_recently()
  112. True
  113. Since things in the future are not 'recent', this is clearly wrong.
  114. Create a test to expose the bug
  115. -------------------------------
  116. What we've just done in the shell to test for the problem is exactly what we
  117. can do in an automated test, so let's turn that into an automated test.
  118. The best place for an application's tests is in the application's ``tests.py``
  119. file - the testing system will look there for tests automatically.
  120. Put the following in the ``tests.py`` file in the ``polls`` application::
  121. import datetime
  122. from django.utils import timezone
  123. from django.test import TestCase
  124. from polls.models import Poll
  125. class PollMethodTests(TestCase):
  126. def test_was_published_recently_with_future_poll(self):
  127. """
  128. was_published_recently() should return False for polls whose
  129. pub_date is in the future
  130. """
  131. future_poll = Poll(pub_date=timezone.now() + datetime.timedelta(days=30))
  132. self.assertEqual(future_poll.was_published_recently(), False)
  133. What we have done here is created a :class:`django.test.TestCase` subclass
  134. with a method that creates a ``Poll`` instance with a ``pub_date`` in the
  135. future. We then check the output of ``was_published_recently()`` - which
  136. *ought* to be False.
  137. Running tests
  138. -------------
  139. In the terminal, we can run our test::
  140. python manage.py test polls
  141. and you'll see something like::
  142. Creating test database for alias 'default'...
  143. F
  144. ======================================================================
  145. FAIL: test_was_published_recently_with_future_poll (polls.tests.PollMethodTests)
  146. ----------------------------------------------------------------------
  147. Traceback (most recent call last):
  148. File "/path/to/mysite/polls/tests.py", line 16, in test_was_published_recently_with_future_poll
  149. self.assertEqual(future_poll.was_published_recently(), False)
  150. AssertionError: True != False
  151. ----------------------------------------------------------------------
  152. Ran 1 test in 0.001s
  153. FAILED (failures=1)
  154. Destroying test database for alias 'default'...
  155. What happened is this:
  156. * ``python manage.py test polls`` looked for tests in the ``polls`` application
  157. * it found a subclass of the :class:`django.test.TestCase` class
  158. * it created a special database for the purpose of testing
  159. * it looked for test methods - ones whose names begin with ``test``
  160. * in ``test_was_published_recently_with_future_poll`` it created a ``Poll``
  161. instance whose ``pub_date`` field is 30 days in the future
  162. * ... and using the ``assertEqual()`` method, it discovered that its
  163. ``was_published_recently()`` returns ``True``, though we wanted it to return
  164. ``False``
  165. The test informs us which test failed and even the line on which the failure
  166. occurred.
  167. Fixing the bug
  168. --------------
  169. We already know what the problem is: ``Poll.was_published_recently()`` should
  170. return ``False`` if its ``pub_date`` is in the future. Amend the method in
  171. ``models.py``, so that it will only return ``True`` if the date is also in the
  172. past::
  173. def was_published_recently(self):
  174. now = timezone.now()
  175. return now - datetime.timedelta(days=1) <= self.pub_date < now
  176. and run the test again::
  177. Creating test database for alias 'default'...
  178. .
  179. ----------------------------------------------------------------------
  180. Ran 1 test in 0.001s
  181. OK
  182. Destroying test database for alias 'default'...
  183. After identifying a bug, we wrote a test that exposes it and corrected the bug
  184. in the code so our test passes.
  185. Many other things might go wrong with our application in the future, but we can
  186. be sure that we won't inadvertently reintroduce this bug, because simply
  187. running the test will warn us immediately. We can consider this little portion
  188. of the application pinned down safely forever.
  189. More comprehensive tests
  190. ------------------------
  191. While we're here, we can further pin down the ``was_published_recently()``
  192. method; in fact, it would be positively embarrassing if in fixing one bug we had
  193. introduced another.
  194. Add two more test methods to the same class, to test the behavior of the method
  195. more comprehensively::
  196. def test_was_published_recently_with_old_poll(self):
  197. """
  198. was_published_recently() should return False for polls whose pub_date
  199. is older than 1 day
  200. """
  201. old_poll = Poll(pub_date=timezone.now() - datetime.timedelta(days=30))
  202. self.assertEqual(old_poll.was_published_recently(), False)
  203. def test_was_published_recently_with_recent_poll(self):
  204. """
  205. was_published_recently() should return True for polls whose pub_date
  206. is within the last day
  207. """
  208. recent_poll = Poll(pub_date=timezone.now() - datetime.timedelta(hours=1))
  209. self.assertEqual(recent_poll.was_published_recently(), True)
  210. And now we have three tests that confirm that ``Poll.was_published_recently()``
  211. returns sensible values for past, recent, and future polls.
  212. Again, ``polls`` is a simple application, but however complex it grows in the
  213. future and whatever other code it interacts with, we now have some guarantee
  214. that the method we have written tests for will behave in expected ways.
  215. Test a view
  216. ===========
  217. The polls application is fairly undiscriminating: it will publish any poll,
  218. including ones whose ``pub_date`` field lies in the future. We should improve
  219. this. Setting a ``pub_date`` in the future should mean that the Poll is
  220. published at that moment, but invisible until then.
  221. A test for a view
  222. -----------------
  223. When we fixed the bug above, we wrote the test first and then the code to fix
  224. it. In fact that was a simple example of test-driven development, but it
  225. doesn't really matter in which order we do the work.
  226. In our first test, we focused closely on the internal behavior of the code. For
  227. this test, we want to check its behavior as it would be experienced by a user
  228. through a web browser.
  229. Before we try to fix anything, let's have a look at the tools at our disposal.
  230. The Django test client
  231. ----------------------
  232. Django provides a test :class:`~django.test.client.Client` to simulate a user
  233. interacting with the code at the view level. We can use it in ``tests.py``
  234. or even in the shell.
  235. We will start again with the shell, where we need to do a couple of things that
  236. won't be necessary in ``tests.py``. The first is to set up the test environment
  237. in the shell::
  238. >>> from django.test.utils import setup_test_environment
  239. >>> setup_test_environment()
  240. Next we need to import the test client class (later in ``tests.py`` we will use
  241. the :class:`django.test.TestCase` class, which comes with its own client, so
  242. this won't be required)::
  243. >>> from django.test.client import Client
  244. >>> # create an instance of the client for our use
  245. >>> client = Client()
  246. With that ready, we can ask the client to do some work for us::
  247. >>> # get a response from '/'
  248. >>> response = client.get('/')
  249. >>> # we should expect a 404 from that address
  250. >>> response.status_code
  251. 404
  252. >>> # on the other hand we should expect to find something at '/polls/'
  253. >>> # we'll use 'reverse()' rather than a harcoded URL
  254. >>> from django.core.urlresolvers import reverse
  255. >>> response = client.get(reverse('polls:index'))
  256. >>> response.status_code
  257. 200
  258. >>> response.content
  259. '\n\n\n <p>No polls are available.</p>\n\n'
  260. >>> # note - you might get unexpected results if your ``TIME_ZONE``
  261. >>> # in ``settings.py`` is not correct. If you need to change it,
  262. >>> # you will also need to restart your shell session
  263. >>> from polls.models import Poll
  264. >>> from django.utils import timezone
  265. >>> # create a Poll and save it
  266. >>> p = Poll(question="Who is your favorite Beatle?", pub_date=timezone.now())
  267. >>> p.save()
  268. >>> # check the response once again
  269. >>> response = client.get('/polls/')
  270. >>> response.content
  271. '\n\n\n <ul>\n \n <li><a href="/polls/1/">Who is your favorite Beatle?</a></li>\n \n </ul>\n\n'
  272. >>> response.context['latest_poll_list']
  273. [<Poll: Who is your favorite Beatle?>]
  274. Improving our view
  275. ------------------
  276. The list of polls shows polls that aren't published yet (i.e. those that have a
  277. ``pub_date`` in the future). Let's fix that.
  278. In :doc:`Tutorial 4 </intro/tutorial04>` we deleted the view functions from
  279. ``views.py`` in favor of a :class:`~django.views.generic.list.ListView` in
  280. ``urls.py``::
  281. url(r'^$',
  282. ListView.as_view(
  283. queryset=Poll.objects.order_by('-pub_date')[:5],
  284. context_object_name='latest_poll_list',
  285. template_name='polls/index.html'),
  286. name='index'),
  287. ``response.context_data['latest_poll_list']`` extracts the data this view
  288. places into the context.
  289. We need to amend the line that gives us the ``queryset``::
  290. queryset=Poll.objects.order_by('-pub_date')[:5],
  291. Let's change the queryset so that it also checks the date by comparing it with
  292. ``timezone.now()``. First we need to add an import::
  293. from django.utils import timezone
  294. and then we must amend the existing ``url`` function to::
  295. url(r'^$',
  296. ListView.as_view(
  297. queryset=Poll.objects.filter(pub_date__lte=timezone.now) \
  298. .order_by('-pub_date')[:5],
  299. context_object_name='latest_poll_list',
  300. template_name='polls/index.html'),
  301. name='index'),
  302. ``Poll.objects.filter(pub_date__lte=timezone.now)`` returns a queryset
  303. containing Polls whose ``pub_date`` is less than or equal to - that is, earlier
  304. than or equal to - ``timezone.now``. Notice that we use a callable queryset
  305. argument, ``timezone.now``, which will be evaluated at request time. If we had
  306. included the parentheses, ``timezone.now()`` would be evaluated just once when
  307. the web server is started.
  308. Testing our new view
  309. --------------------
  310. Now you can satisfy yourself that this behaves as expected by firing up the
  311. runserver, loading the site in your browser, creating ``Polls`` with dates in
  312. the past and future, and checking that only those that have been published are
  313. listed. You don't want to have to do that *every single time you make any
  314. change that might affect this* - so let's also create a test, based on our
  315. shell session above.
  316. Add the following to ``polls/tests.py``::
  317. from django.core.urlresolvers import reverse
  318. and we'll create a factory method to create polls as well as a new test class::
  319. def create_poll(question, days):
  320. """
  321. Creates a poll with the given `question` published the given number of
  322. `days` offset to now (negative for polls published in the past,
  323. positive for polls that have yet to be published).
  324. """
  325. return Poll.objects.create(question=question,
  326. pub_date=timezone.now() + datetime.timedelta(days=days))
  327. class PollViewTests(TestCase):
  328. def test_index_view_with_no_polls(self):
  329. """
  330. If no polls exist, an appropriate message should be displayed.
  331. """
  332. response = self.client.get(reverse('polls:index'))
  333. self.assertEqual(response.status_code, 200)
  334. self.assertContains(response, "No polls are available.")
  335. self.assertQuerysetEqual(response.context['latest_poll_list'], [])
  336. def test_index_view_with_a_past_poll(self):
  337. """
  338. Polls with a pub_date in the past should be displayed on the index page.
  339. """
  340. create_poll(question="Past poll.", days=-30)
  341. response = self.client.get(reverse('polls:index'))
  342. self.assertQuerysetEqual(
  343. response.context['latest_poll_list'],
  344. ['<Poll: Past poll.>']
  345. )
  346. def test_index_view_with_a_future_poll(self):
  347. """
  348. Polls with a pub_date in the future should not be displayed on the
  349. index page.
  350. """
  351. create_poll(question="Future poll.", days=30)
  352. response = self.client.get(reverse('polls:index'))
  353. self.assertContains(response, "No polls are available.", status_code=200)
  354. self.assertQuerysetEqual(response.context['latest_poll_list'], [])
  355. def test_index_view_with_future_poll_and_past_poll(self):
  356. """
  357. Even if both past and future polls exist, only past polls should be
  358. displayed.
  359. """
  360. create_poll(question="Past poll.", days=-30)
  361. create_poll(question="Future poll.", days=30)
  362. response = self.client.get(reverse('polls:index'))
  363. self.assertQuerysetEqual(
  364. response.context['latest_poll_list'],
  365. ['<Poll: Past poll.>']
  366. )
  367. def test_index_view_with_two_past_polls(self):
  368. """
  369. The polls index page may display multiple polls.
  370. """
  371. create_poll(question="Past poll 1.", days=-30)
  372. create_poll(question="Past poll 2.", days=-5)
  373. response = self.client.get(reverse('polls:index'))
  374. self.assertQuerysetEqual(
  375. response.context['latest_poll_list'],
  376. ['<Poll: Past poll 2.>', '<Poll: Past poll 1.>']
  377. )
  378. Let's look at some of these more closely.
  379. First is a poll factory method, ``create_poll``, to take some repetition out
  380. of the process of creating polls.
  381. ``test_index_view_with_no_polls`` doesn't create any polls, but checks the
  382. message: "No polls are available." and verifies the ``latest_poll_list`` is
  383. empty. Note that the :class:`django.test.TestCase` class provides some
  384. additional assertion methods. In these examples, we use
  385. :meth:`~django.test.TestCase.assertContains()` and
  386. :meth:`~django.test.TestCase.assertQuerysetEqual()`.
  387. In ``test_index_view_with_a_past_poll``, we create a poll and verify that it
  388. appears in the list.
  389. In ``test_index_view_with_a_future_poll``, we create a poll with a ``pub_date``
  390. in the future. The database is reset for each test method, so the first poll is
  391. no longer there, and so again the index shouldn't have any polls in it.
  392. And so on. In effect, we are using the tests to tell a story of admin input
  393. and user experience on the site, and checking that at every state and for every
  394. new change in the state of the system, the expected results are published.
  395. Testing the ``DetailView``
  396. --------------------------
  397. What we have works well; however, even though future polls don't appear in the
  398. *index*, users can still reach them if they know or guess the right URL. So we
  399. need similar constraints in the ``DetailViews``, by adding::
  400. queryset=Poll.objects.filter(pub_date__lte=timezone.now)
  401. to them - for example::
  402. url(r'^(?P<pk>\d+)/$',
  403. DetailView.as_view(
  404. queryset=Poll.objects.filter(pub_date__lte=timezone.now),
  405. model=Poll,
  406. template_name='polls/detail.html'),
  407. name='detail'),
  408. and of course, we will add some tests, to check that a ``Poll`` whose
  409. ``pub_date`` is in the past can be displayed, and that one with a ``pub_date``
  410. in the future is not::
  411. class PollIndexDetailTests(TestCase):
  412. def test_detail_view_with_a_future_poll(self):
  413. """
  414. The detail view of a poll with a pub_date in the future should
  415. return a 404 not found.
  416. """
  417. future_poll = create_poll(question='Future poll.', days=5)
  418. response = self.client.get(reverse('polls:detail', args=(future_poll.id,)))
  419. self.assertEqual(response.status_code, 404)
  420. def test_detail_view_with_a_past_poll(self):
  421. """
  422. The detail view of a poll with a pub_date in the past should display
  423. the poll's question.
  424. """
  425. past_poll = create_poll(question='Past Poll.', days=-5)
  426. response = self.client.get(reverse('polls:detail', args=(past_poll.id,)))
  427. self.assertContains(response, past_poll.question, status_code=200)
  428. Ideas for more tests
  429. --------------------
  430. We ought to add similar ``queryset`` arguments to the other ``DetailView``
  431. URLs, and create a new test class for each view. They'll be very similar to
  432. what we have just created; in fact there will be a lot of repetition.
  433. We could also improve our application in other ways, adding tests along the
  434. way. For example, it's silly that ``Polls`` can be published on the site that
  435. have no ``Choices``. So, our views could check for this, and exclude such
  436. ``Polls``. Our tests would create a ``Poll`` without ``Choices`` and then test
  437. that it's not published, as well as create a similar ``Poll`` *with*
  438. ``Choices``, and test that it *is* published.
  439. Perhaps logged-in admin users should be allowed to see unpublished ``Polls``,
  440. but not ordinary visitors. Again: whatever needs to be added to the software to
  441. accomplish this should be accompanied by a test, whether you write the test
  442. first and then make the code pass the test, or work out the logic in your code
  443. first and then write a test to prove it.
  444. At a certain point you are bound to look at your tests and wonder whether your
  445. code is suffering from test bloat, which brings us to:
  446. When testing, more is better
  447. ============================
  448. It might seem that our tests are growing out of control. At this rate there will
  449. soon be more code in our tests than in our application, and the repetition
  450. is unaesthetic, compared to the elegant conciseness of the rest of our code.
  451. **It doesn't matter**. Let them grow. For the most part, you can write a test
  452. once and then forget about it. It will continue performing its useful function
  453. as you continue to develop your program.
  454. Sometimes tests will need to be updated. Suppose that we amend our views so that
  455. only ``Polls`` with ``Choices`` are published. In that case, many of our
  456. existing tests will fail - *telling us exactly which tests need to be amended to
  457. bring them up to date*, so to that extent tests help look after themselves.
  458. At worst, as you continue developing, you might find that you have some tests
  459. that are now redundant. Even that's not a problem; in testing redundancy is
  460. a *good* thing.
  461. As long as your tests are sensibly arranged, they won't become unmanageable.
  462. Good rules-of-thumb include having:
  463. * a separate ``TestClass`` for each model or view
  464. * a separate test method for each set of conditions you want to test
  465. * test method names that describe their function
  466. Further testing
  467. ===============
  468. This tutorial only introduces some of the basics of testing. There's a great
  469. deal more you can do, and a number of very useful tools at your disposal to
  470. achieve some very clever things.
  471. For example, while our tests here have covered some of the internal logic of a
  472. model and the way our views publish information, you can use an "in-browser"
  473. framework such as Selenium_ to test the way your HTML actually renders in a
  474. browser. These tools allow you to check not just the behavior of your Django
  475. code, but also, for example, of your JavaScript. It's quite something to see
  476. the tests launch a browser, and start interacting with your site, as if a human
  477. being were driving it! Django includes :class:`~django.test.LiveServerTestCase`
  478. to facilitate integration with tools like Selenium.
  479. If you have a complex application, you may want to run tests automatically
  480. with every commit for the purposes of `continuous integration`_, so that
  481. quality control is itself - at least partially - automated.
  482. A good way to spot untested parts of your application is to check code
  483. coverage. This also helps identify fragile or even dead code. If you can't test
  484. a piece of code, it usually means that code should be refactored or removed.
  485. Coverage will help to identify dead code. See
  486. :ref:`topics-testing-code-coverage` for details.
  487. :doc:`Testing Django applications </topics/testing/index>` has comprehensive
  488. information about testing.
  489. .. _Selenium: http://seleniumhq.org/
  490. .. _continuous integration: http://en.wikipedia.org/wiki/Continuous_integration
  491. What's next?
  492. ============
  493. The beginner tutorial ends here for the time being. In the meantime, you might
  494. want to check out some pointers on :doc:`where to go from here
  495. </intro/whatsnext>`.
  496. If you are familiar with Python packaging and interested in learning how to
  497. turn polls into a "reusable app", check out :doc:`Advanced tutorial: How to
  498. write reusable apps</intro/reusable-apps>`.