SlideShare a Scribd company logo
2
Most read
8
Most read
10
Most read
Python Unit Testing
By MicroPyramid
Agenda
Unit Testing
Nose
Doc tests
Why Software testing is
important?
To point out the defects during the development phases.
To Ensure that the application works as expected.
Test Driven Development.
The process of implementing code by writing your tests first, seeing them fail, then
writing the code to make the tests pass.
● Write Tests
● Make Them fail
● Write code.
● Make them pass
● Repeat
Python Unit test
Some Important Points
● Every test class must be sub class of unittest.TestCase
● Every test function should start with test name.
● to check for an expected result use assert functions.
● The setUp() method define instructions that will be executed before test
case.
● The tearDown() method define instructions that will be executed after test
case.
● Run Test with python -m unittest -v test_module
● Only test single part of code
Unit Testing with Python
Let’s start
# tests/mul.py
def multiply(a, b):
return a*b
def add(a, b):
return a+b
Test case
import unittest
from mul import multiply
class MultiplyTestCase(unittest.TestCase):
def test_multiplication_with_correct_values(self):
self.assertEqual(multiply(5, 5), 25)
if __name__ == '__main__':
unittest.main()
SetUp() and TearDown()
class MulTestCase(unittest.TestCase):
def setUp(self): # Runs before every test method
self.a = 10
self.b = 20
def test_mult_with_correct_values(self):
self.assertEqual(multiply(self.a, self.b), 200)
def tearDown(self): # runs after every test method
del self.a
del self.b
if __name__ == '__main__':
unittest.main()
Assert functions
● assertEqual(a, b)
● assertNotEqual(a, b)
● assertTrue(x)
● assertFalse(x)
● assertIs(a, b)
● https://docs.python.org/2/library/unittest.html#test-cases
Nose:
$ pip install nose
# Running tests
$ nosetests
Doc Tests:
# tests/mul_dc.py
def multiply(a, b):
"""
>>> multiply(4, 3)
12
"""
return a * b
# running
$ python -m doctest -v file_name
Questions?

More Related Content

PDF
An introduction to Google test framework
Abner Chih Yi Huang
 
PPTX
Python For Data Science - French Course
Haytam ELYOUSSFI
 
PDF
Unit testing best practices
nickokiss
 
PPT
Introduction to Python
amiable_indian
 
PPTX
Automatic Number Plate Recognition (ANPR)
Vidyut Singhania
 
PPTX
Maquinas virtuales
isma2013
 
PPTX
Engineering Physics - Crystal structure - Dr. Victor Vedanayakam.S
Madanapalle Institute of Technology and Science
 
PDF
52 ejercicios resueltos en pseudocodigo
Brivé Soluciones
 
An introduction to Google test framework
Abner Chih Yi Huang
 
Python For Data Science - French Course
Haytam ELYOUSSFI
 
Unit testing best practices
nickokiss
 
Introduction to Python
amiable_indian
 
Automatic Number Plate Recognition (ANPR)
Vidyut Singhania
 
Maquinas virtuales
isma2013
 
Engineering Physics - Crystal structure - Dr. Victor Vedanayakam.S
Madanapalle Institute of Technology and Science
 
52 ejercicios resueltos en pseudocodigo
Brivé Soluciones
 

What's hot (20)

ODP
Python unit testing
Darryl Sherman
 
PPTX
Unit Testing Concepts and Best Practices
Derek Smith
 
ODT
Testing in-python-and-pytest-framework
Arulalan T
 
PPTX
White Box Testing
Alisha Roy
 
PDF
Robot Framework Introduction
Pekka Klärck
 
PPTX
Python in Test automation
Krishnana Sreeraman
 
PPTX
UNIT TESTING PPT
suhasreddy1
 
PDF
What is Python Lambda Function? Python Tutorial | Edureka
Edureka!
 
PDF
Effective testing with pytest
Hector Canto
 
PPTX
Unit Testing And Mocking
Joe Wilson
 
PPSX
Junit
FAROOK Samath
 
PPT
Testing concepts ppt
Rathna Priya
 
PPTX
Robot framework
Rochak Bhalla
 
PDF
Robot framework
Testing World
 
