Category: 3. Python Unit Testing
-
Python unittest subtest
Summary: in this tutorial, you’ll learn how to define parameterized tests using unittest‘s subTest() context manager. Introduction to the unittest subtest context manager First, create a new module called pricing.py and define a calculate() function as follows: The calculate() function calculates the net price from the price, tax, and discount. Second, create the test_pricing.py test module to test the calculate() function: To test the calculate() function, you need to come up with…
-
Python unittest Coverage
Summary: in this tutorial, you’ll learn how to use the Python unittest coverage command to generate a test coverage report. What is a test coverage Test coverage is a ratio between the number of lines executed by at least one test case and the total number of lines of the code base: The test coverage…
-
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,…