SlideShare a Scribd company logo
5
Most read
6
Most read
7
Most read
D. C. Sherman
A UnitTesting FrameworkA UnitTesting Framework
for Pythonfor Python
Traditional approach:
 Plan out the tests on paper
 Manually conduct the tests
 If there is a failure, fix the code and repeat
the tests
How to test your code?How to test your code?
Before writing any Python code you first write an
automated test, with particular focus on the correct
input and output for your code. At this stage you are
not concerned with how you implement your code.
 Run your test and let it fail.This is OK, as you have yet to
implement your algorithm.
 Begin coding your algorithm.
 Run the test again: if it fails, revise the code.
 Once the code passes the test, move onto the next
algorithm.
Test driven development (TDD)Test driven development (TDD)
 Consider what the function should do
 Consider what inputs are required
 Consider what outputs are expected
Planning your testPlanning your test
1.import unittest
2.from Task_1 import *
3.class TestSequenceFunctions(unittest.TestCase):
4. deftest_rollDie(self):
5. maxNumOfSides= 20
6. for die in range(2, maxNumOfSides+1):
7. value = rollDie(die)
8. self.assertTrue(value >= 1)
9. self.assertTrue(value <= maxNumOfSides)
Example Python unit testExample Python unit test
Steps to create a unit testSteps to create a unit test
 Create a new .pyfile for your test code
 Import the Python unittest library
–import unittest
 Import your code to test
–from <file name> import *
 Create a class for your tests by inheriting from
unittest.TestCase
–class <class name> (unittest.TestCase):
 Add one or more new test methods
–def<method name>(self):
 Add assert statements to your method to notify the
system of an error
–self.assertEqual(<variable 1> , <variable 2>)
–self.assertTrue(<condition> )
 Run the test
assert methods available inassert methods available in
Python’s unittest modulePython’s unittest module
Method Checks that
assertEqual(a,b) a == b
assertNotEqual(a,b) a != b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIn(a,b) a in b
assertNotIn(a,b) a not in b
 If an assert method returns false, an error is logged and displayed in PyCharm
 Documentation: https://docs.python.org/3/library/unittest.html

More Related Content

PPTX
Unit Testing with Python
PPTX
Unit Testing And Mocking
PDF
Py.test
PPSX
PDF
JUnit & Mockito, first steps
PPTX
Introduction to JUnit
PDF
Unit Testing in Python
PPTX
TestNG Framework
Unit Testing with Python
Unit Testing And Mocking
Py.test
JUnit & Mockito, first steps
Introduction to JUnit
Unit Testing in Python
TestNG Framework

What's hot (20)

PDF
Mocking in Java with Mockito
PPTX
TDD - Test Driven Development
PDF
Workshop unit test
PPTX
Java Unit Testing
PDF
C++ Unit Test with Google Testing Framework
ODP
Testes pythonicos com pytest
PPS
JUnit Presentation
PDF
Unit testing with JUnit
PPTX
Unit testing JavaScript: Jasmine & karma intro
PPTX
Automation - web testing with selenium
PPTX
Test NG Framework Complete Walk Through
PPTX
TestNG with selenium
PPTX
Unit testing
PPTX
Core java
PDF
An Introduction to Test Driven Development
PDF
Unit testing best practices
PDF
발표자료 1인qa로살아남는6가지방법
PPTX
Unit Testing Concepts and Best Practices
PPTX
An Introduction to Unit Testing
Mocking in Java with Mockito
TDD - Test Driven Development
Workshop unit test
Java Unit Testing
C++ Unit Test with Google Testing Framework
Testes pythonicos com pytest
JUnit Presentation
Unit testing with JUnit
Unit testing JavaScript: Jasmine & karma intro
Automation - web testing with selenium
Test NG Framework Complete Walk Through
TestNG with selenium
Unit testing
Core java
An Introduction to Test Driven Development
Unit testing best practices
발표자료 1인qa로살아남는6가지방법
Unit Testing Concepts and Best Practices
An Introduction to Unit Testing
Ad

Similar to Python unit testing (20)

