Skip to content

Commit b169572

Browse files
committed
Added the views test case
1 parent e2c673f commit b169572

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

README.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,31 @@ the *Django Admin Interface* like this:
111111

112112
Django Admin Interface running
113113

114+
115+
Running the testing
116+
===================
117+
118+
Running the ``helloworld`` application tests with the following command:
119+
120+
::
121+
122+
$ python3 manage.py test helloworld.tests
123+
124+
At which point you should see:
125+
126+
::
127+
128+
Found 2 test(s).
129+
Creating test database for alias 'default'...
130+
System check identified no issues (0 silenced).
131+
..
132+
----------------------------------------------------------------------
133+
Ran 2 tests in 1.017s
134+
135+
OK
136+
Destroying test database for alias 'default'...
137+
138+
114139
Building with docker
115140
====================
116141

helloworld/tests/__init__.py

Whitespace-only changes.

helloworld/tests/test_views.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from django.test import TestCase
2+
from django.urls import reverse
3+
from django.contrib.auth.models import User
4+
5+
class HelloWorldViewTests(TestCase):
6+
def test_hello_world_view(self):
7+
"""
8+
Test the hello world view returns a 200 status code and the correct content.
9+
"""
10+
response = self.client.get(reverse('index'))
11+
self.assertEqual(response.status_code, 200)
12+
self.assertContains(response, "Hello, world!\n")
13+
14+
def test_admin_view(self):
15+
"""
16+
Test the admin view returns a 200 status code.
17+
"""
18+
# Create a superuser
19+
User.objects.create_superuser(username='admin', email='[email protected]', password='adminpass')
20+
# Log in the superuser
21+
self.client.login(username='admin', password='adminpass')
22+
# Access the admin view
23+
response = self.client.get(reverse('admin:index'))
24+
self.assertEqual(response.status_code, 200)

0 commit comments

Comments
 (0)