DEV Community

Mohammad Waseem
Mohammad Waseem

Posted on

Streamlining Test Account Management in Microservices with Python

Streamlining Test Account Management in Microservices with Python

Managing test accounts efficiently is a critical challenge in a microservices architecture, especially when multiple services require isolated test environments. Manual handling of test data can lead to inconsistencies, duplicates, and increased setup times, ultimately hampering the CI/CD pipeline and slowing down development cycles.

As a Lead QA Engineer, I developed a Python-based solution to automate and synchronize test account provisioning and cleanup processes. This approach not only reduces manual effort but also ensures consistency across services, improves test reliability, and accelerates release timelines.

The Challenge

In a microservices ecosystem, each service often has its own user management system or database. During testing, creating, updating, and deleting test accounts across these discrete systems can become complex. Manual interventions lead to risks of residual data, inconsistent states, and increased onboarding time for new test cases.

The goal was to design a unified, reusable, and scalable process that could:

  • Programmatically create test accounts across multiple services.
  • Validate the creation and status of these accounts.
  • Clean up test accounts post-testing to prevent data pollution.

The Python Solution

Leveraging Python's rich ecosystem of HTTP clients, concurrency tools, and configuration management, I built a centralized script that interacts with each microservice's API endpoint.

Architecture Overview

The core components of the solution include:

  • Config file: Defines service endpoints, credentials, and test account parameters.
  • API Client: Handles requests to create, verify, and delete accounts.
  • Orchestrator: Manages the workflow, including retries and parallel execution.

Example Implementation

Here's an example Python script illustrating the main components:

import requests
import json
import threading
import time

# Load configuration
with open('test_account_config.json', 'r') as config_file:
    config = json.load(config_file)

class APIClient:
    def __init__(self, base_url, auth):
        self.base_url = base_url
        self.auth = auth

    def create_account(self, user_data):
        url = f"{self.base_url}/accounts"
        response = requests.post(url, json=user_data, auth=self.auth)
        response.raise_for_status()
        return response.json()

    def delete_account(self, account_id):
        url = f"{self.base_url}/accounts/{account_id}"
        response = requests.delete(url, auth=self.auth)
        response.raise_for_status()
        return response.status_code == 204

    def verify_account(self, account_id):
        url = f"{self.base_url}/accounts/{account_id}"
        response = requests.get(url, auth=self.auth)
        return response.status_code == 200

# Instantiate clients for each service
clients = []
for service in config['services']:
    client = APIClient(service['endpoint'], (service['username'], service['password']))
    clients.append({'name': service['name'], 'client': client})

# Function to create test accounts in parallel
def setup_test_accounts():
    threads = []
    for c in clients:
        t = threading.Thread(target=lambda: c['client'].create_account({
            'username': 'test_user',
            'email': f"test_user_{c['name']}@example.com",
            'role': 'tester'
        }))
        threads.append(t)
        t.start()
    for t in threads:
        t.join()

# Function to cleanup test accounts
def cleanup_test_accounts():
    for c in clients:
        # Ideally store created account IDs when creating accounts
        # Here simulated as a placeholder
        account_id = 'dummy_id'
        try:
            c['client'].delete_account(account_id)
        except requests.HTTPError:
            print(f"Failed to delete account in {c['name']}")

# Run setup
print("Creating test accounts...")
setup_test_accounts()

# Verify creation
for c in clients:
    is_verified = c['client'].verify_account('dummy_id')
    print(f"Account in {c['name']} verified: {is_verified}")

# Run cleanup
print("Cleaning up test accounts...")
cleanup_test_accounts()
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • Centralized Orchestration: Using a Python script to manage test accounts simplifies provisioning and cleanup.
  • Concurrency: Parallel creation and deletion reduce total execution time.
  • Config-Driven: External configuration files enable flexible scaling and environment-specific adjustments.
  • Robust Error Handling: Incorporating retries and exception management ensures resilience.

Conclusion

Automating test account management with Python in a microservices architecture improves testing reliability and operational efficiency. The outlined approach offers a foundation that can be extended with features like account status monitoring, dynamic test data generation, and integration with CI pipelines.

Adopting such automation practices enables QA teams to focus on test coverage and quality assurance, rather than manual data management, ultimately accelerating delivery cycles and ensuring system integrity.


Sources:


🛠️ QA Tip

To test this safely without using real user data, I use TempoMail USA.

Top comments (0)