PPTX
SE2018_Lec 20_ Test-Driven Development (TDD)
PPTX
8-testing.pptx
PDF
SE2_Lec 21_ TDD and Junit
PPT
Unit testing php-unit - phing - selenium_v2
PDF
PresentationqwertyuiopasdfghUnittest.pdf
PPS
J unit presentation
PDF
Test Driven Development
PPT
05 junit
PPTX
Testing in Python: doctest and unittest
PDF
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
PDF
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
PPT
Assessing Unit Test Quality
PPTX
unit 1 (1).pptx
PPTX
Test driven development in .Net - 2010 + Eclipse
PPTX
2.Python_Testing_Using_PyUnit_PyTest.pptx
PPTX
Unit Testing in Java
PPTX
Testing in Python: doctest and unittest (Updated)
PPTX
Unit testing
PPTX
Python: Object-Oriented Testing (Unit Testing)
SE2018_Lec 20_ Test-Driven Development (TDD)
8-testing.pptx
SE2_Lec 21_ TDD and Junit
Unit testing php-unit - phing - selenium_v2
PresentationqwertyuiopasdfghUnittest.pdf
J unit presentation
Test Driven Development
05 junit
Testing in Python: doctest and unittest
ABAPCodeRetreat Frankfurt 2016 - TDD with ABAP
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
Assessing Unit Test Quality
unit 1 (1).pptx
Test driven development in .Net - 2010 + Eclipse
2.Python_Testing_Using_PyUnit_PyTest.pptx
Unit Testing in Java
Testing in Python: doctest and unittest (Updated)
Unit testing
Python: Object-Oriented Testing (Unit Testing)
Ad

More from Darryl Sherman (6)

PPT
Java Naming & Directory Services
PDF
Il 01-search engines
PDF
Il 02-search requeststrings
ODP
PSD to HTML Conversion
ODP
Angular js filters and directives
ODP
Node js lecture
Java Naming & Directory Services
Il 01-search engines
Il 02-search requeststrings
PSD to HTML Conversion
Angular js filters and directives
Node js lecture

Recently uploaded (20)

PDF
Nekopoi APK 2025 free lastest update
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
top salesforce developer skills in 2025.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
medical staffing services at VALiNTRY
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
AI in Product Development-omnex systems
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Digital Strategies for Manufacturing Companies
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Transform Your Business with a Software ERP System
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Nekopoi APK 2025 free lastest update
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Operating system designcfffgfgggggggvggggggggg
top salesforce developer skills in 2025.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
medical staffing services at VALiNTRY
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
AI in Product Development-omnex systems
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Digital Strategies for Manufacturing Companies
Which alternative to Crystal Reports is best for small or large businesses.pdf
Understanding Forklifts - TECH EHS Solution
Odoo POS Development Services by CandidRoot Solutions
2025 Textile ERP Trends: SAP, Odoo & Oracle
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Transform Your Business with a Software ERP System
Adobe Illustrator 28.6 Crack My Vision of Vector Design

Python unit testing

  • 1. D. C. Sherman A UnitTesting FrameworkA UnitTesting Framework for Pythonfor Python
  • 2. Traditional approach:  Plan out the tests on paper  Manually conduct the tests  If there is a failure, fix the code and repeat the tests How to test your code?How to test your code?
  • 3. Before writing any Python code you first write an automated test, with particular focus on the correct input and output for your code. At this stage you are not concerned with how you implement your code.  Run your test and let it fail.This is OK, as you have yet to implement your algorithm.  Begin coding your algorithm.  Run the test again: if it fails, revise the code.  Once the code passes the test, move onto the next algorithm. Test driven development (TDD)Test driven development (TDD)
  • 4.  Consider what the function should do  Consider what inputs are required  Consider what outputs are expected Planning your testPlanning your test
  • 5. 1.import unittest 2.from Task_1 import * 3.class TestSequenceFunctions(unittest.TestCase): 4. deftest_rollDie(self): 5. maxNumOfSides= 20 6. for die in range(2, maxNumOfSides+1): 7. value = rollDie(die) 8. self.assertTrue(value >= 1) 9. self.assertTrue(value <= maxNumOfSides) Example Python unit testExample Python unit test
  • 6. Steps to create a unit testSteps to create a unit test  Create a new .pyfile for your test code  Import the Python unittest library –import unittest  Import your code to test –from <file name> import *  Create a class for your tests by inheriting from unittest.TestCase –class <class name> (unittest.TestCase):  Add one or more new test methods –def<method name>(self):  Add assert statements to your method to notify the system of an error –self.assertEqual(<variable 1> , <variable 2>) –self.assertTrue(<condition> )  Run the test
  • 7. assert methods available inassert methods available in Python’s unittest modulePython’s unittest module Method Checks that assertEqual(a,b) a == b assertNotEqual(a,b) a != b assertTrue(x) bool(x) is True assertFalse(x) bool(x) is False assertIn(a,b) a in b assertNotIn(a,b) a not in b  If an assert method returns false, an error is logged and displayed in PyCharm  Documentation: https://docs.python.org/3/library/unittest.html