PYTHON FOR COMPUTATIONAL
PROBLEM SOLVING
Testing and Debugging
Prof. Sindhu R Pai
PCPS Theory Anchor - 2024
Department of Computer Science and Engineering
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Testing – Pytest
Pytest
• Pytest is a robust testing framework for Python.
• It allows users to write test codes using Python programming language.
• It helps to write tests from simple unit tests to complex functional tests.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Testing - Pytest
Advantages of Pytest
• Free and open source
• Simple syntax - very easy to start with
• Run multiple tests in parallel, which reduces the execution time of the
test suite
• Automatically detect test file and test functions, if not mentioned
explicitly
• Allows to skip a subset of the tests during execution
• Allows to run a subset of the entire test suite
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Testing - Pytest
Features of Pytest
1. Does not require API to use
2. Provides useful plugins
3. Can be written as a function or method
4. Gives useful failure information without the use of debuggers
5. Can be used to run doc tests and unit tests
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Testing - Pytest
Pytest – Environmental Setup
1. Open command prompt
2. Change directory to the location where Python is installed
3. Type command
pip install pytest
4. Confirm the installation
pytest -h
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Testing - Pytest
Pytest – Environmental Setup
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Testing - Pytest
Pytest – Example
1. Create a new directory (say “Automation”) and navigate into the
directory in the command line. import math
2. Create a file [Link] def testsqrt():
num = 25
assert [Link](num) == 5
def testsquare():
num = 7
3. Run the file using the command assert 7*7 == 40
pytest [Link] def testequality():
assert 10 == 11
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Testing - Pytest
Pytest – Example (Output)
7
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Function testing with Doctest
Doctest
• It is a module included in the Python programming language's standard
library.
• It allows the easy generation of tests based on output from the
standard Python interpreter shell.
• It finds patterns in the docstring.
• Docstrings - provides description of a class or a function to provide a
better understanding of the code. Also, used for testing purposes using
doctest module.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Function testing with Doctest
Need for Doctest
• To check that a module’s docstrings are up-to-date (to ensure code still
work as documented).
• To perform regression testing (verifying the changes made to the code
will not impact the existing functionalities of the Software).
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Function testing with Doctest
Steps to write a function with doctest
1. Import the doctest module.
2. Write the function with docstring.
3. Inside the docstring, write the following two lines for testing the
function. If ‘verbose’ is set to False(default),
output will be shown in case of
>>>function_name(args) failure only, not in the case of
success.
Expected Output
4. Write the function logic(Coding).
5. Call the [Link](name= function_name, verbose=True)
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Function testing with Doctest
Example1: Illustrating the testcase-pass
# import testmod for testing a function
from doctest import testmod
# define a function to test
def fact(n):
'''
>>> fact(5)
120 Lines for testing the function.
>>> fact(0)
1
'''
if n==0:
res=1
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Function testing with Doctest
Example1 (Contd…) Output:
Trying:
else: fact(5)
res=n*fact(n-1) Expecting:
return res 120
ok
Trying:
# call the testmod function fact(0)
testmod(name ='fact', verbose = True) Expecting:
1
ok
1 items had no tests:
fact
1 items passed all tests:
2 tests in [Link]
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Function testing with Doctest
Example2: Illustrating the testcase-failed
# import testmod for testing a function
from doctest import testmod
# define a function to test
def fact(n):
'''
>>> fact(5)
120 Lines for testing the function.
>>> fact(0)
1
'''
if n==0:
res=1
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Function testing with Doctest
Example2 (Contd…) Output:
Failed example:
else: fact(5)
res=fact(n-1) #Wrong logic to compute factorial Expected:
120
return res Got:
1
Trying:
# call the testmod function fact(0)
testmod(name ='fact', verbose = True) Expecting:
1
ok
**********************************
**********************************
**
2 items had failures:
2 of 2 in fact
1 of 2 in [Link]
4 tests in 2 items.
1 passed and 1 failed.
***Test Failed*** 1 failure.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
pdb debugger commands
pdb Module
• It is a module with a set of utilities for debugging of Python programs.
• pdb internally uses bdb (basic debugger) and cmd (command
interpreters) modules.
• pdb runs purely in the command line.
• Pdb supports setting breakpoints, stepping through code, source code
listing, viewing stack traces.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
pdb debugger commands
pdb Module
• Pdb debugger can be invoked in two ways
1. Command Line
python -m pdb [Link]
2. Importing pdb module and call pdb.set_trace()
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
pdb debugger commands
• Command Line
1. Create a Python Script def fact(n):
f=1
for i in range(1,n+1):
print (i) [Link]
f=f*i
return f
print("Factorial of 5 =",fact(5))
2. Open Command Prompt
3. Change to the directory location where python is installed
cd C:\Users\SOWMYA SHREE P\AppData\Local\Programs\Python\Python311
3. Type the below command
python -m pdb [Link]
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
pdb debugger commands
Now, type help in front of the debugger prompt to know more about any command.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
pdb debugger commands
19
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
pdb debugger commands
• list command - lists entire code with -> symbol to the left of a line at which
program has halted.
20
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
pdb debugger commands
• step command – move line by line, will cause a program to stop within a
function
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
pdb debugger commands
• next command - move line by line, executes a called function and stops after
it.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
pdb debugger commands
• break command – set breakpoints within a program. Line number must be given.
• continue command – program execution will proceed till it encounters a
breakpoint
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
pdb debugger commands
• break command – Display all break points using break command without line
number
• continue command – program execution will proceed till it encounters a
breakpoint
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
pdb debugger commands
• Using pdb.set_trace()
The Pdb debugger can be used from within Python script also
import pdb [Link]
1. import pdb def fact(n):
f=1
for i in range(1,n+1):
pdb.set_trace()
print (i)
f=f*i
return f
print("Factorial of 5 =",fact(5))
2. Call set_trace function
The behavior of the debugger will be exactly the same as we find it in a
command line environment.
PYTHON FOR COMPUTATIONAL PROBLEM SOLVING
Testing – Pytest, Function testing with Doctest, pdb debugger commands
Summary
• pytest – a tesing framework in Python, helps to write tests from simple unit tests to
complex functional tests.
• doctest - a module that verifies whether the code work as intended. It allows generation
of tests based on output from the standard Python interpreter shell.
• pdb - a module with a set of utilities for debugging of Python programs.
THANK YOU
Department of Computer Science and Engineering
Prof. Sindhu R Pai – sindhurpai@[Link]
Prof. Sowmya Shree P