Source code for test

#!/bin/python3
"""
Pytest and hypothesis examples.

Run using 'pytest test.py'
"""

import pytest
from hypothesis import given
from hypothesis.strategies import integers, text


# Functions to be tested.
[docs]def add(x): """ Return x +1. >>> add(2) 3 """ return x + 1
[docs]def square(x): """ Square x. >>> square(2) 4 >>> square(-2) 4 """ return x * x
[docs]def mystring(s): """Return s.""" return s + s
# Test using hypothesis data generation
[docs]@given(integers()) def test_add_h(i): """Test add.""" assert add(i) == i + 1
[docs]@given(integers()) def test_square_h(i): """Test square.""" assert square(i) == i * i
[docs]@given(text()) def test_mystring_h(s): """Test string.""" x = mystring(s) assert x == s + s
# Test using pytest parameters
[docs]@pytest.mark.parametrize( "value, result", [(1, 2), (2, 3), (3, 4)], ) def test_add_p(value, result): """Test add.""" assert add(value) == result
[docs]@pytest.mark.parametrize( "value, result", [(1, 1), (2, 4), (3, 9)], ) def test_square_p(value, result): """Test add.""" assert square(value) == result
[docs]@pytest.mark.parametrize( "value, result", [("hi", "hihi"), ("bye", "byebye")], ) def test_mystring_p(value, result): """Test add.""" assert mystring(value) == result