class TestCalculator(TestCase): def setUp(self): self.calc = Calculator() @patch('main.Calculator.sum', return_value=9) def test_sum(self, sum): self.assertEqual(sum(2, 3), 9) def test_sum2(self): answer = self.calc.sum(2, 4) self.assertEqual(answer, 6) @patch('main.Blog') def test_blog_posts(self, MockBlog): blog = MockBlog() blog.posts.return_value = [{ 'userId': 1, 'id': 1, 'title': 'Test Title', 'body': 'Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy\ lies a small unregarded yellow sun.' }] response = blog.posts() self.assertIsNotNone(response) self.assertIsInstance(response[0], dict)
class TestCalculator(TestCase): def setUp(self): self.calc = Calculator() def test_sum(self): answer = self.calc.sum(2, 4) self.assertEqual(answer, 6)
class TestCalculator(TestCase): # using mock patch @patch('main.Calculator.sum', return_value=6) def test_sum(self, sum): self.assertEqual(sum(2, 4), 6) # using normal unit testing def setUp(self): self.calc = Calculator() def test_two(self): ans = self.calc.sum(2, 5) self.assertEqual(ans, 7)
class TestCalculator(unittest.TestCase): def setUp(self): self.test_class = Calculator() self.data = {} with open('fixtures_test_class.json', encoding='utf8') as f: self.data = json.load(f) def test_calculate_sum(self): for v in self.data['sum']: self.assertEqual(self.test_class.sum(v[0], v[1]), v[2]) def test_calculate_mult(self): for v in self.data['mult']: self.assertEqual(self.test_class.mult(v[0], v[1]), v[2])
class TestCalc(unittest.TestCase): def setUp(self): self.a = 13 self.b = 2 self.calc = Calculator(self.a, self.b) def test_sum(self): self.assertEqual(self.calc.sum(), 15) def test_sub(self): self.assertEqual(self.calc.sub(), 11) def test_mul(self): self.assertEqual(self.calc.mul(), 26) def test_div(self): self.assertEqual(self.calc.div(), 13 / 2)