In the ever-evolving landscape of software development, the need for scalable and effective testing processes has never been more crucial. As the complexity of applications grows, it becomes increasingly challenging to maintain a fast, reliable testing workflow. Enter pytest, a popular testing framework for Python that makes writing and organizing tests a breeze for developers.
But what if we could make pytest even better?
Launchable leverages machine learning to predict which tests are most likely to catch potential defects, drastically reducing the time spent on running irrelevant tests. By combining the simplicity and flexibility of pytest with the powerful predictive capabilities of Launchable, you'll be able to achieve a new level of testing efficiency and accuracy.
Dive into the benefits of using pytest with Launchable's Predictive Test Selection, walk through the setup process, and see how these tools can work in harmony to revolutionize your testing workflow. Get ready to transform the way you test your Python applications!
Pytest's elegance and simplicity make writing tests, assertions, fixtures, and plugins a seamless experience for developers. With its intuitive syntax and powerful features, you can craft tests for all types of software testing that are both easy to understand and maintain. Let's explore these concepts in more detail and see how they can be utilized in your testing workflow.
Pytest allows you to write test functions using standard Python assert statements, making your tests clean and readable. To create a test, simply define a function with a name starting with test_
, and use assertions to check if the expected behavior is met. Here's a simple example:
def add(a, b):
return a + b
def test_add():
assert add(2, 3) == 5
assert add(-1, 5) == 4
assert add(0, 0) == 0
Fixtures in pytest provide a convenient way to set up and tear down reusable resources, such as database connections, temporary files, or test data. They help you to maintain a clean, modular test suite. To create a fixture, use the @pytest.fixture decorator shown in the example below.
import pytest
@pytest.fixture
def sample_data():
return {"name": "John Doe", "age": 30}
def test_sample_data(sample_data):
assert sample_data["name"] == "John Doe"
assert sample_data["age"] == 30
In the example above, the sample_data
fixture is automatically passed to any test function that requests it as a parameter, ensuring consistent test data across your test suite.
Pytest's plugin system allows you to extend its functionality or integrate it with other tools, making it even more versatile. You can either use existing plugins or create your own. To install an existing plugin, use the pip package manager. For example, run the following in your command line to install the pytest-cov plugin.
pip install pytest-cov
To create a custom plugin, define a new python module (named my_plugin_module
in the example). From there, you will register the module as an entry point in your setup.py file.
# setup.py
from setuptools import setup
setup(
...
entry_points={"pytest11": ["my_plugin = my_plugin_module"]},
...
)
Testing is an essential aspect of software development, ensuring that your code is robust, reliable, and bug-free. Pytest has emerged as a popular choice among Python developers for its simplicity, flexibility, and powerful features. However, like any tool, it has its limitations.
Easy to use - Simple syntax, Easy to write tests, Automation of tests: Pytest offers a simple and intuitive syntax, making it easy to write tests even for beginners. With its built-in test discovery, pytest automatically detects and runs tests, saving developers time and effort.
Comprehensive features: Parameterized testing, Fixtures, Assertions: Pytest provides a suite of powerful features like parameterized testing, fixtures, and advanced assertion capabilities that make it easier to write comprehensive and maintainable tests.
Extensibility: Plugins, Ability to extend functionality: Pytest's plugin system enables developers to extend its functionality or integrate it with other tools, increasing its versatility.
Comprehensive documentation: Easy-to-understand instructions and examples: Pytest's well-organized documentation provides clear instructions and examples, making it easy for developers to learn and master the framework. The Launchable integration for pytest is documented with code samples - this can be helpful as you’re getting started with pytest and Launchable.
Cross-platform compatibility: Runs on Windows, Linux, and MacOS: Pytest is cross-platform compatible, which means it can run on various operating systems like Windows, Linux, and MacOS, ensuring a smooth testing experience.
Community support: Access to a large, helpful community of users: Pytest boasts a large and supportive community, which is invaluable when it comes to troubleshooting issues, sharing knowledge, and staying up-to-date with the latest developments.
Despite its numerous benefits, pytest also has some limitations that developers should consider:
Limited support for non-Python languages: Pytest is designed with Python in mind, so it does not have extensive support for other languages. This can be a problem if you need to test code written in a language other than Python.
Difficult to debug complex tests: Pytest can be difficult to debug when dealing with complex tests. This can be especially challenging if the test involves multiple components or requires a deep understanding of the code being tested.
Not always suitable for all projects: While pytest is great for unit testing, it may not be suitable for all projects. For example, if your project requires integration testing or end-to-end testing, then pytest may not be the best option.
The good news is that you don’t need to run hours of complex integration test suites if you use Launchable’s Predictive Test Selection.
In today's fast-paced development environment, every minute counts. Lengthy test runs can hamper productivity, delay deployments, decay the developer experience, and increase costs. Fortunately, with the Launchable pytest integration, you can harness the power of predictive test selection to optimize your testing workflow, speed up your tests, and improve your developer experience.
Launchable, the test intelligence platform, uses machine learning to predict which tests are most likely to detect potential defects. With Launchable’s pytest integration, you can prioritize running the most relevant tests, saving precious machine hours and providing faster feedback to developers.
Adding Launchable to pytest allows developers and DevOps teams to:
Reduced test execution time: By only running the most relevant tests, you can significantly cut down the time spent on test execution, enabling quicker iterations and faster deployments.
Optimized resource utilization: Launchable's intelligent test selection ensures that your computing resources are used efficiently, reducing the overall cost of testing and freeing up resources for other tasks.
Increased test suite reliability: By prioritizing the tests most likely to catch defects, you can detect and fix issues early in the development cycle, improving the overall quality and reliability of your codebase.
To get started with the Launchable pytest plugin, it takes four simple steps.
✅ Install the Launchable Pytest Plugin
Install the Launchable pytest plugin: pip3 install pytest-launchable
✅ Set your Launchable API Key
Sign up for a Launchable account (if you haven't already) at https://app.launchableinc.com/signup/ and create your API key. Export your key as an environment variable in your CI script: export LAUNCHABLE_TOKEN=<your_launchable_api_key>
Save this key somewhere safe - avoid leaving it in your source code for all to see.
✅ Record and submit test data to Launchable:
Before Launchable can predict which tests to run, it needs historical test data. To record and submit test data, set up your launchable config.
You can generate a Launchable config by running via pip: launchable-config --create
, or you can copy this example to a new file called .launchable.d/config.yml
:
# Launchable test session configuration file
# See https://docs.launchableinc.com/resources/cli-reference for detailed usage of these options
#
schema-version: 1.0
build-name: commit_hash
record-build:
# Put your git repository location here
source: .
max_days: 30
record-session:
subset:
# mode can be subset, subset-and-rest, or record-only
mode: record-only
# if mode is subset or subset-and-rest, you must specify one of target/confidence/time
# examples:
# target: 30% # Create a variable time-based subset of the given percentage. (0%-100%)
# confidence: 30% # Create a confidence-based subset of the given percentage. (0%-100%)
# time: 30m # Create a fixed time-based subset. Select the best set of tests that run within the given time bound. (e.g. 10m for 10 minutes, 2h30m for 2.5 hours, 1w3d for 7+3=10 days. )
confidence: 99
record-tests:
# The test results are placed here in JUnit XML format
result_dir: launchable-test-result
✅ Enable Predictive Test Selection:
Once you've submitted enough test data, you can start using Launchable's predictive test selection.
All you need to do is update your config.yml
file. Change the subset
mode
to subset
, set an optimization target (one of target
, time
, or confidence
), and save the file. This enables subsetting when you run pytest --launchable
.
Easy peasy, lemon squeezy!
Launchable will then select the most critical and relevant tests, optimizing your testing workflow and providing quicker feedback to your development team.
For comprehensive documentation, see pytest (Integration) | Launchable Docs .
Integrating Launchable into your pytest testing workflow can lead to substantial time and resource savings, allowing you to accelerate the development cycle and deliver high-quality code more efficiently.
By prioritizing the most relevant tests, you'll not only save machine hours but also enable your development team to receive faster feedback, leading to quicker issue resolution and improved overall code quality.
Also published here.