Posts

Pytest Tutorial - 10 | Advanced - Fixture Scopes and Performance Optimization

In this tutorial, we focus on optimizing test performance by choosing the right fixture scope and managing complex test setups with proper teardown using yield . 1. Optimizing Test Performance: Choosing the Right Fixture Scope Fixtures can have different scopes, such as function , class , module , or session , depending on how long you want the fixture to persist. Simple Example: Module-Scoped Database Connection import pytest @pytest.fixture(scope="module") def db_connection(): print("Connecting to the database...") yield {"db": "connected"} print("Closing database connection") def test_query_1(db_connection): assert db_connection["db"] == "connected" def test_query_2(db_connection): assert db_connection["db"] == "connected" Explanation: The database connection is created once for the entire module and shared across multiple tests, reducing setup overhead. C...

Pytest Tutorial - 9 | Advanced - Fixture Usage and Optimization

In this tutorial, we'll explore advanced fixture features in Pytest: implicit fixtures using autouse=True , handling dependencies between multiple fixtures, and creating dynamic fixtures using factories. 1. Fixture Autouse: Implicit Fixture Usage Fixtures can be automatically applied to all test functions without explicitly including them as arguments, using the autouse=True option. Simple Example: Setting Up and Tearing Down a Temporary Directory import pytest import os @pytest.fixture(autouse=True) def create_tmp_directory(tmpdir): os.mkdir(tmpdir) print(f"Temporary directory created: {tmpdir}") def test_file_creation(): assert os.path.exists("some_file.txt") is False Explanation: The fixture automatically creates a temporary directory before the test runs, ensuring each test has a fresh, isolated environment. Complex Example: Automatically Set Up and Tear Down Database Connections import pytest @pytest.fixture(autouse=True)...

Pytest Tutorial - 8 | Advanced - Parametrization Techniques

In this article, we will explore advanced techniques to master parametrization in Pytest, enabling you to write more scalable and flexible test cases. Pytest's @pytest.mark.parametrize is a powerful feature that allows us to run the same test multiple times with different data inputs. However, beyond the basics, there are more sophisticated ways to leverage this tool, which we’ll cover here. Recap: Basic Parametrization If you're already familiar with basic parametrization in Pytest, you'll know that it involves the @pytest.mark.parametrize decorator, which allows passing various inputs to a single test function. Here’s a quick recap of a basic example: import pytest @pytest.mark.parametrize("a, b, expected", [(1, 2, 3), (4, 5, 9), (10, -5, 5)]) def test_addition(a, b, expected): assert a + b == expected In this simple example, the test runs three times, each with a different set of input values for a , b , and expected . 1. Parametrizing Multip...

Pytest Tutorial 7 | Intermediate - Fixtures in Detail

In this tutorial, we'll explore one of the most powerful features of Pytest: fixtures. Fixtures allow you to set up the environment your tests need, making them more reliable and easier to maintain. We'll cover what fixtures are, how to create and use them, and tackle advanced topics like fixture scope, lifetime, and combining fixtures with parameterization. 1. What are Fixtures? Fixtures in Pytest are functions that manage the setup and teardown of resources needed by your tests. These could be anything from database connections to temporary files or even mock APIs. Using fixtures ensures that your tests start from a clean state and can be isolated from one another, which is crucial for consistent and reliable testing. 2. Creating and Using Fixtures Let's look at how to create a fixture and use it in a real-world scenario, such as testing an API client. Example: API Client Fixture Imagine you’re writing tests for a web application that interacts with a third-party API. You...

Pytest Tutorial - 6 | Intermediate - Parametrization in detail

Introduction In our previous tutorial, we delved into the powerful world of Pytest's parametrize feature. We discovered how it enables us to perform data-driven tests with ease. However, to truly master data-driven testing, we need to move beyond the basics and develop an intermediate understanding of this feature. In this article, we will explore what Pytest parametrize is, the parameters it supports, how it operates, and illustrate its usage with practical examples. What is the Pytest Parametrize Feature? Pytest parametrize is a versatile feature that allows you to execute the same test function multiple times with different sets of input data. This feature streamlines the process of testing various scenarios, making it a fundamental tool in software testing. Allowed Parameters The Pytest parametrize feature supports two main  argnames:  This parameter defines the names of the input arguments to your test function. It can be a string or a list of strings, specifying one or m...

Pytest Tutorial - 5 | The Basics - How to write data-driven tests?

Image
So far we have understood how to write simple tests in pytests . We also saw pytest in action. In this tutorial we will now learn how to write data driven tests. What are data driven Tests? These are those tests, which tests a functionality using various scenarios mocked through a set of test data. In order to achieve this we pass the test data from external data sources such as csv/excel/yaml/json/database/etc. Now why do we need to pass test data from an external data source? Very simple reasons: If the test data is not passed through an external data source, then everytime we add/update/remove test data, we need to update our code. One test case can be tested using multiple combinations of data to test for various conditions of validation Example 1: you can test login using 2 types of test data   Valid credentials Invalid credentials      Example 2: you can also test password change functionality with 6 types of test data : (Minimum - 1) allowed length of passwo...