📖 Scenario: You work as a developer testing a simple REST API that returns user data. You want to check how the API behaves when many requests are sent quickly.
🎯 Goal: Build a small program to send multiple requests to the API and count how many succeed.
📋 What You'll Learn
Create a list of URLs to test
Set a variable for the number of requests to send
Use a loop to send requests and count successful responses
Print the total number of successful requests
💡 Why This Matters
🌍 Real World
Load testing helps check if an API can handle many users or requests at once without slowing down or crashing.
💼 Career
Developers and testers use load testing to ensure web services are reliable and perform well under pressure.
Progress0 / 4 steps
1
Create the list of API URLs
Create a list called api_urls with these exact URLs: "https://jsonplaceholder.typicode.com/users/1", "https://jsonplaceholder.typicode.com/users/2", and "https://jsonplaceholder.typicode.com/users/3".
Rest API
Hint
Use square brackets [] to create a list and include the URLs as strings inside.
2
Set the number of requests
Create a variable called num_requests and set it to 5 to represent how many times you will send requests to each URL.
Rest API
Hint
Just write num_requests = 5 on a new line.
3
Send requests and count successes
Import the requests module. Create a variable called success_count and set it to 0. Use nested for loops: the outer loop uses _ to repeat num_requests times, the inner loop uses url to go through api_urls. Inside the inner loop, send a GET request to url using requests.get(url). If the response status code is 200, increase success_count by 1.
Rest API
Hint
Remember to import the requests module first. Use two loops: one for the number of requests, one for each URL.
4
Print the total successful requests
Write a print statement to display the text "Total successful requests:" followed by the value of success_count using an f-string.
Rest API
Hint
Use print(f"Total successful requests: {success_count}") to show the result.
Practice
(1/5)
1. What is the main purpose of load testing in REST APIs?
easy
A. To check how many users the API can handle at once
B. To find syntax errors in the API code
C. To improve the API's user interface design
D. To add new features to the API
Solution
Step 1: Understand load testing goal
Load testing is done to see how many users or requests an API can handle simultaneously without failing.
Step 2: Compare options with load testing purpose
Only To check how many users the API can handle at once talks about handling many users at once, which matches load testing's main goal.
Final Answer:
To check how many users the API can handle at once -> Option A
Quick Check:
Load testing = check user load [OK]
Hint: Load testing measures user capacity, not code errors [OK]
Common Mistakes:
Confusing load testing with debugging
Thinking load testing improves UI
Assuming load testing adds features
2. Which of the following is the correct way to start a basic Locust load test script?
easy
A. from locust import User
class MyUser(User):
def run():
get('/')
B. import locust
class MyUser:
def task():
client.get('/')
C. from locust import HttpUser, task
class MyUser(HttpUser):
@task
def load(self):
self.client.get('/')
D. from locust import HttpUser
class MyUser(HttpUser):
def test():
self.get('/')
Solution
Step 1: Identify correct Locust import and class
The Locust library uses HttpUser and @task decorator to define user behavior.
Step 2: Check method and client usage
from locust import HttpUser, task
class MyUser(HttpUser):
@task
def load(self):
self.client.get('/') correctly uses HttpUser, @task, and self.client.get('/'). Other options miss correct imports or method calls.
Final Answer:
from locust import HttpUser, task
class MyUser(HttpUser):
@task
def load(self):
self.client.get('/') -> Option C
Quick Check:
Use HttpUser and @task decorator [OK]
Hint: Look for HttpUser and @task decorator to spot correct syntax [OK]
Common Mistakes:
Missing @task decorator
Using wrong class name like User instead of HttpUser
Calling get() without self.client
3. What will be the output when running this Locust test snippet?
from locust import HttpUser, task
class MyUser(HttpUser):
@task
def index(self):
self.client.get("/home")
# Simulate 10 users making requests concurrently
medium
A. No requests will be sent because task is missing
B. 10 users will send GET requests to /home concurrently
C. Syntax error due to missing main function
D. Only 1 user will send a GET request to /home
Solution
Step 1: Understand Locust user simulation
Locust simulates multiple users running the tasks concurrently when configured to do so.
Step 2: Analyze the code behavior
The class defines a task sending GET requests to /home. If run with 10 users, all will send requests concurrently.
Final Answer:
10 users will send GET requests to /home concurrently -> Option B
Hint: Tasks run concurrently for all simulated users [OK]
Common Mistakes:
Thinking only one user runs the task
Assuming missing main causes syntax error
Believing tasks won't run without explicit start
4. Identify the error in this Locust test code:
from locust import HttpUser, task
class MyUser(HttpUser):
def index(self):
self.client.get("/")
medium
A. Class name should be lowercase
B. HttpUser class should be imported from requests
C. self.client.get should be self.get
D. Missing @task decorator before index method
Solution
Step 1: Check for task decorator usage
Locust requires the @task decorator on methods to mark them as tasks to run.
Step 2: Verify other parts of the code
HttpUser is correctly imported, self.client.get is correct, and class names are capitalized by convention.
Final Answer:
Missing @task decorator before index method -> Option D
Quick Check:
Tasks need @task decorator [OK]
Hint: Always add @task decorator to methods to run as tasks [OK]
Common Mistakes:
Forgetting @task decorator
Importing HttpUser from wrong module
Using self.get instead of self.client.get
5. You want to test your REST API's login endpoint under heavy load using Locust. Which approach best simulates realistic user behavior?
hard
A. Create a task that sends POST requests with different user credentials and add wait times between requests
B. Send continuous GET requests to the login endpoint without any delay
C. Run a single user sending POST requests in a tight loop
D. Only test the API with one user to avoid server overload
Solution
Step 1: Understand realistic load testing
Realistic load testing simulates many users with varied data and natural pauses between actions.
Step 2: Evaluate options for realism
Create a task that sends POST requests with different user credentials and add wait times between requests uses different credentials and wait times, mimicking real user behavior. Others either overload or under-test the API.
Final Answer:
Create a task that sends POST requests with different user credentials and add wait times between requests -> Option A
Quick Check:
Use varied data and wait times for realism [OK]
Hint: Add wait times and varied data for realistic load [OK]
Common Mistakes:
Sending requests nonstop without delay
Testing with only one user
Using only GET requests for login
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.