-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_test_for_add.py
36 lines (22 loc) · 1.1 KB
/
unit_test_for_add.py
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
import unittest
def add(a, b):
return a + b
class TestAddFunction(unittest.TestCase):
def test_add_positive_numbers(self):
result = add(3, 5)
self.assertEqual(result, 8)
def test_add_negative_numbers(self):
result = add(-3, -5)
self.assertEqual(result, -8)
def test_add_zero(self):
result = add(0, 10)
self.assertEqual(result, 10)
# In this example, we have a simple add function that takes two numbers as input and returns their sum. The TestAddFunction class inherits from unittest.TestCase and defines multiple test methods. Each test method begins with the prefix test_ and uses various assertions provided by unittest.TestCase to verify the expected behavior.
# To run the unit tests, execute the script, and the unittest.main() function will discover and run all the test methods within the TestAddFunction class.
# The output will indicate whether the tests pass or fail. For example:
# ...
# ----------------------------------------------------------------------
# Ran 3 tests in 0.001s
# OK
if __name__ == '__main__':
unittest.main()