SlideShare a Scribd company logo
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
About me:
CEO, Ashpot
Software Engineer
Author, and Tech Blogger
Masters in Computer Science,
Nnamdi Azikiwe University
CS Lecturer, Abia State Polytechnic
Community Involvement
• Lead, Python
Abia
• Mentor, PyData
Aba
• Board Member,
Python NIgeria
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
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)
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
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.
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
•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
•SimpletestCase - write simple test that
does not involve database queries by
default
•TransactionTestCase - an extension of
SimpleTestCase that includes supports
for database transactions
• 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
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
Instance from
one of the test
classes form
our Open
Source
project:
Get-A-Doc
Source Code:
https://github.com/Python-Abia/Get-a-Doc
Twitter: @AbiaPython
Facebook: @PythonAbia
An open source platform for patients to connect to licensed medical practitioners and doctors for
free medical advise and possible diagnosis. Aimed at improving access to FREE medical care in
developing countries
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.
• 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.
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.

More Related Content

PPT
Testing In Django
PPTX
Express JS Rest API Tutorial
PPSX
API Test Automation
PPTX
Nodejs functions & modules
PPTX
Unit Tests And Automated Testing
PPTX
Spring data jpa
PPTX
POSTMAN.pptx
Testing In Django
Express JS Rest API Tutorial
API Test Automation
Nodejs functions & modules
Unit Tests And Automated Testing
Spring data jpa
POSTMAN.pptx

What's hot (20)

DOCX
Api testing bible using postman
PPTX
Unit Testing And Mocking
PPTX
API Test Automation Using Karate (Anil Kumar Moka)
PDF
API Testing. Streamline your testing process.
PDF
Unit Testing in Angular
PPTX
C# Async Await
PPTX
Express js
PDF
Functional Tests Automation with Robot Framework
PDF
Espresso testing
PPTX
Rest assured
PPT
Postman.ppt
PPTX
Asynchronous programming
PPTX
How to select the right automated testing tool
PPTX
API Automation Testing Using RestAssured+Cucumber
PDF
Spring boot introduction
PDF
Python Testing 101 with Selenium
PPTX
Building a REST Service in minutes with Spring Boot
PPTX
Node js introduction
PDF
Spring Boot
PDF
Katalon Studio - Successful Test Automation for both Testers and Developers
Api testing bible using postman
Unit Testing And Mocking
API Test Automation Using Karate (Anil Kumar Moka)
API Testing. Streamline your testing process.
Unit Testing in Angular
C# Async Await
Express js
Functional Tests Automation with Robot Framework
Espresso testing
Rest assured
Postman.ppt
Asynchronous programming
How to select the right automated testing tool
API Automation Testing Using RestAssured+Cucumber
Spring boot introduction
Python Testing 101 with Selenium
Building a REST Service in minutes with Spring Boot
Node js introduction
Spring Boot
Katalon Studio - Successful Test Automation for both Testers and Developers
Ad

Similar to A Beginer's Guide to testing in Django (20)

PPTX
Test Driven Development
PPS
Unit Testing
PDF
stackconf 2024 | Test like a ninja with Go by Ivan Presenti.pdf
PPS
Why Unit Testingl
PPS
Why Unit Testingl
PPS
Why unit testingl
PPTX
JavaScript Unit Testing
PDF
May: Automated Developer Testing: Achievements and Challenges
PPTX
Episode 5 - Writing unit tests in Salesforce
PDF
An Introduction to Test Driven Development
PPTX
Unit tests & TDD
PDF
Keyword Driven Testing
PPTX
Unit testing & TDD concepts with best practice guidelines.
PDF
Test Driven Development Introduction
ODP
Writing useful automated tests for the single page applications you build
PPTX
TDD Best Practices
PDF
Testing Experience - Evolution of Test Automation Frameworks
PPTX
Unit Testing
PPTX
Unit testing
PDF
TDD for Testers Workshop
Test Driven Development
Unit Testing
stackconf 2024 | Test like a ninja with Go by Ivan Presenti.pdf
Why Unit Testingl
Why Unit Testingl
Why unit testingl
JavaScript Unit Testing
May: Automated Developer Testing: Achievements and Challenges
Episode 5 - Writing unit tests in Salesforce
An Introduction to Test Driven Development
Unit tests & TDD
Keyword Driven Testing
Unit testing & TDD concepts with best practice guidelines.
Test Driven Development Introduction
Writing useful automated tests for the single page applications you build
TDD Best Practices
Testing Experience - Evolution of Test Automation Frameworks
Unit Testing
Unit testing
TDD for Testers Workshop
Ad

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Machine learning based COVID-19 study performance prediction
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPT
Teaching material agriculture food technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Cloud computing and distributed systems.
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
KodekX | Application Modernization Development
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Per capita expenditure prediction using model stacking based on satellite ima...
Machine learning based COVID-19 study performance prediction
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Teaching material agriculture food technology
MYSQL Presentation for SQL database connectivity
Unlocking AI with Model Context Protocol (MCP)
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Empathic Computing: Creating Shared Understanding
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Diabetes mellitus diagnosis method based random forest with bat algorithm
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Cloud computing and distributed systems.
cuic standard and advanced reporting.pdf
Encapsulation_ Review paper, used for researhc scholars
KodekX | Application Modernization Development
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...

A Beginer's Guide to testing 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
  • 3. Community Involvement • Lead, Python Abia • Mentor, PyData Aba • Board Member, Python NIgeria
  • 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
  • 13. Instance from one of the test classes form our Open Source project: Get-A-Doc
  • 14. Source Code: https://github.com/Python-Abia/Get-a-Doc Twitter: @AbiaPython Facebook: @PythonAbia An open source platform for patients to connect to licensed medical practitioners and doctors for free medical advise and possible diagnosis. Aimed at improving access to FREE medical care in developing countries
  • 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.