Search⌘ K

Command-Line Interface

Explore how to use the unittest module's command-line interface to run individual tests or test cases in Python. Learn to execute tests without calling unittest.main() and control test runs for more efficient debugging and verification.

We'll cover the following...

The unittest module comes with a few other commands that we might find useful.

Testing the implementation using commands

Let’s try some commands in the terminal given below and see the output.

#test_mymath2.py
import mymath
import unittest

class TestAdd(unittest.TestCase):
    """
    Test the add function from the mymath library
    """

    def test_add_integers(self):
        """
        Test that the addition of two integers returns the correct total
        """
        result = mymath.add(1, 2)
        self.assertEqual(result, 3)

    def test_add_floats(self):
        """
        Test that the addition of two floats returns the correct result
        """
        result = mymath.add(10.5, 2)
        self.assertEqual(result, 12.5)

    def test_add_strings(self):
        """
        Test the addition of two strings returns the two string as one
        concatenated string
        """
        result = mymath.add('abc', 'def')
        self.assertEqual(result, 'abcdef')


if __name__ == '__main__':
    unittest.main()
Testing different unitest commands

To find out what they are, we can run the unittest module directly and pass it ...