SlideShare a Scribd company logo
INTRODUCTION
UNIT TESTING IN PYTHON
TESTING
• Testing is finding out how well something works.
How do you test whether a new table you bought is strong enough?
DIFFERENT TYPES OFTESTS?
• Unit tests
• Integration tests
• Functional tests
• Non function tests
• UAT (User Acceptance)
• Stress test
• Regression tests
And many more.. (100+ types)
UNIT TEST
• Why?
- To detect changes that may break a design contract
- To reduces defects in the newly developed features
- To verify the accuracy/functionality of a unit.
Two popular ways to do it in python
unittest pytest
To write better code.
• How?
By testing individual units/components of the app/software.
A unit is the smallest testable part of any software.
WHY PYTEST?
• Popular
• Concise
• Has helpful assert messages
• Powerful fixture
• etc .. (Don’t ask me what more!)
INSTALL & SETUP
• To install pytest -
pip install pytest
• unittest module is part of python3
• Coverage module –
pip install coverage
OR
pip install pytest-cov
• Run tests –
• pytest arg1 arg2 arg3
• Run coverage –
• coverage run -m pytest arg1 arg2 arg3
OR
• pytest --cov=src/ tests/
PYTEST – TEST DISCOVERY
• Collection starts from the cmd line args which maybe directories, files or test ids.
• Recurse into directories
• Files with *_test.py or test_*.py
• Test prefixed test classes without an init method
• test_ prefixed methods within them
PYTEST – FEW OPTIONS
• -r : short test summary info
and its variants,
f - failed
E - error
s - skipped
x - xfailed
X - xpassed
p - passed
P - passed with output
• Special characters for (de)selection of groups:
a - all except pP
A - all
N - none, this can be used to display nothing (since fE is the default)
• --pdb : PDB (Python Debugger) on failures
• setUp() - to set up initial state for all test methods
• tearDown() – to clean-up after a test method runs.
• setUpClass()
• tearDownClass()
• run() – Define the order of running the test cases
• skipTest(reason)
• subTest(msg=None, **params)
UNITTEST - Unit testing framework
MOCKING & PATCHING
• Mock and MagicMock objects create all attributes and methods as you access them and
store details of how they have been used.
• The patch() decorator / context manager makes it easy to mock classes or objects in a
module under test.
FIXTURES
• pytest fixtures offer dramatic improvements over the classic xUnit style of setup/teardown
functions:
• fixtures have explicit names and are activated by declaring their use from test functions, modules,
classes or whole projects.
• fixtures are implemented in a modular manner, as each fixture name triggers a fixture
function which can itself use other fixtures.
• fixture management scales from simple unit to complex functional testing, allowing to parametrize
fixtures and tests according to configuration and component options, or to re-use fixtures across
function, class, module or whole test session scopes.
THANKYOU

More Related Content

PPTX
Introduction to unit testing in python
PPTX
unittestinginpythonfor-PYDevelopers.pptx
ODT
Testing in-python-and-pytest-framework
PDF
Write unit test from scratch
PDF
Py.test
PDF
New and improved: Coming changes to the unittest module
PPTX
2.Python_Testing_Using_PyUnit_PyTest.pptx
PDF
PresentationqwertyuiopasdfghUnittest.pdf
Introduction to unit testing in python
unittestinginpythonfor-PYDevelopers.pptx
Testing in-python-and-pytest-framework
Write unit test from scratch
Py.test
New and improved: Coming changes to the unittest module
2.Python_Testing_Using_PyUnit_PyTest.pptx
PresentationqwertyuiopasdfghUnittest.pdf

Similar to 2.Python_Unit _Testing_Using_PyUnit_Pytest.pptx (20)

PPTX
Testing in Python: doctest and unittest
PPT
Python testing
PPTX
Upstate CSCI 540 Unit testing
PDF
Effective testing with pytest
PDF
Python testing-frameworks overview
ODP
Automated testing in Python and beyond
 
PPTX
Testing Django APIs
PDF
Unit Testing in Python
PDF
Testing Django Applications
PDF
DIY in 5 Minutes: Testing Django App with Pytest
PDF
Quality of life through Unit Testing
PDF
pytest로 파이썬 코드 테스트하기
PPTX
Unit testing and mocking in Python - PyCon 2018 - Kenya
PDF
Python Advanced – Building on the foundation
PDF
Writing tests
PDF
Debugging 2013- Thomas Ammitzboell-Bach
PDF
Python testing like a pro by Keith Yang
PDF
Test your code
PDF
Unit Testing in Software Development: Why It Matters and How to Do It Right
PDF
The Future is Now: Writing Automated Tests To Grow Your Code
Testing in Python: doctest and unittest
Python testing
Upstate CSCI 540 Unit testing
Effective testing with pytest
Python testing-frameworks overview
Automated testing in Python and beyond
 
Testing Django APIs
Unit Testing in Python
Testing Django Applications
DIY in 5 Minutes: Testing Django App with Pytest
Quality of life through Unit Testing
pytest로 파이썬 코드 테스트하기
Unit testing and mocking in Python - PyCon 2018 - Kenya
Python Advanced – Building on the foundation
Writing tests
Debugging 2013- Thomas Ammitzboell-Bach
Python testing like a pro by Keith Yang
Test your code
Unit Testing in Software Development: Why It Matters and How to Do It Right
The Future is Now: Writing Automated Tests To Grow Your Code
Ad

More from Ganesh Bhosale (20)

