This framework implements automated interface testing using Python's unittest module combined with the requests library for HTTP operations.
Project Structure
aototest/
├── Common/ # Utility classes: database helpers, file operations
├── Config/ # Configuration: environment URLs, credentials, DB connections
├── Data/ # Test data: JSON, XML, form data, database records
├── Log/ # Execution logs for debugging
├── Page/ # Page objects modeling system interfaces
├── Result/ # Test reports and execution history
├── Case/ # Test case definitions
└── Run/ # Test execution orchestration
The Page layer models system interfaces as testable objects. Each subsystem contains multiple pages, where a page represents an interface endpoint and its operations become testable methods. This abstraction allows passing different test data sets to validate various scenarios.
Business Logic Layer
The service layer contains core interface operations using the requests library:
# -*- coding: utf-8 -*-
import sys
import requests
sys.setdefaultencoding('utf-8')
def authenticate(username, password):
"""Simulate login interface"""
session = requests.Session()
response = session.get('http://api.example.com/login')
return response.status_code == 200
def create_user(username, password):
"""Create user endpoint"""
return {'username': username, 'password': password}
def update_user(username, password):
"""Update user endpoint"""
return {'username': username, 'password': password}
def remove_user(username, password):
"""Delete user endpoint"""
return {'username': username, 'password': password}
def query_users(username, password):
"""Query users endpoint"""
return {'username': username, 'password': password}
Test Case Layer
Test cases extend unittest.TestCase to validate enterface behavior:
# -*- coding: utf-8 -*-
import sys
import unittest
from Pages.Auth import authenticate
from Pages.UserManager import user_operations
import HTMLTestRunner
import time
class UserManagementTests(unittest.TestCase):
def setUp(self):
"""Initialize test fixtures"""
print('Initializing test environment')
def tearDown(self):
"""Cleanup after each test"""
print('Cleaning up test resources')
def test_create_user(self):
"""Test user creation interface"""
session = authenticate('admin', 'secret123')
result = user_operations.create(session[0], session[1])
self.assertTrue(result)
# Verify duplicate creation fails
result = user_operations.create(session[0], session[1])
self.assertFalse(result)
def test_update_user(self):
"""Test user update interface"""
session = authenticate('admin', 'secret123')
response = user_operations.update(session[0], session[1])
self.assertNotEqual(response, {'username': 'invalid'})
def test_delete_user(self):
"""Test user deletion interface"""
session = authenticate('admin', 'secret123')
response = user_operations.delete(session[0], session[1])
self.assertNotEqual(response, {'username': 'invalid'})
Test Execution
The runner module assembles test suites and generates reports:
# -*- coding: utf-8 -*-
import sys
import os
import unittest
from HTMLTestRunner import HTMLTestRunner
from TestCases.UserManagement import UserManagementTests
import time
if __name__ == '__main__':
# Build test suite
suite = unittest.TestSuite()
suite.addTest(UserManagementTests('test_create_user'))
suite.addTest(UserManagementTests('test_update_user'))
suite.addTest(UserManagementTests('test_delete_user'))
suite.addTest(UserManagementTests('test_update_user'))
suite.addTest(UserManagementTests('test_update_user'))
# Generate report with timestamp
report_path = "../Result/{0}TestReport.html".format(
time.strftime("%Y%m%d%H%M%S", time.localtime())
)
with open(report_path, 'wb') as report_file:
runner = HTMLTestRunner(
stream=report_file,
title='API Test Report',
description='User Management Module Test Results',
verbosity=2
)
runner.run(suite)
Parameterized Testing
The standard unittest framewokr runs one test case per method. For data-driven testing, the parameterized library enables multiple test iterations from a single test method:
# -*- coding: utf-8 -*-
import sys
from nose.tools import assert_equal
from parameterized import parameterized
import HTMLTestRunner
import time
import unittest
import math
@parameterized([
(2, 2, 4),
(2, 3, 8),
(1, 9, 1),
(0, 9, 0),
])
def test_power(base, exponent, expected):
"""Parameterized power function tests"""
assert_equal(math.pow(base, exponent), expected)
class MathOperationsTest(unittest.TestCase):
@parameterized.expand([
("negative_input", -1.5, -2.0),
("integer_input", 1, 1.0),
("fractional_input", 1.6, 1),
])
def test_floor(self, name, input_value, expected_value):
"""Parameterized floor tests"""
assert_equal(math.floor(input_value), expected_value)
Execute tests using nosetests:
nosetests test_run.py --with-html --html-report=report_output.html
This generates an HTML report containing all test execution results.