PPTX
Advance OOP concepts in Python
Sujith Kumar
 
PPTX
Robot framework
boriau
 
PPTX
Scripting robot
Chonlasith Jucksriporn
 
PPTX
Unit Testing
Sergey Podolsky
 
PDF
Modern Python Testing
Alexander Loechel
 
PDF
Introduction to Robot Framework
Somkiat Puisungnoen
 
Python unit testing
Darryl Sherman
 
Unit Testing Concepts and Best Practices
Derek Smith
 
Testing in-python-and-pytest-framework
Arulalan T
 
White Box Testing
Alisha Roy
 
Robot Framework Introduction
Pekka Klärck
 
Python in Test automation
Krishnana Sreeraman
 
UNIT TESTING PPT
suhasreddy1
 
What is Python Lambda Function? Python Tutorial | Edureka
Edureka!
 
Effective testing with pytest
Hector Canto
 
Unit Testing And Mocking
Joe Wilson
 
Testing concepts ppt
Rathna Priya
 
Robot framework
Rochak Bhalla
 
Robot framework
Testing World
 
Advance OOP concepts in Python
Sujith Kumar
 
Robot framework
boriau
 
Scripting robot
Chonlasith Jucksriporn
 
Unit Testing
Sergey Podolsky
 
Modern Python Testing
Alexander Loechel
 
Introduction to Robot Framework
Somkiat Puisungnoen
 
Ad

Similar to Unit Testing with Python (20)

PDF
PresentationqwertyuiopasdfghUnittest.pdf
kndemo34
 
PPTX
Test driven development in .Net - 2010 + Eclipse
UTC Fire & Security
 
PDF
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
RubenGray1
 
PDF
Unit Testing in Angular
Knoldus Inc.
 
PDF
Unit testing (eng)
Anatoliy Okhotnikov
 
PDF
SE2_Lec 21_ TDD and Junit
Amr E. Mohamed
 
PPTX
Unit testing
Prabhat Kumar
 
DOCX
Test Driven Development
Anand Kumar Rajana
 
PPTX
SE2018_Lec 20_ Test-Driven Development (TDD)
Amr E. Mohamed
 
PPTX
Unit testing
Pooya Sagharchiha
 
PDF
Android Test Driven Development & Android Unit Testing
mahmoud ramadan
 
PPT
RPG Program for Unit Testing RPG
Greg.Helton
 
PDF
Python and test
Micron Technology
 
PPT
Unit testing
dubbu
 
PDF
Test Driven Development
Kumaresh Chandra Baruri
 
ODP
Beginners - Get Started With Unit Testing in .NET
Baskar K
 
PPTX
Unit tests & TDD
Dror Helper
 
PPTX
Upstate CSCI 540 Unit testing
DanWooster1
 
PDF
Writing Tests with the Unity Test Framework
Peter Kofler
 
PDF
Unit Testing in Software Development: Why It Matters and How to Do It Right
jamescantor38
 
PresentationqwertyuiopasdfghUnittest.pdf
kndemo34
 
Test driven development in .Net - 2010 + Eclipse
UTC Fire & Security
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
RubenGray1
 
Unit Testing in Angular
Knoldus Inc.
 
Unit testing (eng)
Anatoliy Okhotnikov
 
SE2_Lec 21_ TDD and Junit
Amr E. Mohamed
 
Unit testing
Prabhat Kumar
 
Test Driven Development
Anand Kumar Rajana
 
SE2018_Lec 20_ Test-Driven Development (TDD)
Amr E. Mohamed
 
Unit testing
Pooya Sagharchiha
 
Android Test Driven Development & Android Unit Testing
mahmoud ramadan
 
RPG Program for Unit Testing RPG
Greg.Helton
 
Python and test
Micron Technology
 
Unit testing
dubbu
 
Test Driven Development
Kumaresh Chandra Baruri
 
Beginners - Get Started With Unit Testing in .NET
Baskar K
 
Unit tests & TDD
Dror Helper
 
Upstate CSCI 540 Unit testing
DanWooster1
 
Writing Tests with the Unity Test Framework
Peter Kofler
 
Unit Testing in Software Development: Why It Matters and How to Do It Right
jamescantor38
 
