testing is an important part of programming and software engineering. In this slide, I share insight on how to get started with writing tests in Django
1. A Beginner's Guide to Testing
in Django
Understanding the importance of Writing tests in a project,
and learning the best way to do it
2. About me:
CEO, Ashpot
Software Engineer
Author, and Tech Blogger
Masters in Computer Science,
Nnamdi Azikiwe University
CS Lecturer, Abia State Polytechnic
4. Outline
• What is testing?
• Why write tests?
• Types of test.
• Django test tools
• Choosing a test case
• Where to put my test
• Extending test with third party tools
5. What is testing?
•Testing is simply the act of writing another code
to ensure that a piece of code is working as
intended!
•WRITE CODE TO KNOW IF MY CODE IS
WORKING! (Sounds like hard work)
6. Why write tests?
•to ensure that your code works as expected the
first time you write it
•to ensure that changes made by refactoring or
modifying old code haven't changed the
behaviour of your app unexpectedly
7. Types of test
Unit Test: tests designed to test one particular function
in your code or app. A test to validate that HTTP
response code 200 is returned when form is submitted
Integration Test: are written to test the functionality of
a combine piece of code. Eg: A test to validate that the
entire user registration process is working.
8. Django Test Tools
The prefered way to write tests in Django is using unit
module built into the Python standard library
The unittest defines tests using a class-based approach
django.test.Testcase unittest.Testcase
9. •A test class is written by subclassing any of
the Django unittest TestCase.
•Django unittest has four basic types of test
cases which you can subclass depending on
the type of test you are writing
10. •SimpletestCase - write simple test that
does not involve database queries by
default
•TransactionTestCase - an extension of
SimpleTestCase that includes supports
for database transactions
11. • TestCase - the most comonly used class for writing
tests that support databse queries. They are also
extension of the TransactionTestCase
• LiveSeverTestCase - this is similar to the
TransactionTestCase except that it has the ability to it
launch a live Django server in the background on
setup, and shuts it down on teardown. This is used for
connecting of automated test clients like Selenium
12. What can I test vs What Should I test?
You can test everything and anything. The rule in
testing is if it can break, then you should test it.
assertEquals(), assertNotEquals(),
assetTemplateUsed(), assertContains(),
assertNotContains(), assertTrue(),
assertFalse(), assertIs(), assertIsNot(),
assertIsNone(), etc
15. Where to put my test
There is the tests.py file
that is created with
every app. That is an
excellent place to put
start putting your tests.
16. • However you should
adopt more constructive
test file struture that is
easy to scale with your
application by creating a
complete test module
and creating different
test files for different
units.
17. Extending test with third party tools
• django-webtest: makes it much easier to write functional
tests and assertions that match the end user’s experience.
Couple these tests with Selenium tests for full coverage on
templates and views.
• coverage: is used for measuring the effectiveness of tests,
showing the percentage of your codebase covered by tests.
• django-discover-runner: helps locate tests if you organize
them in a different way (e.g, outside of tests.py).
• factory_boy: used for populating needed data for testing.