Easy Tips to Parametrize your Pytest

by:

Python

This tutorial contains some useful and simple tips to parametrize your pytest functions. We all know that writing a test for a function that you have written is very important. Here, we show how to make it much simpler using Pytest Parametrize.

1. Tested Function

First and foremost, I wrote a simple function that sums all the numbers from a list and returns the sum. This function will be tested here.

# !/usr/bin/env python3
# -*- coding: utf-8 -*-


def sum_number_list(list_numbers):
    """Loop over list with numbers and returns the sum of numbers.

    Args:
        list_numbers (list): List with numbers.

    Returns:
        float: Sum of numbers in list_numbers

    """
    total_sum = 0.0
    for n in list_numbers:
        total_sum += n

    return total_sum

2. Non-Parametrize Test

Next, here is an example of a simple test function where for every test assert, we need to call the function, pass the parameter and the results to be asserted.

import pytest

from my_sum_number_list import sum_number_list


def test_sum_number_list_using_non_parametrize():
    # test 1
    assert sum_number_list([1, 2, 3, 3]) == 9

    # test 2
    assert sum_number_list([1]) == 1

    # test 3
    assert sum_number_list([1, 2]) == 3

    # test 4
    assert sum_number_list([1, 3]) == 4

    # test 5
    assert sum_number_list([2]) == 2

3. Parametrize Test

Now, you can see the parametrized test is much cleaner. We don’t need to call the tested function multiple times. All we need is to define the parameters and results and only call the function once.

import pytest

from my_sum_number_list import sum_number_list


@pytest.mark.parametrize("list_numbers, result", [
    # test 1
    ([1, 2, 3, 3], 9),

    # test 2
    ([1], 1),

    # test 3
    ([1, 2], 3),

    # test 4
    ([1, 3], 4),

    # test 5
    ([2], 2)
])
def test_sum_number_list_using_parametrize(list_numbers, result):
    assert sum_number_list(list_numbers) == result

Finally, we can run the pytest -vv, and pass all the tests

platform darwin -- Python 3.7.4, pytest-5.2.3, py-1.8.0, pluggy-0.13.0 --
cachedir: .pytest_cache
plugins: cov-2.8.1
collected 6 items                                                                                                                                                                                                    

test_my_function.py::test_sum_number_list_using_parametrize[list_numbers0-9] PASSED                                                                                                                            [ 16%]
test_my_function.py::test_sum_number_list_using_parametrize[list_numbers1-1] PASSED                                                                                                                            [ 33%]
test_my_function.py::test_sum_number_list_using_parametrize[list_numbers2-3] PASSED                                                                                                                            [ 50%]
test_my_function.py::test_sum_number_list_using_parametrize[list_numbers3-4] PASSED                                                                                                                            [ 66%]
test_my_function.py::test_sum_number_list_using_parametrize[list_numbers4-2] PASSED                                                                                                                            [ 83%]
test_my_function.py::test_sum_number_list_using_non_parametrize PASSED                                                                                                                                         [100%]

================================================================================================= 6 passed in 0.03s ==================================================================================================

More Resources

Here are two of my favorite Pytest Books in case you want to learn more about it.

Conclusion

Hopefully, you learned how much cleaner is to have a pytest using Parametrize vs when not using it.

Related Posts