Best practices in software engineering

arrays.py
"""
This module contains functions for manipulating and combining Python lists.
"""

def add_arrays(x, y):
    """
    This function adds together each element of the two passed lists.

    Args:
        x (list): The first list to add
        y (list): The second list to add

    Returns:
        list: the pairwise sums of ``x`` and ``y``.
    
    Raises:
        ValueError: If the length of the lists ``x`` and ``y`` are different.

    Examples:
        >>> add_arrays([1, 4, 5], [4, 3, 5])
        [5, 7, 10]
    """

    if len(x) != len(y):
        raise ValueError("Both arrays must have the same length.")

    z = []
    for x_, y_ in zip(x, y):
        z.append(x_ + y_)

    return z
test_arrays.py
import pytest

from arrays import add_arrays

def test_add_arrays_error():
    a = [1, 2, 3]
    b = [4, 5]
    with pytest.raises(ValueError):
        output = add_arrays(a, b)
$
pytest test_arrays.py
=================== test session starts ====================
platform linux -- Python 3.8.5, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/matt/projects/courses/software_engineering_best_practices
plugins: attrib-0.1.3, requests-mock-1.8.0, cov-2.10.1, hypothesis-5.24.2
collected 1 item                                           

test_arrays.py .                                     [100%]

==================== 1 passed in 0.01s =====================
test_arrays.py
import pytest

from arrays import add_arrays

@pytest.mark.parametrize("a, b", [
    ([1, 2, 3], [4, 5]),
    ([1, 2], [4, 5, 6]),
    ([], [4]),
    ([4], []),
])
def test_add_arrays_error(a, b):
    a = [1, 2, 3]
    b = [4, 5]
    with pytest.raises(ValueError):
        output = add_arrays(a, b)
$
pytest test_arrays.py
=================== test session starts ====================
platform linux -- Python 3.8.5, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /home/matt/projects/courses/software_engineering_best_practices
plugins: attrib-0.1.3, requests-mock-1.8.0, cov-2.10.1, hypothesis-5.24.2
collected 4 items                                          

test_arrays.py ....                                  [100%]

==================== 4 passed in 0.01s =====================