Author: admin

  • Python Mock Requests

    Summary: In this tutorial, you’ll learn how to mock the requests module in Python to test an API call using the unittest module. The requests module is an HTTP library that allows you to send HTTP requests easily. Typically, you use the requests module to call an API from a remote server. The scenario For the demo purposes, we’ll use a public API…

  • Python Stubs

    Summary: in this tutorial, you’ll learn how to use Python stubs to isolate parts of your program from each other for unit testing. Introduction to the Python stubs Stubs are test doubles that return hard-coded values. The primary purpose of stubs is to prepare a specific state of the system under test. Stubs are beneficial…

  • Python patch()

    Summary: in this tutorial, you’ll learn how to use the Python patch() to replace a target with a mock object temporarily. Introduction to the Python patch The unittest.mock module has a patch() that allows you to temporarily replace a target with a mock object. A target can be a function, a method, or a class. It’s a string with the following format: To use…

  • Python Unittest Mock

    Summary: in this tutorial, you’ll learn about the Python unittest Mock class and how to use it to mock other classes. Introduction to Python unittest Mock class Mocks simulate the behaviors of real objects. To test an object that depends on other objects in an isolated manner, you use mock objects to mock the real objects. To…

  • Python assertIn()

    Summary: in this tutorial, you’ll learn how to use the Python assertIn() method to test if a member is in a container. Introduction to the Python assertIn() method The assertIn() is a method of the TestCase class of the unittest module. The assertIn() method tests if a member is in a container: If the member is in the container, the test will pass. Otherwise, it’ll…

  • Python assertTrue()

    Summary: in this tutorial, you’ll learn how to use Python assertTrue() to test if an expression is True and assertFalse() method to test if an expression is False. Introduction to the Python assertTrue() method The assertTrue() is a method of the TestCase class in the unittest module. The assertTrue() method tests if an expression is True: If the expr is True, the test passes. Otherwise, the test fails. The msg is optional. If…

  • Python assertIsNone()

    Summary: in this tutorial, you’ll learn how to use the Python assertIsNone() method to test if an expression is None. Introduction to the Python assertIsNone() method The assertIsNone() is a method of the TestCase class of the unittest module. The assertIsNone() test if an expression is None: If the expr is None, the test passes. Otherwise, the test will fail. The msg is optional. It’ll be displayed in the test result if…

  • Python assertIsInstance()

    Summary: in this tutorial, you’ll learn how to use Python assertIsInstance() method to test if an object is an instance of a class. Introduction to the Python assertIsInstance() method The assertIsInstance() is a method of the TestCase class of the unittest module. The assertIsInstance() method tests if an object is an instance of a class. The following shows the syntax of the assertIsInstance() method: In this syntax: Internally,…

  • 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:…