-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_calc.py
More file actions
executable file
·46 lines (35 loc) · 1.82 KB
/
unit_calc.py
File metadata and controls
executable file
·46 lines (35 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import unittest
import Unit_testing
class TestClac(unittest.TestCase): # inheriting from unittest module the Testcase class
#defining the method , the method needs to start with test_ to know which methods reprents test
def test_add(self):
#result =Unit_testing.add(10,5)
#self.assertEqual(result,14)
self.assertEqual(Unit_testing.add(10,5),15)
#checking the mulitple edge cases
self.assertEqual(Unit_testing.add(-1,1),0)
self.assertEqual(Unit_testing.add(-1,-1),-2)
self.assertEqual(Unit_testing.add(0,0),0) #ruuning only one test: test_add
def test_subtract(self):
self.assertEqual(Unit_testing.subtract(10,5),5)
self.assertEqual(Unit_testing.subtract(-1,1),-2)
self.assertEqual(Unit_testing.subtract(-1,-1),0)
def test_multiply(self):
self.assertEqual(Unit_testing.multiply(10,5),50)
self.assertEqual(Unit_testing.multiply(-1,1),-1)
self.assertEqual(Unit_testing.multiply(-1,-1),1)
def test_divide(self):
self.assertEqual(Unit_testing.divide(10,5),2)
self.assertEqual(Unit_testing.divide(-1,1),-1)
self.assertEqual(Unit_testing.divide(-1,-1),1)
self.assertEqual(Unit_testing.divide(5,2),2.5)
#must add this case so that the case will give error if it is floor division
self.assertRaises(ValueError, Unit_testing.divide,10 ,0) #to test the zero division error
# Or using the context Manager for exceptions
with self.assertRaises(ValueError):
Unit_testing.divide(10,0)
# '..F.' here dot indicates that the test case is passed and F indicates that it is failed
#navigate module dir and in cmd run "pyhton -m unittest unit_calc.py"
# To run the method directly in cmmd and terminal
if __name__ == '__main__':
unittest.main()