Reputation: 23
I want to test the reachability of home page of a demo app.
So I do this:
from django.test import TestCase
class TestHomePageView(TestCase):
def test_reachable_home(self):
response = self.client.get('/home/')
self.assertEqual(response.status_code, 200)
and the views.py
from django.shortcuts import render
def home_view(request):
return render(request, 'home.html', {})
home.html
is a simple one:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
Hi, circleci and django
</body>
</html>
and urls.py
:
from django.contrib import admin
from django.urls import path
from demo_app.views import home_view
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', home_view)
]
It's a really simple demo app.
What I am curious is "Why I can test the status code without running django server?"
Just simply $ python manage.py test
without $ python manage.py runserver
and get the test result:
$ python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.011s
OK
Destroying test database for alias 'default'...
Any idea or suggestion is welcome, thanks.
Upvotes: 0
Views: 1545
Reputation: 2721
Testing allows you to modify your code with more confidence: tests are ok, you modify your code, if tests are still ok, then, you didn't break anything.
Thus, you want to be able to execute your tests often and fast, so you can modify your code more easily, with more confidence.
Framworks and languages provide means of testing that are the lighter possible, so tests are more flexible and runs faster.
Also, you want to test your code, not others, if your tests runs through http, you also test your http server.
Moreover, HTTP, is a transport protocol, we don't need a transport protocol to provide arguments to functions, to check it behaves as intended.
To sum up, you want to be able to run your test often, the fastest it is, the more you are able to run your tests, plus, you want to test only your code, not the code of your webserver.
Upvotes: 1