Unit testing: It’s not just for robots anymore
Unit testing is a software testing technique that has been around for decades. But it’s only recently become more mainstream, thanks partly to the rise of agile development.
Unit testing is a great way to ensure the quality of your code. By writing unit tests, you can catch bugs early and prevent them from causing problems in your code.
Unit testing in Python with unit test
Unit testing is a software testing technique by which individual units of source code, such as functions, classes, or modules, are tested to determine if they are fit for use. Unit testing is often automated using specialised frameworks.
The unit test module in Python provides a framework for writing unit tests. To use the unit test, you first need to import the module:
# This line imports the unittest module.
import unittest
# The unittest module provides a framework for writing unit tests.
# A unit test is a class that inherits from unittest.TestCase.
# Each method in the class is a unit test.
Once you have imported the module, you can start writing unit tests. A unit test is a class that inherits from unittest.TestCase. Each method in the class is a unit test. For example:
# This class contains unit tests for the string methods in Python.
class TestStringMethods(unittest.TestCase):
# This method tests the upper() method.
def test_upper(self):
# Check that the upper() method returns the uppercase string version.
self.assertEqual('test'.upper(), 'TEST')
# This method tests the isupper() method.
def test_isupper(self):
# Check that the isupper() method returns True if the string is uppercase and False otherwise.
self.assertTrue('TEST'.isupper())
self.assertFalse('Test'.isupper())
# This method tests the split() method.
def test_split(self):
# Check that the split() method returns a list of strings, each a substring of the original string.
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# Check that the split() method fails when the separator is not a string.
with self.assertRaises(TypeError):
s.split(2)
The unit test module provides several assert methods that you can use to check the results of your unit tests. For example, the assertEqual() method checks that two values are equal. The assertTrue() method checks that a value is True. The assertFalse() method checks that a value is False. The assertRaises() method checks that a method raises an exception.
Once you have written your unit tests, you can run them using the unittest.main() function:
if __name__ == '__main__':
unittest.main()
The unittest.main() function will run all unit tests in the current module. If any of the unit tests fail, the unittest.main() function will print an error message.
Unit testing is a valuable tool for ensuring the quality of your Python code. By writing unit tests, you can catch bugs early and prevent them from causing problems in your code.
Sources
1.github.com/t2y/python-study subject to license (Apache — 2.0)
2.github.com/hoffstadt/DearPyGui subject to license (bsd-0-clause)