Reusing core functions for Locust and pytest: A Path to Efficient Testing
In the world of software development, testing plays a crucial role in ensuring the quality and reliability of applications. Two widely used testing frameworks, Locust and pytest, have gained popularity due to their versatility and ease of use. However, as projects grow in complexity, it becomes essential to adopt practices that promote code reusability and maintainability. This article explores how to leverage the power of reusable functions across Locust and pytest, streamlining the testing process and improving overall efficiency.
To create this article, I've used mocked data with https://mockapi.io/ services and the sources could be found in my automation ideas repository, here: Formartha/python-automation-ideas: Let's speak about ideas related to test automation, with Python (github.com)
Code Breakdown
The provided code demonstrates a practical approach to creating reusable functions for both performance testing with Locust and functional testing with pytest. Let's break it down:
import requests
class ProductApi(object):
def __init__(self, host):
self.host = host
self.session = requests.session()
def users(self, method):
return self.session.request(method=method, url=self.host, verify=False)
performance_test.py: This file defines a Locust test case for load testing the API. The QuickstartUser class extends the HttpUser class from Locust and utilizes the ProductApi class to send API requests. The api task function initiates the API call and prints the response status code.
from locust import HttpUser, task, between
from api import ProductApi
class QuickstartUser(HttpUser):
wait_time = between(1, 2)
@task
def api(self):
lo_client = ProductApi("https://66333084f7d50bbd9b487735.mockapi.io/api/v1/users")
lo_client.session = self.client
print(lo_client.users("GET").status_code)
领英推荐
test_functional.py: This code snippet demonstrates how to use the ProductApi class in a pytest test case. The test_with_pytest function creates an instance of ProductApi, sends a GET request, and asserts that the first user's name matches the expected value.
from api import ProductApi
def test_with_pytest():
client = ProductApi("https://66333084f7d50bbd9b487735.mockapi.io/api/v1/users")
assert client.users('GET').json()[0]['name'] == 'Clemens_Crist'
The Benefits of Reusable Functions
By encapsulating the API interaction logic in the ProductApi class, both the Locust and Pytest tests can leverage the same functionality. This approach offers several advantages:
Conclusion
Reusing functions across Locust and pytest not only promotes code reusability and maintainability but also fosters a more efficient and consistent testing approach. By following the principles demonstrated in this article, automation developers can streamline their testing efforts, reduce code duplication, and ensure that both performance and functional tests remain aligned with the evolving codebase. Embrace the power of reusable functions and unlock a more efficient and reliable testing workflow.
???? ??? ?? ??????! ??? ????? ???? ?????? ??? ?????? ??? ??????? ???? ????? ?????? ?????? ???? ?????? ???? ????, ????? ????? ?????? ?????? ?????: https://chat.whatsapp.com/BubG8iFDe2bHHWkNYiboeU