Ad

More from MicroPyramid . (8)

PPTX
Custom web application development with Django for startups and Django-CRM intro
MicroPyramid .
 
PPTX
Social login integration
MicroPyramid .
 
PPTX
Packaging and distributing python code to Pypi
MicroPyramid .
 
PPTX
Introduction to react_js
MicroPyramid .
 
PDF
Git & github
MicroPyramid .
 
PPTX
Django elastic beanstalk
MicroPyramid .
 
PPTX
Introduction to Vi
MicroPyramid .
 
PPTX
Hands on django part 1
MicroPyramid .
 
Custom web application development with Django for startups and Django-CRM intro
MicroPyramid .
 
Social login integration
MicroPyramid .
 
Packaging and distributing python code to Pypi
MicroPyramid .
 
Introduction to react_js
MicroPyramid .
 
Git & github
MicroPyramid .
 
Django elastic beanstalk
MicroPyramid .
 
Introduction to Vi
MicroPyramid .
 
Hands on django part 1
MicroPyramid .
 

Recently uploaded (20)

PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PDF
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PDF
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
PPTX
oapresentation.pptx
mehatdhavalrajubhai
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PPTX
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PDF
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PDF
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
PPTX
Explanation about Structures in C language.pptx
Veeral Rathod
 
PDF
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
PDF
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
49785682629390197565_LRN3014_Migrating_the_Beast.pdf
Abilash868456
 
Activate_Methodology_Summary presentatio
annapureddyn
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
advancepresentationskillshdhdhhdhdhdhhfhf
jasmenrojas249
 
oapresentation.pptx
mehatdhavalrajubhai
 
Become an Agentblazer Champion Challenge
Dele Amefo
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
QAware_Mario-Leander_Reimer_Architecting and Building a K8s-based AI Platform...
QAware GmbH
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
What to consider before purchasing Microsoft 365 Business Premium_PDF.pdf
Q-Advise
 
Explanation about Structures in C language.pptx
Veeral Rathod
 
How to Seamlessly Integrate Salesforce Data Cloud with Marketing Cloud.pdf
NSIQINFOTECH
 
Multi-factor Authentication (MFA) requirement for Microsoft 365 Admin Center_...
Q-Advise
 

Unit Testing with Python

  • 1. Python Unit Testing By MicroPyramid
  • 3. Why Software testing is important? To point out the defects during the development phases. To Ensure that the application works as expected.
  • 4. Test Driven Development. The process of implementing code by writing your tests first, seeing them fail, then writing the code to make the tests pass. ● Write Tests ● Make Them fail ● Write code. ● Make them pass ● Repeat
  • 5. Python Unit test Some Important Points ● Every test class must be sub class of unittest.TestCase ● Every test function should start with test name. ● to check for an expected result use assert functions. ● The setUp() method define instructions that will be executed before test case. ● The tearDown() method define instructions that will be executed after test case. ● Run Test with python -m unittest -v test_module ● Only test single part of code
  • 7. Let’s start # tests/mul.py def multiply(a, b): return a*b def add(a, b): return a+b
  • 8. Test case import unittest from mul import multiply class MultiplyTestCase(unittest.TestCase): def test_multiplication_with_correct_values(self): self.assertEqual(multiply(5, 5), 25) if __name__ == '__main__': unittest.main()
  • 9. SetUp() and TearDown() class MulTestCase(unittest.TestCase): def setUp(self): # Runs before every test method self.a = 10 self.b = 20 def test_mult_with_correct_values(self): self.assertEqual(multiply(self.a, self.b), 200) def tearDown(self): # runs after every test method del self.a del self.b if __name__ == '__main__': unittest.main()
  • 10. Assert functions ● assertEqual(a, b) ● assertNotEqual(a, b) ● assertTrue(x) ● assertFalse(x) ● assertIs(a, b) ● https://docs.python.org/2/library/unittest.html#test-cases
  • 11. Nose: $ pip install nose # Running tests $ nosetests
  • 12. Doc Tests: # tests/mul_dc.py def multiply(a, b): """ >>> multiply(4, 3) 12 """ return a * b # running $ python -m doctest -v file_name