Implementing Interface Automation Testing with Python Requests Library

Core Environment Configuration

This approach leverages Python's unittest framework combined with the requests library to automate HTTP request testing.

Key Components:

  • Python: Offers clear syntax and powerful standard/third-party libraries.
  • unittest: The predominant testing framework in Python's ecosystem.
  • Requests: A user-friendly HTTP library built on urllib that simplifies client-server communication.

Setup Commands:

pip install requests
python -m pip install pillow

Executing HTTP Requests

Install the requests library and import necessary modules.

import requests
import json

url = 'http://***/account/login'
auth_data = {
    "username": "***01",
    "password": "password",
}

# Execute POST request and parse JSON response
response = requests.post(url, auth_data).json()
print(response)

# Get raw text response
response_text = requests.post(url, auth_data).text
print(response_text)

Structured Request Handler

Create a class to manage different HTTP methods.

import requests
import json

class ApiClient:
    def send_request(self, endpoint, payload, http_method):
        if http_method == "post":
            result = requests.post(endpoint, payload)
        else:
            result = requests.get(endpoint, payload)
        print(result.text)  # Raw output
        print(result.json()) # JSON output
        return result

# Instantiate and use the client
client = ApiClient()
login_endpoint = 'https://www.******/pc/member/sign'
credentials = {
    "flag": "mobile",
    "password": "f5e05a41724115d076bfb1fd2bd9613e",
    "mobile_phone": "136********"
}
client.send_request(login_endpoint, credentials, 'post')

Unit Testing Implementation

Unit testing verifies individual code units (functions/classes) early in development, typically conducted by developers.

1. Create a Module Under Test

Save as math_operations.py.

class Calculator:
    def add_values(self, x, y):
        return x + y
    
    def subtract_values(self, x, y):
        return x - y

2. Define Test Cases

import unittest
from math_operations import Calculator

class TestCalculator(unittest.TestCase):
    def test_addition(self):
        calc = Calculator()
        total = calc.add_values(3, 6)
        print(f'Sum result: {total}')
        self.assertEqual(9, total)

    def test_subtraction(self):
        calc = Calculator()
        difference = calc.subtract_values(9, 8)
        print(f'Difference result: {difference}')
        self.assertEqual(1, difference)

3. Generate Text-Based Test Report

import unittest
from test_calculator import TestCalculator

# Assemble test suite
suite = unittest.TestSuite()
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(TestCalculator))

# Execute and log to file
with open('test_results.txt', 'w+') as report_file:
    runner = unittest.TextTestRunner(
        stream=report_file,
        verbosity=2,
    )
    runner.run(suite)

Sample Console/File Output:

test_addition (test_calculator.TestCalculator) ... ok
test_subtraction (test_calculator.TestCalculator) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK

4. Generate HTML Test Report

import unittest
from HTMLTestRunner import HTMLTestRunner
from test_calculator import TestCalculator

# Assemble test suite
suite = unittest.TestSuite()
loader = unittest.TestLoader()
suite.addTest(loader.loadTestsFromTestCase(TestCalculator))

# Generate HTML report
with open('D:/test/test_report.html', 'wb') as html_file:
    report_runner = HTMLTestRunner(
        stream=html_file,
        title="Arithmetic Operations Test",
        description="Test Case Execution Summary:"
    )
    report_runner.run(suite)

Tags: python Automation Testing Requests Library Unit Testing HTTP Client

Posted on Sun, 10 May 2026 00:11:14 +0000 by Jas