Advertisement
Chl_Snt

4_HW_31

Dec 8th, 2023
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. import pytest
  2. from contextlib import nullcontext as does_not_raise
  3. from main import Calculator
  4. from pytest import mark
  5.  
  6.  
  7. class TestCalcFunctions:
  8.     @mark.parametrize(
  9.         "a, b, result, expectation",
  10.         [
  11.             (10, 2, 5, does_not_raise()),
  12.             (50, 2, 25, does_not_raise()),
  13.             (20, 10, 2, does_not_raise()),
  14.             (30, -3, -10, does_not_raise()),
  15.             (10, 0, 0, pytest.raises(ZeroDivisionError)),
  16.             ("hello", 3, 5, pytest.raises(ValueError)),
  17.             (40, "hello", 5, pytest.raises(ValueError)),
  18.             (Calculator, range, 5, pytest.raises(ValueError)),
  19.             (pytest, {3, 3, 3}, 5, pytest.raises(ValueError)),
  20.         ]
  21.     )
  22.     def test_division(self, a, b, result, expectation):
  23.         c = Calculator()
  24.         with expectation:
  25.             assert c.calc(f"{a} / {b}") == result
  26.  
  27.     @mark.parametrize(
  28.         "a, b, result, expectation",
  29.         [
  30.             (1, 1, 2, does_not_raise()),
  31.             (100, 100, 200, does_not_raise()),
  32.             (-203, 3, -200, does_not_raise()),
  33.             (43, -3, 40, does_not_raise()),
  34.             (-3, -2, -5, does_not_raise()),
  35.             ("hi", -2, 0, pytest.raises(ValueError)),
  36.             (3, "hello", 0, pytest.raises(ValueError)),
  37.             ("ni hao", "bonjour", 0, pytest.raises(ValueError)),
  38.             ("howareyou", "imok", 0, pytest.raises(ValueError)),
  39.             (int, float, 0, pytest.raises(ValueError)),
  40.         ]
  41.     )
  42.     def test_addition(self, a, b, result, expectation):
  43.         c = Calculator()
  44.         with expectation:
  45.             assert c.calc(f"{a} + {b}") == result
  46.  
  47.     @mark.parametrize(
  48.         "a, b, result, expectation",
  49.         [
  50.             (10, 1, 9, does_not_raise()),
  51.             (100, 14, 86, does_not_raise()),
  52.             (-203, 3, -206, does_not_raise()),
  53.             (0, -3, 3, does_not_raise()),
  54.             (-3, -2, -1, does_not_raise()),
  55.             ("hi", -2, 0, pytest.raises(ValueError)),
  56.             (3, "hello", 0, pytest.raises(ValueError)),
  57.             ("ni hao", "bonjour", 0, pytest.raises(ValueError)),
  58.             ("howareyou", "imok", 0, pytest.raises(ValueError)),
  59.             (int, float, 0, pytest.raises(ValueError)),
  60.         ]
  61.     )
  62.     def test_subtraction(self, a, b, result, expectation):
  63.         c = Calculator()
  64.         with expectation:
  65.             assert c.calc(f"{a} - {b}") == result
  66.  
  67.     @mark.parametrize(
  68.         "a, b, result, expectation",
  69.         [
  70.             (1, 1, 1, does_not_raise()),
  71.             (100, 100, 10000, does_not_raise()),
  72.             (-203, 3, -609, does_not_raise()),
  73.             (43, -3, -129, does_not_raise()),
  74.             (-3, -2, 6, does_not_raise()),
  75.             ("hi", -2, 0, pytest.raises(ValueError)),
  76.             (3, "hello", 0, pytest.raises(ValueError)),
  77.             ("ni hao", "bonjour", 0, pytest.raises(ValueError)),
  78.             ("howareyou", "imok", 0, pytest.raises(ValueError)),
  79.             (int, float, 0, pytest.raises(ValueError)),
  80.         ]
  81.     )
  82.     def test_multiplication(self, a, b, result, expectation):
  83.         c = Calculator()
  84.         with expectation:
  85.             assert c.calc(f"{a} * {b}") == result
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement