def test_div(self): comb1 = Calc(4, 2) comb2 = Calc(4, 0) div = comb1.div() self.assertEqual(div, 2) with self.assertRaises(ValueError): comb2.div()
class TestCalc: def setup(self): self.calc = Calc() @pytest.mark.parametrize("a,b,expected", [ (1, 1, 2), (-1, 0, -1), (0.1, 0.1, 0.2), (1234567890, 987654321, 2222222211), ("a", 1, TypeError), ([1], 1, TypeError), ({1}, 1, TypeError), ((1,), 1, TypeError), (None, 1, TypeError) ]) def add_test(self, a, b, expected): # a,b 为整数或浮点数 if isinstance(a, (int, float)) and isinstance(b, (int, float)): result = self.calc.add(a, b) assert result == expected # a,b 为其他数据类型时 else: with pytest.raises(expected): self.calc.add(a, b) @pytest.mark.parametrize("a,b,expected", [ (9, 3, 3), (8, 3, 8/3), (-1, 1, -1), (1.1, 0.1, 11.0), (0, 1, 0), (987654310, 123456789, 987654310/123456789), (0, 0, ZeroDivisionError), ("a", 1, TypeError), ([1], 1, TypeError), ({1}, 1, TypeError), ((1,), 1, TypeError), (None, 1, TypeError), ]) def div_test(self, a, b, expected): # 除数为0 if b == 0: with pytest.raises(expected): self.calc.div(a, b) # a,b 为整数或浮点数 elif isinstance(a, (int, float)) and isinstance(b, (int, float)): result = self.calc.div(a, b) assert result == expected # a,b 为其他数据类型 else: with pytest.raises(expected): self.calc.div(a, b)
class TestCalc: def setup(self): self.calc = Calc() @pytest.mark.parametrize( "a,b,expected", [(1, 1, 2), (-1, 0, -1), (0.1, 0.1, 0.2), (1234567890, 987654321, 2222222211), ("a", 1, 'can only concatenate str (not "int") to str'), ([1], 1, 'can only concatenate list (not "int") to list'), ({1}, 1, "unsupported operand type(s) for +: 'set' and 'int'"), ((1, ), 1, 'can only concatenate tuple (not "int") to tuple'), (None, 1, "unsupported operand type(s) for +: 'NoneType' and 'int'")]) def test_add(self, a, b, expected): # a,b 为整数或浮点数 if isinstance(a, (int, float)) and isinstance(b, (int, float)): result = self.calc.add(a, b) assert result == expected # a,b 为其他数据类型时 else: with pytest.raises(TypeError): self.calc.add(a, b) @pytest.mark.parametrize("a,b,expected", [ (9, 3, 3), (8, 3, 8 / 3), (-1, 1, -1), (1.1, 0.1, 11.0), (0, 1, 0), (987654310, 123456789, 987654310 / 123456789), (0, 0, "division by zero"), ("a", 1, "unsupported operand type(s) for /: 'str' and 'int'"), ([1], 1, "unsupported operand type(s) for /: 'list' and 'int'"), ({1}, 1, "unsupported operand type(s) for /: 'set' and 'int'"), ((1, ), 1, "unsupported operand type(s) for /: 'tuple' and 'int'"), (None, 1, "unsupported operand type(s) for /: 'NoneType' and 'int'"), ]) def test_div(self, a, b, expected): # 除数为0 if b == 0: with pytest.raises(ZeroDivisionError): self.calc.div(a, b) # a,b 为整数或浮点数 elif isinstance(a, (int, float)) and isinstance(b, (int, float)): result = self.calc.div(a, b) assert result == expected # a,b 为其他数据类型 else: with pytest.raises(TypeError): self.calc.div(a, b)
class TestCalc2: def setup(self): self.calc = Calc() # def teardown(self) -> None: # print("____________________________________________________________") @pytest.mark.parametrize(["a", "b"], [(0, 0), (1, 3), (0.5, 0.7), (-1, 5)]) def test_add(self, a, b): result = self.calc.add(a, b) print(result) # assert 4==result @pytest.mark.parametrize(["a", "b"], [(0, 0), (1, 3), (0.5, 0.7), (-1, 5)]) def test_div(self, a, b): result = self.calc.div(a, b) print(result) # assert 3==result @pytest.mark.parametrize(["a", "b"], [(0, 0), (1, 3), (0.5, 0.7), (-1, 5)]) def test_sub(self, a, b): result = self.calc.sub(a, b) print(result) @pytest.mark.parametrize(["a", "b"], [(0, 0), (1, 3), (0.5, 0.7), (-1, 5)]) def test_mul(self, a, b): result = self.calc.mul(a, b) print(result) if __name__ == '__main__': pytest.main(['-vs'])
def main(): calc = Calc(int(input("첫번째 수 : ")),int(input("두번째 수 : "))) print(calc.first) print(calc.second) print("{}+{}={}".format(calc.first, calc.second, calc.sum())) print("{}x{}={}".format(calc.first, calc.second, calc.mul())) print("{}-{}={}".format(calc.first, calc.second, calc.sub())) print("{}/{}={}".format(calc.first, calc.second, calc.div()))
def main(): calc = Calc(int(input('첫번째 수: ')), int(input('두번재 수: '))) print(calc.first) print(calc.second) print('{} + {} = {}'.format(calc.first, calc.second, calc.sum())) print('{} * {} = {}'.format(calc.first, calc.second, calc.mul())) print('{} - {} = {}'.format(calc.first, calc.second, calc.sub())) print('{} / {} = {}'.format(calc.first, calc.second, calc.div()))
class TestCalc: @pytest.mark.parametrize(("a", "b", "expect"), yaml.safe_load(open("data/add_data.yml"))) def test_add(self, a, b, expect): self.calc = Calc() result = self.calc.add(a, b) assert result == expect @pytest.mark.parametrize(("a", "b", "expect"), yaml.safe_load(open("data/div_data.yml"))) def test_div(self, a, b, expect): self.calc = Calc() result = self.calc.div(a, b) assert result == expect
class Test_calc(): def setup_class(self): self.calc = Calc() @pytest.mark.parametrize('a,b,c', [(1, 2, 3), (0.1, 0.2, 0.3), (1, 0.1, 1.1), (-1, -2, -3), (1, -2, -1), (-2, 1, -1), (-1, 0.2, -0.8)]) def test_add(self, a, b, c): assert self.calc.add(a, b) == c @pytest.mark.parametrize('a,b,c', [(6, 2, 3), (6, -2, -3), (-6, 2, -3), (-6, -2, 3), (6, 0.5, 12), (0.6, 2, 0.3), (0.6, 0.2, 3), (6, 0, 0)]) def test_div(self, a, b, c): assert self.calc.div(a, b) == c
def calc_div(self, a, b): calc = Calc() result = calc.div(a, b) return result
def test_div(self): calc = Calc() self.assertEqual(3, calc.div(3, 1)) self.assertEqual(-2, calc.div(-4, 2)) self.assertEqual(3, calc.div(-15, -5))
if choice == 1: n1 = float(input("Enter the 1st number to add :")) n2 = float(input("Enter the 2nd number to add :")) cal.add(n1, n2) if choice == 2: n1 = float(input("Enter the 1st number to sub :")) n2 = float(input("Enter the 2nd number to sub :")) cal.sub(n1, n2) if choice == 3: n1 = float(input("Enter the 1st number to mul :")) n2 = float(input("Enter the 2nd number to mul :")) cal.mul(n1, n2) if choice == 4: n1 = float(input("Enter the 1st number to div :")) n2 = float(input("Enter the 2nd number to div :")) cal.div(n1, n2) if choice == 5: n = float(input("Enter a number to find its sine in radians:")) cal.sinrad(n) if choice == 6: n = float(input("Enter a number to find its cos in radians:")) cal.cosrad(n) if choice == 7: n = float(input("Enter a number to find its tan in radians:")) cal.tanrad(n) if choice == 8: n = float(input("Enter a number to find its cosec in radians:")) cal.sinerad(n) if choice == 9: n = float(input("Enter a number to find its sec in radians:")) cal.secrad(n)
from calc import Calc c=Calc() x=c.add(5,7) y=c.sub(5,7) z=c.mult(5,7) a=c.div(20,3) b=c.mod(20,3) print("user 2 made this change") print(f"the diff between 5 and 7 is {y}") print(f"the product of 5 and 7 is {z}") print(f"the div of 20 / 3 is {a}") print(f"the modulus of 20 / 3 is {b}") print("user 1 made this change") print("user 2 made this change") print("milomia 2 made this change") print("milomia made this change")
def test_div1(self): a = Calc(1, 2) self.assertEqual(a.div(), 0.5)
def test_div2(self): b = Calc(3, 4) self.assertEqual(b.div(), 0.75)
def test_div_zero(): calc = Calc() with pytest.raises(ZeroDivisionError): calc.div(1, 0)
def main(): calc = Calc(3, 7) print("{}+{}={}".format(calc.first, calc.second, calc.sum())) print("{}*{}={}".format(calc.first, calc.second, calc.mul())) print("{}-{}={}".format(calc.first, calc.second, calc.minus())) print("{}/{}={}".format(calc.first, calc.second, calc.div()))