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 test_add_notint_x(self): c = Calc() x = "aaa" y = "bbb" result = 0 with self.assertRaises(TypeError): result = c.add(x,y) print '{0} + {1} = {2}'.format(x, y, result)
def test_add(self): c = Calc() x = 100 y = 200 result = 0 result = c.add(x,y) print '{0} + {1} = {2}'.format(x, y, result) self.assertEqual(x + y, result)
class TestAdd(unittest.TestCase): def setUp(self): self.calc = Calc() def test_add_int(self): result = self.calc.add(10, 15) self.assertEqual(result, 25) def test_add_float(self): result = self.calc.add(10.1, 15.2) self.assertEqual(round(result, 1), 25.3) def test_add_str(self): result = self.calc.add('aaa', 'bbb') self.assertEqual(result, 'aaabbb') def test_add_int_str(self): with self.assertRaises(TypeError): self.calc.add(1, 'aa')
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 CalcTests(unittest.TestCase): def setUp(self): self.subject = Calc() def test_add(self): result = self.subject.add(10, 5) self.assertEqual(result, 15) def test_divide(self): result = self.subject.divide(10, 5) self.assertEqual(2, result) with self.assertRaises(ValueError): self.subject.divide(10, 0)
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
class ModuleTest(unittest.TestCase): # 在每一个测试用例开始时执行,执行测试用例执行前的初始化工作,如初始化变量,生成测试数据等 def setUp(self): self.cal = Calc(6, 4) # 在每一个测试用例结束时执行,执行测试用例的清理工作,如关闭数据库、关闭文件、删除数据等 def tearDown(self): pass # 方法必须以 test 开头 def test_add(self): result = self.cal.add() self.assertEqual(result, 10) def test_sub(self): result = self.cal.sub() self.assertEqual(result, 2)
class TestCalc(unittest.TestCase): """Basic calculator with add, subrtract, multiply and divide functions """ def setUp(self): self.calc = Calc() def test_add(self): self.assertEqual(self.calc.add(20, 10), 30, "incorrect addition") def test_subtract(self): self.assertEqual(self.calc.subtract(20, 10), 10, "incorrect subtraction") def test_multiply(self): self.assertEqual(self.calc.multiply(20, 10), 200, "incorrect multiplication") def test_divide(self): self.assertEqual(self.calc.divide(20, 10), 2, "incorrect division")
def test_add_thee_numbers(): c = Calc() assert c.add(4, 5, 6) == 15
def test_add_two_numbers(): c = Calc() assert c.add(4, 5) == 9
def test_add_five_numbers(): c = Calc() assert c.add(4, 5, 7, 8)
def test_add(self): c = Calc() assert c.add(2,3) == 5 assert c.add(2,5) != 10
def test_add1(self): a = Calc(1,2) self.assertEqual(a.add(), 3)
def test_add(self): calc = Calc() self.assertEqual(13, calc.add(8, 5)) self.assertEqual(-2, calc.add(-8, 6)) self.assertEqual(-6, calc.add(-2, -4))
def calc_add(self, a, b): calc = Calc() result = calc.add(a, b) return result
time.sleep(2) menu() if choice == 0: print("") print("#"*60) print("#") print("# You are closing Calculator 2000. See you soon!") print("#") print("#"*60) print("") exit() elif choice == 1: try: a = float(input("Enter your 1st number to add: ")) b = float(input("Enter your 2nd number to add: ")) cal.add(a,b) time.sleep(2) menu() except: exception() elif choice == 2: try: a = float(input("Enter your 1st number to substract: ")) b = float(input("Enter your 2nd number to substract: ")) cal.substract(a,b) time.sleep(2) menu() except: exception() elif choice == 3: try:
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_add2(self): b = Calc(3,4) self.assertEqual(b.add(), 7)
def test_calc_add(a, b, expected): calc = Calc() assert expected == calc.add(a, b)
print("7: tan in radians \t\t\t 18: Natural Log") print("8: cosec in radians \t\t\t 19: Base 10 log") print("9: sec in radians \t\t\t 20: Log base 'x' ") print("10: cot \t\t\t 21: Square root") print("11: pi \t\t\t 22: power of") print('-' * 20) choice = "" while True: try: choice = int(input("Enter your Choice: ")) except: print("Enter the valid choice: ") 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)
from calc import Calc calc = Calc() output = calc.add(1, 1) assert output == 2
def test_add(self): comb1 = Calc(4, 2) sum1 = comb1.add() self.assertEqual(sum1, 6)