Meeting the Unittest
INTRODUCTION TO TESTING IN PYTHON
Alexander Levin
Data Scientist
Recap of OOP
OOP - programming paradigm based on Example of a Python class:
objects and classes.
class Rectangle:
Class - a template of an object that can # Constructor of Rectangle
def __init__(self, a, b):
contain methods and attributes.
self.a = a
Method - a function or procedure that self.b = b
belongs to a class. # Area method
def get_area(self):
Attribute - a variable that belongs to a return self.a * self.b
class. # Usage example
r = Rectangle(4, 5)
Object - an instance of a class. print(r.get_area())
>> 20
INTRODUCTION TO TESTING IN PYTHON
OOP Inheritance
Classes can inherit properties from other classes.
Put the parent class in the brackets after the name of the new class.
class RedRectangle(Rectangle):
[Link] = 'red'
INTRODUCTION TO TESTING IN PYTHON
What is unittest
unittest - built-in Python framework for test automation (it is installed with Python ).
unittest - not only for unit tests alone.
Based on OOP: each test case is a class, and each test is a method.
Test case - is an instance of testing.
Test suite - is a collection of test cases.
INTRODUCTION TO TESTING IN PYTHON
unittest vs. pytest
unittest pytest
OOP-based - requires to create test Functon-based - searches for scripts and
classes) functions starting with test_
Built-in (is installed with the Python Third-party package (has to be installed
distribution) separately from the Python distribution)
More assertion methods Less assertion methods
INTRODUCTION TO TESTING IN PYTHON
How to create a test with unittest
Test of the exponentiation operator:
import unittest
# Declaring the TestCase class
class TestSquared([Link]):
# Defining the test
def test_negative(self):
[Link]((-3) ** 2, 9)
INTRODUCTION TO TESTING IN PYTHON
Assertion methods
.assertEqual() , .assertNotEqual()
.assertTrue() , .assertFalse()
.assertIs() , .assertIsNone()
.assertIsInstance() , .assertIn()
.assertRaises()
Many others
INTRODUCTION TO TESTING IN PYTHON
Summary
unittest - OOP-based built-in Python framework for test automation
Test case - is a testing instance in unittest
To create a test:
1. Declare a class inheriting from [Link]
2. Define test functions
Assertion methods
INTRODUCTION TO TESTING IN PYTHON
Let's practice!
INTRODUCTION TO TESTING IN PYTHON
CLI Interface
INTRODUCTION TO TESTING IN PYTHON
Alexander Levin
Data Scientist
Example: code
Test of the exponentiation operator:
# test_sqneg.py
import unittest
# Declaring the TestCase class
class TestSquared([Link]):
# Defining the test
def test_negative(self):
[Link]((-3) ** 2, 9)
CLI command:
python3 -m unittest test_sqneg.py
Run Python script test_sqneg.py using module unittest .
INTRODUCTION TO TESTING IN PYTHON
Example: output
The command: python3 -m unittest test_sqneg.py
The test output:
INTRODUCTION TO TESTING IN PYTHON
Keyword argument -k
unittest -k - run test methods and classes that match the pattern or substring
Command: python3 -m unittest -k "SomeStringOrPattern" test_script.py
Example: python3 -m unittest -k "Squared" test_sqneg.py
Output:
1 [Link]
INTRODUCTION TO TESTING IN PYTHON
Fail fast flag -f
unittest -f - stop the test run on the first error or failure.
Command: python3 -m unittest -f test_script.py
Use case example: when all of tests are crucial, like testing the airplane before a flight.
1 [Link]
INTRODUCTION TO TESTING IN PYTHON
Catch flag -c
Catch flag unittest -c - lets to interrupt the test by pushing "Ctrl - C".
If "Ctrl - C"
is pushed once, unittest waits for the current test to end and reports all the results so
far.
is pushed twice, unittest raises the KeyboardInterrupt exception.
Command: python3 -m unittest -c test_script.py
Use case example: when debugging a big test suite
1 [Link]
INTRODUCTION TO TESTING IN PYTHON
Verbose flag -v
unittest -v - run tests with more detail
Command: python3 -m unittest -v test_script.py .
Use case example: debugging purposes
Output example:
1 [Link]
INTRODUCTION TO TESTING IN PYTHON
Summary
Basic command without arguments python3 -m unittest test_script.py
Output in unittest
Keyword argument: python3 -m unittest -k "SomeStringOrPattern" test_script.py
Fail fast flag: python3 -m unittest -f test_script.py
Catch flag: python3 -m unittest -c test_script.py
Verbose flag: python3 -m unittest -v test_script.py
INTRODUCTION TO TESTING IN PYTHON
Let's practice!
INTRODUCTION TO TESTING IN PYTHON
Fixtures in unittest
INTRODUCTION TO TESTING IN PYTHON
Alexander Levin
Data Scientist
Fixtures recap
Fixture
a prepared environment for a test
separate the preparation from the test code
Fixture setup - setting up resources for tests
Fixture teardown - cleaning up ("tearing down") resources that were allocated
Example: preparing the food for a picnic and the cleaning at the end
INTRODUCTION TO TESTING IN PYTHON
Fixtures in the unittest library
Fixture in unittest - the preparaton needed to perform one or more tests
.setUp() - a method called to prepare the test fixture before the actual test
.tearDown() - a method called after the test method to clean the environment
1 [Link]
INTRODUCTION TO TESTING IN PYTHON
Example code
import unittest
class TestLi([Link]):
# Fixture setup method
def setUp(self):
[Link] = [i for i in range(100)]
# Fixture teardown method
def tearDown(self):
[Link]()
# Test method
def test_your_list(self):
[Link](99, [Link])
[Link](100, [Link])
INTRODUCTION TO TESTING IN PYTHON
Capital U and capital D
The correct syntax: setUp with capital U and tearDown with capital D.
class TestLi([Link]):
# Fixture setup method
def setUp(self):
[Link] = [i for i in range(100)]
# Fixture teardown method
def tearDown(self):
[Link]()
INTRODUCTION TO TESTING IN PYTHON
Example output
The command: python3 -m unittest test_in_list.py
Output of a run with a .setUp() and .tearDown() :
INTRODUCTION TO TESTING IN PYTHON
Incorrectly named methods
Output of a run with a .set_up() :
INTRODUCTION TO TESTING IN PYTHON
Summary
Fixture in unittest - the preparation needed to perform one or more tests
To create a fixture:
Implement the .setUp() method
Implement the .tearDown() method
.setUp() - a method called to prepare the test fixture before the actual test.
.tearDown() - a method called after the test method to clean the environment.
INTRODUCTION TO TESTING IN PYTHON
Let's practice!
INTRODUCTION TO TESTING IN PYTHON
Practical examples
INTRODUCTION TO TESTING IN PYTHON
Alexander Levin
Data Scientist
Data and pipeline
Data: salaries in data science. Pipeline: to get the mean salary:
Each row contains information about a data 1. Read the data
science worker with his salary, title and other
2. Filter by employment type
attributes.
3. Get the mean salary
4. Save the results
INTRODUCTION TO TESTING IN PYTHON
Code of the pipeline
import pandas as pd
# Fixture to get the data
@[Link]
def read_df():
return pd.read_csv('ds_salaries.csv')
# Function to filter the data
def filter_df(df):
return df[df['employment_type'] == 'FT']
# Function to get the mean
def get_mean(df):
return df['salary_in_usd'].mean()
INTRODUCTION TO TESTING IN PYTHON
Integration tests
Test cases:
Reading the data
Writing to the file
Code:
def test_read_df(read_df):
# Check the type of the dataframe
assert isinstance(read_df, [Link])
# Check that df contains rows
assert read_df.shape[0] > 0
INTRODUCTION TO TESTING IN PYTHON
Integration tests
Example of checking that Python can create files.
def test_write():
# Opening a file in writing mode
with open('[Link]', 'w') as wfile:
# Writing the text to the file
[Link]('Testing stuff is awesome')
# Checking the file exists
assert [Link]('[Link]')
# Don't forget to clean after yourself
[Link]('[Link]')
INTRODUCTION TO TESTING IN PYTHON
Unit tests
Test cases:
Filtered dataset contains only 'FT' employment type
The get_mean() function returns a number
Code:
def test_units(read_df):
filtered = filter_df(read_df)
assert filtered['employment_type'].unique() == ['FT']
assert isinstance(get_mean(filtered), float)
INTRODUCTION TO TESTING IN PYTHON
Feature tests
Test cases:
The mean is greater than zero
The mean is not bigger than the maximum salary in the dataset
Code:
def test_feature(read_df):
# Filtering the data
filtered = filter_df(read_df)
# Test case: mean is greater than zero
assert get_mean(filtered) > 0
# Test case: mean is not bigger than the maximum
assert get_mean(filtered) <= read_df['salary_in_usd'].max()
INTRODUCTION TO TESTING IN PYTHON
Performance tests
Test cases:
Pipeline execution time from the start to the end
Code:
def test_performance(benchmark, read_df):
# Benchmark decorator
@benchmark
# Function to measure
def get_result():
filtered = filter_df(read_df)
return get_mean(filtered)
INTRODUCTION TO TESTING IN PYTHON
Final test suite
import pytest ## Feature Tests
def test_feature(read_df):
## Integration Tests # Filtering the data
def test_read_df(read_df): filtered = filter_df(read_df)
# Check the type of the dataframe # Test case: mean is greater than zero
assert isinstance(read_df, [Link]) assert get_mean(filtered) > 0
# Check that df contains rows # Test case: mean is not bigger than the maximum
assert read_df.shape[0] > 0 assert get_mean(filtered) <= read_df['salary_in_usd'].max()
def test_write():
with open('[Link]', 'w') as wfile: ## Performance Tests
[Link]('12345') def test_performance(benchmark, read_df):
assert [Link]('[Link]') # Benchmark decorator
[Link]('[Link]') @benchmark
# Function to measure
## Unit Tests def pipeline():
def test_units(read_df): filtered = filter_df(read_df)
filtered = filter_df(read_df) return get_mean(filtered)
assert filtered['employment_type'].unique() == ['FT']
assert isinstance(get_mean(filtered), float)
INTRODUCTION TO TESTING IN PYTHON
Let's practice!
INTRODUCTION TO TESTING IN PYTHON
Congratulations!
INTRODUCTION TO TESTING IN PYTHON
Alexander Levin
Data Scientist
Chapter 1 - Creating tests with pytest
Testing and pytest
CLI: pytest test_script.py
Test markers
INTRODUCTION TO TESTING IN PYTHON
Chapter 2 - Pytest fixtures
Introduction to fixtures
Chain fixtures requests
Fixtures autouse
Fixtures teardown
INTRODUCTION TO TESTING IN PYTHON
Chapter 3 - Basic Testing Types
Unit testing
Feature testing
Integration testing
Performance testing
INTRODUCTION TO TESTING IN PYTHON
Chapter 4 - Writing tests with unittest
Meeting the unittest
import unittest
class TestSquared([Link]):
def test_negative(self):
[Link]((-3) ** 2, 9)
Unittest CLI
Fixtures in unittest
Practical examples
INTRODUCTION TO TESTING IN PYTHON
Congratulations!
INTRODUCTION TO TESTING IN PYTHON