def test_monkeypatch(monkeypatch):
	c = Calculator()
	def fake_add(a, b):
		return 42

	monkeypatch.setattr(c, "add", fake_add)
	assert c.add(2, 3) == 42
예제 #2
0
class TestCal:
    def setup(self):
        self.cal = Calculator()

    @pytest.mark.run(order=1)
    @pytest.mark.dependency(name='add')
    @pytest.mark.parametrize('a,b,c',
                             yaml.safe_load(open('./data.yml', 'r'))['add'])
    def test_add(self, a, b, c):
        print(f"测试 {a} + {b} = {c}")
        assert c == self.cal.add(a, b)

    @pytest.mark.run(order=2)
    @pytest.mark.dependency(depends=["add"])
    @pytest.mark.parametrize('a,b,c',
                             yaml.safe_load(open('./data.yml', 'r'))['sub'])
    def test_sub(self, a, b, c):
        print(f"测试 {a} - {b} = {c}")
        assert c == self.cal.sub(a, b)

    @pytest.mark.run(order=4)
    @pytest.mark.dependency(depends=["mul"])
    @pytest.mark.parametrize('a,b,c',
                             yaml.safe_load(open('./data.yml', 'r'))['div'])
    def test_div(self, a, b, c):
        print(f"测试 {a} / {b} = {c}")
        assert c == self.cal.div(a, b)

    @pytest.mark.run(order=3)
    @pytest.mark.dependency(name='mul')
    @pytest.mark.parametrize('a,b,c',
                             yaml.safe_load(open('./data.yml', 'r'))['mul'])
    def test_mul(self, a, b, c):
        print(f"测试 {a} * {b} = {c}")
        assert c == self.cal.mul(a, b)
def test_add_weird():
	c = Calculator()

	# Raise error when any one is string
	with pytest.raises(CalculatorError) as context: 
		result = c.add('10', 20)

	assert str(context.value) == '10 is not a number'
예제 #4
0
class CheckCal:
    def setup(self):
        self.cal = Calculator()

    @pytest.mark.run(order=1)
    @pytest.mark.parametrize('a,b,c',
                             yaml.safe_load(open('./data.yml', 'r'))['mod'])
    def check_mod(self, a, b, c):
        print(f"测试 {a} % {b} = {c}")
        assert c == self.cal.mod(a, b)
예제 #5
0
class ModuleTest(unittest.TestCase):
    def setUp(self):
        self.calc = Calculator(8, 4)

    def tearDown(self):
        pass

    def test_add(self):
        result = self.calc.add()
        self.assertEqual(result, 12)

    def test_sub(self):
        result = self.calc.sub()
        self.assertEqual(result, 4)

    def test_mul(self):
        result = self.calc.mul()
        self.assertEqual(result, 32)

    def test_div(self):
        result = self.calc.div()
        self.assertEqual(result, 2)
예제 #6
0
 def test_div(self, a, b, c):
     cal = Calculator()
     assert c == cal.div(a, b)
예제 #7
0
def conf():
    print("开始计算")
    cal = Calculator()
    yield cal
    print("计算结束")
예제 #8
0
 def test_sub(self, a, b, c):
     cal = Calculator()
     assert c == cal.sub(a, b)
예제 #9
0
 def test_mul(self, a, b, c):
     cal = Calculator()
     assert c == cal.mul(a, b)
예제 #10
0
def check_div(a, b, c):
    cal = Calculator()
    assert c == cal.div(a, b)
예제 #11
0
 def test_add(self, a, b, c):
     cal = Calculator()
     assert c == cal.add(a, b)
예제 #12
0
 def setUp(self):
     self.calc = Calculator(8, 4)
예제 #13
0
def check_sub(a, b, c):
    cal = Calculator()
    assert c == cal.sub(a, b)
	def test_div_weird(self):
		c = Calculator()

		with pytest.raises(CalculatorError):
			result = c.div(3, 0)
def test_div():
	c = Calculator()
	result = c.div(3, 10)
	assert result == 3/10
def test_mul():
	c = Calculator()
	result = c.mul(3, 4)
	assert result == 12
def test_sub():
	c = Calculator()
	result = c.sub(3, 4)
	assert result == -1
def test_add():
	c = Calculator()
	result = c.add(3, 4)
	assert result == 7
def test_add_parameterize(a, b, expected):
	c = Calculator()
	assert c.add(a, b) == expected
def test_add_range():
	c = Calculator()
	for i in range(10):
		assert c.add(i, 2) == i+2
예제 #21
0
 def setup(self):
     self.cal = Calculator()
예제 #22
0
 def test_calculator_add_method_returns_correct_result(self):
 	
     calc = Calculator()
     result = calc.add(2,2)
     self.assertEqual(4, result)