pip install pytest
pytest -rfE tests.py
from random import randint | |
def hello_world(): | |
return "Hello world !" | |
def increment(value): | |
return increment + 1 | |
def random_int(): | |
return randint(0, 100) | |
def is_even(): | |
value = random_int() | |
if value % 2 == 0: | |
return True | |
else: | |
return False |
from unittest.mock import MagicMock | |
import pytest | |
@pytest.fixture | |
def two(): | |
return 2 | |
@pytest.fixture | |
def random_int_mock(monkeypatch): | |
random_int_mock = MagicMock() | |
monkeypatch.setattr('mon_module.random_int', random_int_mock) | |
return random_int_mock | |
def test_when_hello_world_then_return_expected_message(): | |
expected = "Hello world !" | |
result = hello_world() | |
assert result == expected | |
def test_given_value_when_increment_then_return_incremented_value(two): | |
expected = 3 | |
result = increment(two) | |
assert result == expected | |
def test_given_even_value_from_random_int_when_is_even_then_return_true(random_int_mock): | |
random_int_mock.return_value = 2 | |
result = is_even() | |
assert result is True | |
def test_given_odd_value_from_random_int_when_is_even_then_return_true(random_int_mock): | |
random_int_mock.return_value = 3 | |
result = is_even() | |
assert result is False |