|
1 | | -from django.contrib.auth.models import AnonymousUser |
2 | | -from django.test import TestCase, RequestFactory |
| 1 | +from django.test import TestCase |
3 | 2 |
|
4 | | -from .views import index |
| 3 | +# Create your tests here. |
5 | 4 |
|
6 | 5 |
|
7 | | -class SimpleTest(TestCase): |
8 | | - def setUp(self): |
9 | | - # Every test needs access to the request factory. |
10 | | - self.factory = RequestFactory() |
| 6 | +# Note: The tests below rely upon static assets (for the rendered templates), so require that either: |
| 7 | +# 1. The static assets have been processed - ie: `./manage.py collectstatic` has been run. |
| 8 | +# 2. Or, the tests are run in debug mode (which means WhiteNoise will use auto-refresh mode), |
| 9 | +# using: `./manage.py test --debug-mode` |
| 10 | +class ExampleTest(TestCase): |
| 11 | + def test_index_page(self): |
| 12 | + response = self.client.get("/") |
| 13 | + self.assertContains( |
| 14 | + response, "Getting Started with Python on Heroku", status_code=200 |
| 15 | + ) |
11 | 16 |
|
12 | | - def test_details(self): |
13 | | - # Create an instance of a GET request. |
14 | | - request = self.factory.get("/") |
15 | | - request.user = AnonymousUser() |
| 17 | + def test_db_page(self): |
| 18 | + # Each time the page is requested, the number of recorded greetings increases. |
16 | 19 |
|
17 | | - # Test my_view() as if it were deployed at /customer/details |
18 | | - response = index(request) |
19 | | - self.assertEqual(response.status_code, 200) |
| 20 | + first_response = self.client.get("/db/") |
| 21 | + self.assertEqual(first_response.status_code, 200) |
| 22 | + self.assertEqual(len(first_response.context["greetings"]), 1) |
| 23 | + |
| 24 | + second_response = self.client.get("/db/") |
| 25 | + self.assertEqual(second_response.status_code, 200) |
| 26 | + self.assertEqual(len(second_response.context["greetings"]), 2) |
0 commit comments