Using Python Generator Functions for Efficient API Automation Testing
This article demonstrates how Python generator functions can be applied in API automation testing to process large data streams, batch requests, infinite sequences, file handling, dynamic test data generation, recursive file traversal, lazy calculations, data filtering, response processing, retry logic, and test report creation, thereby improving memory efficiency and test flexibility.
In API automation testing, Python generator functions can efficiently handle large data streams or batch execution of test cases, avoiding the memory overhead of loading all data at once.
Batch sending requests – Generators split a large list into smaller batches, reducing memory usage.
def batch_request(data_list, batch_size=10):
for i in range(0, len(data_list), batch_size):
yield data_list[i:i+batch_size]
# Assume api_call sends a request for a batch
def api_call(batch_data):
# implement API call logic
pass
data = [1, 2, 3, ..., 100] # large dataset
for batch in batch_request(data, 10):
api_call(batch)Infinite data stream – Generators can produce an unbounded sequence, such as continuously increasing IDs for testing.
def infinite_ids(start=1):
id = start
while True:
yield id
id += 1
id_generator = infinite_ids()
for _ in range(5):
print(next(id_generator))Reading large files in chunks – Using a generator to read a file line‑by‑line prevents loading the entire file into memory.
def read_large_file(file_path):
with open(file_path, 'r') as file:
for line in file:
yield line.strip()
for line in read_large_file('data.txt'):
process_line(line) # assume process_line handles each lineDynamic test data generation – Generators create test case data on demand.
def test_data_generator(num_cases):
for _ in range(num_cases):
yield {
'username': f'user{_}',
'email': f'user{_}@example.com',
'password': 'password123'
}
for case in test_data_generator(5):
print(case)Recursive directory traversal – A generator walks through all files in a directory tree.
import os
def walk_files(dir_path):
for root, dirs, files in os.walk(dir_path):
for file in files:
yield os.path.join(root, file)
for filepath in walk_files('./test_files'):
print(filepath)Lazy computation (Fibonacci) – Generators compute values only when needed.
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
yield a
a, b = b, a + b
for num in fibonacci(10):
print(num)Data filtering – Generators filter large datasets based on a condition.
def filter_data(data_list, condition=lambda x: x % 2 == 0):
for item in data_list:
if condition(item):
yield item
data = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter_data(data))
print(even_numbers)Batch response processing – Generators handle API responses one by one.
def process_responses(responses):
for response in responses:
yield parse_response(response) # assume parse_response processes a single response
responses = [api_call(data) for data in batch_request(data)]
for processed in process_responses(responses):
print(processed)Retry until success – A generator repeatedly calls an API until a successful result is returned or attempts are exhausted.
def retry_until_success(api_func, max_attempts=5):
for attempt in range(max_attempts):
result = api_func()
if result['status'] == 'success':
yield result
break
else:
yield {'status': 'failed', 'message': 'Max attempts reached'}
def check_status():
# placeholder for actual API call logic
pass
for outcome in retry_until_success(check_status):
print(outcome)Test report generation – Generators produce report entries incrementally, avoiding large memory usage.
def generate_test_report(test_results):
for test in test_results:
yield f"Test {test['name']} - Status: {test['status']}\nDetails: {test['details']}"
results = [
{'name': 'Test 1', 'status': 'Pass', 'details': 'All good'},
# ... more test results
]
with open('report.txt', 'w') as report_file:
for entry in generate_test_report(results):
report_file.write(entry)The examples above illustrate diverse applications of generator functions in API automation testing, covering data handling, resource management, and test logic control to enhance testing efficiency and flexibility.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.