Unit Test - koglak/SWE573 GitHub Wiki
-
Smallest testable part of software is called as unit which can individually and independently tested.
-
Unit test can be done manually. As usually it is automated. Automated test advantageous is that we can continue to test our code while we are developing it.
-
Automated tests are piece of codes which make sure that another piece of code is working properly.
-
Unit tests help to fix bugs early in the development cycle and save costs.
-
If we assign something true to assert, it should return true. But if we assert smomething false, we get error. We test our function like that.
- Create an tests directory under your app. Later on, create test.py documents per view, url, model and form as shown below.
-
When you write the tests, you can run them with below code:
python manage.py test <<appname>>
-
You can create more than 1 method under same class in order to run test for different urls.
-
reverse() method finds the url of given view function.
-
assertEquals() compare the functions. If thoese are same sends "TRUE", if not raises an error.
-
If url takes int:pk or str:title, reverse method should be like below:
url = reverse('course_detail', args=['some-slug']) url = reverse('course_enroll',args=[5])
-
Here is an example code:
from django.test import SimpleTestCase from django.urls import reverse, resolve from userprofile.views import user_profile,course_detail,course_enroll
class TestUrls(SimpleTestCase):
def test_user_profile_url_is_resolved(self): url = reverse('user_profile') self.assertEquals(resolve(url).func, user_profile)
-
Client() acts like a dummy web browser and allow us to test our views.
-
self.assertEquals(response.status_code, 200)
, This class of status code indicates that the client's request was successfully received, understood, and accepted.HTTP_200_OK
/ Code Status -
Test Code below returns an error as
TypeError: 'AnonymousUser' object is not iterable
.from django.test import TestCase, Client from django.urls import reverse from userprofile.models import Lecture,Course,Rating,Profile import json from django.contrib.auth.models import User class TestViews(TestCase): def setUp(self): self.client=Client() self.user = User.objects.create_user('testuser', '[email protected]','123456') Profile.objects.create( user = self.user, bio= "Hello" ) def test_user_profile_GET(self): self.client.login(username='testuser', password='123456') response=client.get(reverse('user_profile')) self.assertEquals(response.status_code, 200) self.assertTemplateUsed(response,'userprofile/profile.html')
-
login_required is the solution to avoid AnonymousUsers.
- In user_profile view, Profile model is used. Profile model has user and bio attributes. Test user and new Profile object are created in setup. The test Profile object is used to test user_profile view.
-
It is difficult to test each item in big projects. You can get a report how many item you have covered with tests by using below method.
-
First install coverage.
pip install coverage
-
This will generate a file that coverage.py uses for creating a report.
coverage run manage.py test
-
Get your report:
coverage report