Unit testing
Unit testing is a type of testing that involves testing individual components or modules of a software application. In unit testing, each unit of code is tested in isolation to ensure that it performs as expected. Here is an example of a unit test written in Python using the unittest library:
css
import unittest
def add(a, b):
return a + b
class TestAdd(unittest.TestCase):
def test_add(self):
self.assertEqual(add(2, 3), 5)
self.assertEqual(add(0, 0), 0)
self.assertEqual(add(-2, 3), 1)
if __name__ == '__main__':
unittest.main()
In this example, we define a function add that takes two arguments and returns their sum. We also define a test class TestAdd that inherits from the unittest.TestCase class. The TestAdd class contains a test method test_add that tests the add function using the assertEqual method. The assertEqual method checks whether the actual output of the add function matches the expected output.
No comments:
Post a Comment