Unit Tests why would you need this at all?
1. Unit tests and regression testing!
Gives you confidence of not breaking your previous features by creating new features.
2. Program some more!
When you create unit-tests you have to think-through on the source-code to be proper applied to the tests.
From my own experience I have to confess - there were times when I've changed source-code of application to support properly unit-tests.
3. Time-saver !
You code new feature, you are not sure and have no proper time to test if all of "old" features are working properly.
Then you use your previously created unit-tests and find-out if at the "unit" stage, you got something broke or not.
How to use django integrated tests?
Django integrated tests uses python unittest module. This is setup with integration to django, thanks to which to run initial tests simply:
python manage.py test
But when you find yourself creating multiple django "applications" this will not work. You may need to add something like this:
python manage.py test application_directory
In my project I've added to Makefile :
python bikingendorphines/manage.py test web
How to create unit tests that are integrated with Django Framework?
Code below show how to setup your initial first test. This is placed in "test*.py" files.
""" Unit tests for web-application. """ # disabling because this import will be used in future # pylint: disable=unused-import from django.test import TestCase from .models import User class UserTestCase(TestCase): """ A User-class test-case """ def setUp(self): User.objects.create(name='Bart', weight=80, height=175) User.objects.create(name='Trab', weight=99, height=175) def test_user_bmi(self): """ Tests if users has been created in db """ bart = User.objects.get(name='Bart') trab = User.objects.get(name='Trab') self.assertEqual(bart.bmi(), (80/(175*175)* 10000)) self.assertEqual(trab.bmi(), (99/(175*175)* 10000))
The User class itself I've defined in this way:
class User(models.Model): """ User model for obtaining personal information about biking riders """ name = models.CharField(max_length=50) weight = models.IntegerField(default=0) height = models.IntegerField(default=0) def __unicode__(self): """ Returns User information when using str/printing """ return self.name def bmi(self): """ Body Mass Index calculator simplified to number """ return (self.weight / (self.height * self.height)) * 10000
Code commits done for this post:
LL (Lessons Learned)
1. Allowed_Hosts problem.
I finally was at the stage when I wanted to see if at least the debug page for djagno will popup.
But didn't know that django developers change default behaviour of ALLOWED_HOSTS and now does not add the "star" (as all-hosts allowed)
Since I'm using VM that is simply a server-based - I had to a change allowed_hosts variable at the bikingendorphines/bikingendorphines/settings.py file with:
ALLOWED_HOSTS = [*]
2. Pylint errors.
Pylint was not optimized as I found out at the coding stage with some django - elements i.e.
objects at the Models.
Pylint issue with not recognizing "migration" django scripts and finding lint-errors. I wouldn't mind if I had to manually create this files, but they were made automatically at the
python manage.py makemigrations phase.
Added pylint_django python module using
pip install pylint_django and added
--load-plugins pylint_django to my Makefile task that is used by Travis to complete pylint tests.
.pylintrc file within section
3. Pylint and Django-unit tests errors
I had some issues when integrating together Pylint and Django-Unit tests.
For django-unit test to properly work I had to firstly remove the
__init__.py python file that describes directory as a module - at the root of the
bikingendorhpines project dir.
I had to change pylint to search for particular folders to find lint-errors in web- "application"
What's Next ??
PyReverse to create a PNG files with all classes at Django and our project.
Check what I've got to do in issues of github: ISSUES