What if you could test hundreds of users without typing a single username by hand?
Why Reading test data from CSV in Selenium Python? - Purpose & Use Cases
Imagine testing a website login form by typing usernames and passwords manually every time you run a test.
You have dozens of user accounts to check, and you write each one by hand in your test script.
Manually entering test data is slow and boring.
It is easy to make mistakes like typos or forgetting to update data.
When you want to test many users, it becomes a huge pain and wastes time.
Reading test data from a CSV file lets your test script load many user details automatically.
This means you write the data once in a simple file, and your tests use it again and again without errors.
username = 'user1' password = 'pass1' # Repeat for each user manually
import csv with open('users.csv', newline='') as file: reader = csv.DictReader(file) for row in reader: username = row['username'] password = row['password']
You can run many tests quickly and reliably by loading data from files instead of typing it every time.
Testing a shopping site login with 100 different user accounts stored in a CSV file, so you can check all logins in one go without rewriting tests.
Manual data entry is slow and error-prone.
CSV files store test data cleanly and simply.
Reading CSV data automates and speeds up testing.
Practice
Solution
Step 1: Understand the purpose of external test data
Using CSV files lets you separate test data from test code, making tests flexible.Step 2: Identify the benefit in test execution
With CSV, you can run the same test multiple times with different inputs easily.Final Answer:
It allows running tests with multiple sets of data without changing the code. -> Option AQuick Check:
CSV externalizes data = flexible tests [OK]
- Thinking CSV speeds up test execution
- Believing CSV fixes code bugs automatically
- Assuming CSV encrypts data
data.csv for reading test data?Solution
Step 1: Identify the correct file mode for reading
To read a file, use mode 'r' which stands for read-only.Step 2: Check the options
file = open('data.csv', 'r') uses 'r' mode correctly; others use write ('w'), append ('a'), or default mode which may not be explicit.Final Answer:
file = open('data.csv', 'r') -> Option BQuick Check:
Read mode = 'r' [OK]
- Using 'w' which overwrites the file
- Using 'a' which appends instead of reading
- Not specifying mode explicitly
users.csv with content:username,password alice,pass123 bob,secure456
What will the output of this code be?
import csv
with open('users.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)Solution
Step 1: Understand csv.reader output
csv.reader reads each line as a list of strings split by commas.Step 2: Analyze the print output
Each row prints as a list, so header and data rows print as lists of strings.Final Answer:
['username', 'password'] ['alice', 'pass123'] ['bob', 'secure456'] -> Option DQuick Check:
csv.reader returns lists per row [OK]
- Expecting strings instead of lists
- Confusing tuples with lists
- Thinking csv.reader raises error on valid files
import csv
file = open('testdata.csv')
reader = csv.reader(file)
for row in reader:
print(row)
file.close()Solution
Step 1: Check file handling best practices
Using open() without 'with' can cause the file to remain open if errors occur.Step 2: Identify the issue in the code
Though file.close() is called, if an error happens before, file may not close properly.Final Answer:
Using open() without a context manager can cause resource leaks. -> Option CQuick Check:
Use 'with' to auto-close files [OK]
- Thinking file.close() always prevents leaks
- Believing csv.reader can't work with open()
- Ignoring explicit read mode is optional
credentials.csv. Which code snippet correctly reads the CSV and uses the data in a test function?Solution
Step 1: Choose correct CSV reading method for named columns
csv.DictReader reads CSV rows as dictionaries with column names as keys.Step 2: Use dictionary keys to access username and password
Accessing row['username'] and row['password'] matches CSV headers and passes correct data.Final Answer:
Using csv.DictReader and accessing row['username'] and row['password'] correctly. -> Option AQuick Check:
DictReader + keys = clean data access [OK]
- Using csv.reader but accessing row by keys
- Not closing files without 'with' block
- Forgetting to specify read mode explicitly
Ready to prove it?
Pass the test and this pattern moves unknown
