0
0
Testing Fundamentalstesting~10 mins

Stress testing concepts in Testing Fundamentals - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test simulates a stress test on a web application by sending a high number of requests to check if the system can handle extreme load without crashing. It verifies that the application remains responsive and does not return error status codes under stress.

Test Code - PyTest
Testing Fundamentals
import requests
import threading
import time

# URL to stress test
url = "https://example.com/api/data"

# Number of concurrent requests
num_requests = 50

# Store results
responses = []

# Function to send a GET request
def send_request():
    try:
        response = requests.get(url, timeout=5)
        responses.append(response.status_code)
    except requests.exceptions.RequestException:
        responses.append('error')

# Start time
start_time = time.time()

# Create and start threads
threads = []
for _ in range(num_requests):
    thread = threading.Thread(target=send_request)
    threads.append(thread)
    thread.start()

# Wait for all threads to complete
for thread in threads:
    thread.join()

# End time
end_time = time.time()

duration = end_time - start_time

# Assertions
assert all(code == 200 for code in responses), f"Some requests failed: {responses}"
assert duration < 10, f"Stress test took too long: {duration} seconds"
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsTest environment ready, target URL set to https://example.com/api/data-PASS
2Creates 50 threads to send GET requests concurrently50 threads running, each sending a request to the server-PASS
3Waits for all threads to completeAll requests completed, responses collected-PASS
4Checks all response status codes are 200Responses array contains status codes from all requestsassert all(code == 200 for code in responses)PASS
5Checks total test duration is less than 10 secondsDuration calculated from start to end of requestsassert duration < 10PASS
6Test ends successfullySystem handled stress load without errors or delays-PASS
Failure Scenario
Failing Condition: One or more requests return a status code other than 200 or the test duration exceeds 10 seconds
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the server responses?
AResponses are slower than 10 seconds
BResponses contain at least one error
CAll responses have status code 200
DResponses have status code 404
Key Result
Stress testing uses many simultaneous requests to check system limits and ensures the system remains stable and responsive under heavy load.