Skip to content

Commit 37f270a

Browse files
authored
Improve example unit tests (#191)
The example unit tests have been rewritten to actually test the app, and so they no longer use unnecessary setup boilerplate.
1 parent b4f9e3f commit 37f270a

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

hello/tests.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1-
from django.contrib.auth.models import AnonymousUser
2-
from django.test import TestCase, RequestFactory
1+
from django.test import TestCase
32

4-
from .views import index
3+
# Create your tests here.
54

65

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+
)
1116

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.
1619

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

Comments
 (0)