Skip to content

Commit b6c3250

Browse files
committed
Simple example of Python unittest module
1 parent d71d698 commit b6c3250

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Programs/P71_PythonUnittest.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Author: OMKAR PATHAK
2+
3+
# This module helps to build the testcases for a particular program to test its integrity and overall execution
4+
5+
import unittest
6+
7+
def checkPrime(number):
8+
'''This function checks if the number is a prime number'''
9+
if number == 2:
10+
return True
11+
if number > 2:
12+
for i in range(2, number):
13+
if number % i == 0:
14+
return False
15+
break
16+
else:
17+
return True
18+
break
19+
else:
20+
return False
21+
22+
# Class for providing test cases
23+
class CheckPrime(unittest.TestCase):
24+
25+
def test_checkPrime(self):
26+
self.assertEqual(checkPrime(3), True) # Check if the function returns the value specified in the second argument
27+
28+
def test_checkPrime2(self):
29+
self.assertTrue(checkPrime(5)) # Check if the function returns True
30+
self.assertFalse(checkPrime(4)) # Check if the function returns False
31+
32+
def test_checkPrime3(self):
33+
# Check that providing a string input produces an error
34+
with self.assertRaises(TypeError):
35+
checkPrime('1')
36+
37+
if __name__ == '__main__':
38+
unittest.main()
39+
40+
# OUTPUT:
41+
# ----------------------------------------------------------------------
42+
# Ran 3 tests in 0.000s
43+
#  
44+
# OK

0 commit comments

Comments
 (0)