Santiago Fraire
Software Engineer
🇦🇷 ➡️ 🇳🇱
It is an essential part of the lifecycle of a project
# unittest
def test_location(self):
location = get_location()
self.assertEqual(location, "bordeaux")
# pytest
def test_location():
location = get_location()
assert location == "bordeaux"
pytest -k isupper tests/
@pytest.mark.skip
@pytest.mark.xfail
pytest -m awesome tests/
@pytest.fixture()
@pytest.fixture(scope="module")
@pytest.fixture()
def new_customer():
def _new_customer(name):
return {"name": name}
return _new_customer
def test_multi_customers(new_customer):
customer_1 = new_customer("jon")
customer_2 = new_customer("mike")
assert customer_1["name"] == "jon"
assert customer_2["name"] == "mike"
factoryboy
@pytest.mark.parametrize(
"current_version, increment,expected",
[
("2.0.0", "PATCH", "2.0.1"),
("2.0.1", "PATCH", "2.0.2"),
("2.0.2", "MINOR", "2.1.0"),
("2.1.0", "MAJOR", "3.0.0"),
],
)
def test_generate_version(current_version, increment, expected):
new_version = generate_version(current_version, increment)
assert new_version == expected
Mocks record how you use them, allowing you to make assertions about what your code has done to them
class Serializer:
def __init__(self, protocol):
self.protocol = protocol
def serialize(self, payload):
return self.protocol.serialize(payload)
mock_protocol = mock.Mock()
s = Serializer(mock_protocol)
payload = {"name": "jon"}
s.serialize(payload)
mock_protocol.serialize.assert_called_with(payload)
No hardcoded protocol
Our mock as protocol
Assert mock was called