Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pytest
- from contextlib import nullcontext as does_not_raise
- from main import Calculator
- from pytest import mark
- class TestCalcFunctions:
- @mark.parametrize(
- "a, b, result, expectation",
- [
- (10, 2, 5, does_not_raise()),
- (50, 2, 25, does_not_raise()),
- (20, 10, 2, does_not_raise()),
- (30, -3, -10, does_not_raise()),
- (10, 0, 0, pytest.raises(ZeroDivisionError)),
- ("hello", 3, 5, pytest.raises(ValueError)),
- (40, "hello", 5, pytest.raises(ValueError)),
- (Calculator, range, 5, pytest.raises(ValueError)),
- (pytest, {3, 3, 3}, 5, pytest.raises(ValueError)),
- ]
- )
- def test_division(self, a, b, result, expectation):
- c = Calculator()
- with expectation:
- assert c.calc(f"{a} / {b}") == result
- @mark.parametrize(
- "a, b, result, expectation",
- [
- (1, 1, 2, does_not_raise()),
- (100, 100, 200, does_not_raise()),
- (-203, 3, -200, does_not_raise()),
- (43, -3, 40, does_not_raise()),
- (-3, -2, -5, does_not_raise()),
- ("hi", -2, 0, pytest.raises(ValueError)),
- (3, "hello", 0, pytest.raises(ValueError)),
- ("ni hao", "bonjour", 0, pytest.raises(ValueError)),
- ("howareyou", "imok", 0, pytest.raises(ValueError)),
- (int, float, 0, pytest.raises(ValueError)),
- ]
- )
- def test_addition(self, a, b, result, expectation):
- c = Calculator()
- with expectation:
- assert c.calc(f"{a} + {b}") == result
- @mark.parametrize(
- "a, b, result, expectation",
- [
- (10, 1, 9, does_not_raise()),
- (100, 14, 86, does_not_raise()),
- (-203, 3, -206, does_not_raise()),
- (0, -3, 3, does_not_raise()),
- (-3, -2, -1, does_not_raise()),
- ("hi", -2, 0, pytest.raises(ValueError)),
- (3, "hello", 0, pytest.raises(ValueError)),
- ("ni hao", "bonjour", 0, pytest.raises(ValueError)),
- ("howareyou", "imok", 0, pytest.raises(ValueError)),
- (int, float, 0, pytest.raises(ValueError)),
- ]
- )
- def test_subtraction(self, a, b, result, expectation):
- c = Calculator()
- with expectation:
- assert c.calc(f"{a} - {b}") == result
- @mark.parametrize(
- "a, b, result, expectation",
- [
- (1, 1, 1, does_not_raise()),
- (100, 100, 10000, does_not_raise()),
- (-203, 3, -609, does_not_raise()),
- (43, -3, -129, does_not_raise()),
- (-3, -2, 6, does_not_raise()),
- ("hi", -2, 0, pytest.raises(ValueError)),
- (3, "hello", 0, pytest.raises(ValueError)),
- ("ni hao", "bonjour", 0, pytest.raises(ValueError)),
- ("howareyou", "imok", 0, pytest.raises(ValueError)),
- (int, float, 0, pytest.raises(ValueError)),
- ]
- )
- def test_multiplication(self, a, b, result, expectation):
- c = Calculator()
- with expectation:
- assert c.calc(f"{a} * {b}") == result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement