Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added views test case #34

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ And later to test the Django Installation is done with the following command:

::

$ python -m django --version
$ python3 -m django --version
5.1.6


Expand Down Expand Up @@ -97,9 +97,9 @@ see the hello world example like this:
.. figure:: https://github.com/django-ve/django-helloworld/raw/master/docs/django_helloword.png
:width: 315px
:align: center
:alt: A Django 'Hello World' program example
:alt: A Django 'Hello World' application example

A Django 'Hello World' program example
A Django 'Hello World' application example

Also you can open in your web browser the URL http://127.0.0.1:8000/admin for access to
the *Django Admin Interface* like this:
Expand All @@ -111,6 +111,31 @@ the *Django Admin Interface* like this:

Django Admin Interface running


Running the testing
===================

Running the ``helloworld`` application tests with the following command:

::

$ python3 manage.py test helloworld.tests

At which point you should see:

::

Found 2 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..
----------------------------------------------------------------------
Ran 2 tests in 1.017s

OK
Destroying test database for alias 'default'...


Building with docker
====================

Expand Down Expand Up @@ -147,6 +172,11 @@ Requesting the URL http://localhost:4000 with the following command:
::

$ curl localhost:4000

At which point you should see:

::

Hello, world!


Expand Down
Empty file added helloworld/tests/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions helloworld/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.test import TestCase
from django.urls import reverse
from django.contrib.auth.models import User

class HelloWorldViewTests(TestCase):
def test_hello_world_view(self):
"""
Test the hello world view returns a 200 status code and the correct content.
"""
response = self.client.get(reverse('index'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Hello, world!\n")

def test_admin_view(self):
"""
Test the admin view returns a 200 status code.
"""
# Create a superuser
User.objects.create_superuser(username='admin', email='[email protected]', password='adminpass')
# Log in the superuser
self.client.login(username='admin', password='adminpass')
# Access the admin view
response = self.client.get(reverse('admin:index'))
self.assertEqual(response.status_code, 200)