Category: 3. Python Unit Testing

  • Python assertIs()

    Summary: in this tutorial, you’ll learn how to use the Python assertIs() to test if two objects are the same. Introduction to Python assertIs() method The assertIs() allows you to test if two objects are the same. The following shows the syntax of the assertIs() method: If the first and second reference the same object, the test will pass. Otherwise, it’ll fail. The msg is optional. It’s…

  • Python assertAlmostEqual()

    Summary: in this tutorial, you learn how to use the Python assertAlmostEqual() method to test if two values are approximately equal. Introduction to the Python assertAlmostEqual() method The assertAlmostEqual() is a method of the TestCase class of the unittest module. The assertAlmostEqual() test if two values are approximately equal by doing the following: The following shows the syntax of the assertAlmostEqual() method: It uses the following check:…

  • Python assertEqual

    Summary: in this tutorial, you’ll learn how to use the Python assertEqual() method to test if two values are equal. Introduction to the Python assertEqual() method The assertEqual() is a method of the TestCase class of the unittest module. The assertEqual() tests if two values are equal: If the first value does not equal the second value, the test will fail. The msg is optional. If the msg is provided, it’ll…

  • Python Unittest Assert Methods

    Summary: in this tutorial, you’ll learn the overview of the Python unittest assert methods to perform unit testing. Introduction to Python unittest assert methods The TestCase class of the unittest module provides you with a large number of assert methods to test. The following table shows the most commonly used assert methods: Method Checks that assertEqual(x, y, msg=None) x == y assertNotEqual(x,y,msg=None) x != y assertTrue(x,…

  • Organizing Code & Running unittest

    Summary: in this tutorial, you’ll learn how to organize the test code and how to use the various commands to run unit tests. Organizing code If you have a few modules, you can create test modules and place them within the same directory. In practice, you may have many modules organized into packages. Therefore, it’s important to…

  • Skipping Tests

    Summary: in this tutorial, you’ll learn how to skip tests using the Python unittest module. The unittest module allows you to skip a test method or a test class. To skip a test, you have three available options: Skipping a test method examples The following example uses the @unittest.skip() decorator to skip the test_case_2() method unconditionally: Run the test: Output: The output…

  • Python Test Fixtures

    Summary: in this tutorial, you’ll learn about Python test fixtures including setUp() and tearDown() methods. Introduction to the Python Test fixtures By definition, a test fixture is a function or method that runs before and after a block of test code executes. In other words, it is a step carried out before or after a test. Module-level fixtures Suppose you have a test…

  • Python unittest

    Summary: in this tutorial, you’ll learn about the unit test concept and how to use the Python unittest module to perform unit testing. What is a unit test A unit test is an automated test that: The idea of unit testing is to check each small piece of your program to ensure it works properly. It’s different…