Package to add a data_provider decorator to a function, to run the test with different values
Project description
Python Unittest dataprovider
Description
Python package to add a data_provider
decorator to a test function,
to execute the test with different test values / cases.
The test function will be executed for each record, which is defined at the data_provider
.
@data_provider([ # The DataSet as a List
(value1, value2, value3), # Record 1
(value1, value2, value3) # Record 2
(value1, value2, value3) # Record 3
])
The data_provider accepts the following Types as argument
- Function
- Dict
- List
- Tuple
This package is a Python version of the PHP UnitTest dataprovider.
https://phpunit.de/manual/3.7/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers
Installation
pip install sat_unittest_dataprovider
or
python -m pip install sat_unittest_dataprovider
Usage
The test method will be called with the values of the record as arguments
Therefor you need to make sure, to define the same amount of arguments at the test function,
as you defined values at the record.@data_provider([ (arg1, arg2, arg3, arg4) ]) def test_multiply(arg1, arg2, arg3, arg4): ... do someting the arguments ...
Without the unittest data provider you would probably create a test like this:
class Test(TestCase): def test_without_dataprovider(self): test_data = [ (1, 1, '1 * 1 is 1'), (2, 4, '2 * 2 is 4') ] for row in test_data: given_value, expected_result, msg = row # We unpack the tuple here calculated_result = given_value * given_value # Calculation self.assertEqual(expected_result, calculated_result, msg) # The Test
The same test with the data_provider decorator would look like this:
class Test(TestCase): @data_provider([ (1, 1, '1 * 1 is 1'), (2, 4, '2 * 2 is 4') ]) def test_with_dataprovider(self, given_value, expected_result, msg): # We get all values as function arguments calculated_result = given_value * given_value # Calculation self.assertEqual(expected_result, calculated_result, msg) # The TestThis makes the test more readable to others.
Simple example:
@data_provider([
(1, 2, 3, '1 + 2 = 3')
])
def test_simple_example(self, value, value2, expected, msg):
self.assertEqual(expected, value + value2, msg)
At the example above, we define 4 values at the record, we want to test a simple number addition.
The first argumentvalue
will be added to the second argumentvalue2
and we expect,
that the calculated result is the same as defined inexpected
.
The last argumentmsg
is used as a message which will be shown, when the test fails.
Example 1:
DataSet is a List of Tupels
from unittest import TestCase
from sat_unittest_dataprovider import data_provider
class Test(TestCase):
@data_provider([
(1, 1, '1 * 1 is 1'),
(2, 4, '2 * 2 is 4'),
(3, 9, '3 * 3 is 9')
])
def test_multiply(self, given, expected, message):
calculated_result = given * given
self.assertEqual(expected, calculated_result, message)
Example 2:
DataSet is a List with List items
from unittest import TestCase
from sat_unittest_dataprovider import data_provider
class Test(TestCase):
@data_provider([
[1, 1, '1 * 1 is 1'],
[2, 4, '2 * 2 is 4'],
[3, 9, '3 * 3 is 9']
])
def test_multiply(self, given, expected, message):
calculated_result = given * given
self.assertEqual(expected, calculated_result, message)
Example 3:
DataSet is a Function, which should return the test records either as List, Dict or Tuple
from unittest import TestCase
from sat_unittest_dataprovider import data_provider
def my_data_set():
return [
[1, 1, '1 * 1 is 1'],
[2, 4, '2 * 2 is 4'],
[3, 9, '3 * 3 is 9']
]
class Test(TestCase):
@data_provider(my_data_set)
def test_multiply(self, given, expected, message):
calculated_result = given * given
self.assertEqual(expected, calculated_result, message)
@data_provider(my_data_set)
def test_divider(self, divider, given, message):
expected_result = divider # the expected result is the same as the divider
calculated_result = given / divider
self.assertEqual(expected_result, calculated_result) # We don't use the message here, because for this test it doesn't make sense ;-)
In the example above, you can use the data_set function for multiple test cases
to reduce code duplication
Example 4:
DataSet is a seperate file
For bigger tests you can place the provider functions or values in a separate file
test/data_providers.py
def tuples_in_list() -> list[tuple[int, int, int, str]]:
return [
(1, 2, 3, '1 + 2 is 3'),
(2, 2, 4, '2 + 2 is 3')
]
def tuples_in_tuple() -> tuple[tuple[int, int, int, str], ...]:
return (
(1, 2, 3, '1 + 2 is 3'),
(2, 2, 4, '2 + 2 is 3'),
)
def dict_with_tuples() -> dict[str, tuple[int, int, int, str]]:
return {
"record_1": (1, 2, 3, '1 + 2 is 3'),
"record_2": (2, 2, 4, '2 + 2 is 3'),
}
a_dict: dict[str, tuple[int, int, int, str]] = {
"record_1": (1, 2, 3, '1 + 2 is 3'),
"record_2": (2, 2, 4, '2 + 2 is 3'),
}
test/test_readme_examples.py
from unittest import TestCase
from sat_unittest_dataprovider import data_provider
from .data_providers import (
tuples_in_list,
tuples_in_tuple,
dict_with_tuples,
a_dict
)
class Test(TestCase):
@data_provider(tuples_in_list)
def test_addition_with_tuples_in_list(self, value1, value2, expected, msg):
self.assertEqual(expected, value1 + value2, msg)
@data_provider(tuples_in_tuple)
def test_addition_with_tuples_in_tuple(self, value1, value2, expected, msg):
self.assertEqual(expected, value1 + value2, msg)
@data_provider(dict_with_tuples)
def test_addition_with_dict_with_tuples(self, value1, value2, expected, msg):
self.assertEqual(expected, value1 + value2, msg)
@data_provider(a_dict)
def test_addition_with_a_dict(self, value1, value2, expected, msg):
self.assertEqual(expected, value1 + value2, msg)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for sat_unittest_dataprovider-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21285397c92019d89aed20dc9d3dfd1fedcf433f916cb173f5168311c0a3d8da |
|
MD5 | 203d345340698b4705f01d2b11d672b3 |
|
BLAKE2b-256 | 7b0de125dccec55fb9aedee6896e8bf5fe0e21e5fb7d754d93f5822a56d5c21d |
Hashes for sat_unittest_dataprovider-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a820bad3cd69a5d51ab01004fc61bae570ff40059e21043d1143c2e62c0bac0c |
|
MD5 | 987bbf0b076408cc4bbcb27d6b34d0f7 |
|
BLAKE2b-256 | 379b99a41665d441a8faef01ce37647f40e64b6138c63f25cd338f4af3719730 |