DOCX
3.AWR and ASH Reportsfor Oracle Tuning.docx
DOCX
Step by stepDoc for Oracle TuningsandAWR.docx
PPTX
1.Python_Testing_Using_PyUnit_Pytest.pptx
PPTX
awsfundamentals1_cloud_Infrastructure.pptx
PPTX
Generators-in-Python-for-Developers.pptx
PPTX
Advance-Python-Iterators-for-developers.pptx
PPTX
The ES Library for JavaScript Developers
PPTX
Git Repository for Developers working in Various Locations
PPTX
4.Problem Solving Techniques and Data Structures.pptx
PPTX
3.Problem Solving Techniques and Data Structures.pptx
PPTX
2.Problem Solving Techniques and Data Structures.pptx
PPTX
1. Problem Solving Techniques and Data Structures.pptx
PPTX
SQL-queries-for-Data-Analysts-Updated.pptx
PPTX
javascriptbasicsPresentationsforDevelopers
PPTX
Cloud-Architecture-Technology-Deovps-Eng
PDF
Backup-and-Recovery Procedures decribed in AWS
PPTX
KMSUnix and Linux.pptx
PPT
RDBMS_Concept.ppt
PPTX
CLI.pptx
DOCX
Exam_Questions.docx
3.AWR and ASH Reportsfor Oracle Tuning.docx
Step by stepDoc for Oracle TuningsandAWR.docx
1.Python_Testing_Using_PyUnit_Pytest.pptx
awsfundamentals1_cloud_Infrastructure.pptx
Generators-in-Python-for-Developers.pptx
Advance-Python-Iterators-for-developers.pptx
The ES Library for JavaScript Developers
Git Repository for Developers working in Various Locations
4.Problem Solving Techniques and Data Structures.pptx
3.Problem Solving Techniques and Data Structures.pptx
2.Problem Solving Techniques and Data Structures.pptx
1. Problem Solving Techniques and Data Structures.pptx
SQL-queries-for-Data-Analysts-Updated.pptx
javascriptbasicsPresentationsforDevelopers
Cloud-Architecture-Technology-Deovps-Eng
Backup-and-Recovery Procedures decribed in AWS
KMSUnix and Linux.pptx
RDBMS_Concept.ppt
CLI.pptx
Exam_Questions.docx
Ad

Recently uploaded (20)

PDF
Encapsulation theory and applications.pdf
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Tartificialntelligence_presentation.pptx
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Getting Started with Data Integration: FME Form 101
PDF
MIND Revenue Release Quarter 2 2025 Press Release
Encapsulation theory and applications.pdf
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
SOPHOS-XG Firewall Administrator PPT.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation_ Review paper, used for researhc scholars
Per capita expenditure prediction using model stacking based on satellite ima...
Big Data Technologies - Introduction.pptx
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Tartificialntelligence_presentation.pptx
Group 1 Presentation -Planning and Decision Making .pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Network Security Unit 5.pdf for BCA BBA.
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25-Week II
MYSQL Presentation for SQL database connectivity
Getting Started with Data Integration: FME Form 101
MIND Revenue Release Quarter 2 2025 Press Release

2.Python_Unit _Testing_Using_PyUnit_Pytest.pptx

  • 2. TESTING • Testing is finding out how well something works. How do you test whether a new table you bought is strong enough?
  • 3. DIFFERENT TYPES OFTESTS? • Unit tests • Integration tests • Functional tests • Non function tests • UAT (User Acceptance) • Stress test • Regression tests And many more.. (100+ types)
  • 4. UNIT TEST • Why? - To detect changes that may break a design contract - To reduces defects in the newly developed features - To verify the accuracy/functionality of a unit. Two popular ways to do it in python unittest pytest To write better code. • How? By testing individual units/components of the app/software. A unit is the smallest testable part of any software.
  • 5. WHY PYTEST? • Popular • Concise • Has helpful assert messages • Powerful fixture • etc .. (Don’t ask me what more!)
  • 6. INSTALL & SETUP • To install pytest - pip install pytest • unittest module is part of python3 • Coverage module – pip install coverage OR pip install pytest-cov • Run tests – • pytest arg1 arg2 arg3 • Run coverage – • coverage run -m pytest arg1 arg2 arg3 OR • pytest --cov=src/ tests/
  • 7. PYTEST – TEST DISCOVERY • Collection starts from the cmd line args which maybe directories, files or test ids. • Recurse into directories • Files with *_test.py or test_*.py • Test prefixed test classes without an init method • test_ prefixed methods within them
  • 8. PYTEST – FEW OPTIONS • -r : short test summary info and its variants, f - failed E - error s - skipped x - xfailed X - xpassed p - passed P - passed with output • Special characters for (de)selection of groups: a - all except pP A - all N - none, this can be used to display nothing (since fE is the default) • --pdb : PDB (Python Debugger) on failures
  • 9. • setUp() - to set up initial state for all test methods • tearDown() – to clean-up after a test method runs. • setUpClass() • tearDownClass() • run() – Define the order of running the test cases • skipTest(reason) • subTest(msg=None, **params) UNITTEST - Unit testing framework
  • 10. MOCKING & PATCHING • Mock and MagicMock objects create all attributes and methods as you access them and store details of how they have been used. • The patch() decorator / context manager makes it easy to mock classes or objects in a module under test.
  • 11. FIXTURES • pytest fixtures offer dramatic improvements over the classic xUnit style of setup/teardown functions: • fixtures have explicit names and are activated by declaring their use from test functions, modules, classes or whole projects. • fixtures are implemented in a modular manner, as each fixture name triggers a fixture function which can itself use other fixtures. • fixture management scales from simple unit to complex functional testing, allowing to parametrize fixtures and tests according to configuration and component options, or to re-use fixtures across function, class, module or whole test session scopes.