Skip to content

Instantly share code, notes, and snippets.

@KerberosMorphy
Created March 15, 2022 21:26
Show Gist options
  • Save KerberosMorphy/830ddbad325744786b8b57df82f377a0 to your computer and use it in GitHub Desktop.
Save KerberosMorphy/830ddbad325744786b8b57df82f377a0 to your computer and use it in GitHub Desktop.
Tests avec Pytest

Tests avec Pytest

Installer Pytest

pip install pytest

Exécuter les tests

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment