📌 Who This Article Is For
- Those who want to set up a Python API testing environment from scratch
- Those who are unsure what to install with pip install
- Those who want to learn how to use virtual environments (venv)
- Those who want to start API testing with pytest, requests, or Playwright
✅ What You Will Learn
- The libraries needed for API testing and their install commands
- How to isolate environments using virtual environments (venv)
- How to auto-generate HTML reports by configuring pytest.ini
- The full workflow from installation to verifying everything works
When starting API testing with Python, many people wonder: “What do I need to install with pytest or requests?” or “Do I really need a virtual environment?” Environment setup is where most beginners get stuck first.
This article walks you through everything from installing the Python libraries needed for API testing to verifying your setup. Just copy and paste the commands and your environment will be ready to go.
- 00. Libraries Required for Python API Test Environment Setup
- 01. Creating a Virtual Environment (Recommended)
- 02. Installing Python API Test Libraries with pip install
- 03. Verifying the Installation
- 04. Configuring pytest.ini for Automatic HTML Reports
- 05. Verification Test
- 06. Pitfalls & Lessons Learned
- 07. Frequently Asked Questions (FAQ)
- 08. Summary
00. Libraries Required for Python API Test Environment Setup
There are three main types of libraries used in API testing.
| Library | Role | Use Case |
|---|---|---|
requests | HTTP client | Send requests to the API |
pytest | Test framework | Run and manage tests |
pytest-html | Report generation | Auto-generate HTML reports |
playwright | Browser automation | Run UI tests (E2E tests). API testing is also available. |
pytest-playwright | pytest integration | Use Playwright via pytest fixtures |
💡 Key Takeaway:You only need one of requests or Playwright. Use requests for simple API testing only, and Playwright if you want to combine UI and API testing.
01. Creating a Virtual Environment (Recommended)
Using a virtual environment lets you isolate libraries per project. It’s strongly recommended to set this up first to avoid affecting other projects.
Creating and Activating the Virtual Environment
# Create a virtual environment
python -m venv venv
# Activate (Windows)
venv\Scripts\activate
# Activate (Mac/Linux)
source venv/bin/activateOnce activated, (venv) will appear at the start of the terminal prompt.
(venv) PS C:\Users\HP\Desktop\upwork>💡 Key Takeaway:When you install libraries while the virtual environment is active, they are installed only for that project. Run deactivate to exit the virtual environment when you’re done.
02. Installing Python API Test Libraries with pip install
① requests-based setup (simple configuration)
pip install requests pytest pytest-html② Playwright-based setup (UI + API both supported)
pip install playwright pytest pytest-playwright pytest-html
playwright install chromium⚠️ Note:playwright install chromium requires downloading a browser, so the first run may take a few minutes. It can be skipped for API-only testing, but it’s handy to have ready if you plan to add UI tests later.
03. Verifying the Installation
Version Check Commands
# Check pytest version
pytest --version
# Check requests version
python -c "import requests; print(requests.__version__)"
# Check playwright version
playwright --versionExpected Output
pytest 9.0.2
2.32.3
Version 1.50.0💡 Key Takeaway:If version numbers are displayed, the installation was successful. If you get an error, refer to the pitfalls section below.
04. Configuring pytest.ini for Automatic HTML Reports
Create a pytest.ini file at the project root so that an HTML report is automatically generated every time you run tests.
Folder Structure
project/
├── pytest.ini ← Create this file here
├── test_api.py
└── report.html ← Auto-generated after running testspytest.ini Contents
# pytest.ini
[pytest]
addopts = --html=report.html --self-contained-htmlHow to Run After Configuration
# HTML report is auto-generated with just this command
pytest test_api.py -v💡 Key Takeaway:Adding --self-contained-html embeds CSS and images directly into the HTML file, making it fully self-contained. This makes it easy to share on GitHub or with clients.
05. Verification Test
Once the environment is set up, run the following code to verify everything is working.
# test_check.py
import requests
def test_environment_check():
"""Verification test to confirm the environment is working"""
response = requests.get("https://jsonplaceholder.typicode.com/users/1")
assert response.status_code == 200
assert response.json()["id"] == 1
print("\n✅ Environment setup successful! API testing is working.")# Run command
pytest test_check.py -v -sExpected Output on Success
test_check.py::test_environment_check
✅ Environment setup successful! API testing is working.
PASSED
1 passed in 1.23s ✅💡 Key Takeaway:The -s option makes print() output visible in the terminal. This is handy when verifying your setup.
06. Pitfalls & Lessons Learned
Here are the key issues I encountered during implementation. I hope this helps others who run into the same problems.
① Import error after pip install
Sometimes you get a ModuleNotFoundError even after installing a package. The cause is usually that multiple Python versions are installed and pip and python are pointing to different ones.
# ❌ pip and python pointing to different versions
pip install requests # Installed into Python 3.11
python test.py # Running with Python 3.9 → ModuleNotFoundError# ✅ Using python -m pip guarantees the same Python is used
python -m pip install requests💡 Key Takeaway:Using python -m pip instead of just pip ensures that libraries are installed into the same Python environment that will be used to run your code.
② Forgetting to activate the virtual environment
A common mistake is creating a virtual environment but forgetting to activate it, causing libraries to be installed globally. Make a habit of checking the terminal prompt before running any commands.
# ❌ Virtual environment is not activated
PS C:\upwork> pip install requests # Installed globally
# ✅ (venv) at the start means it's active
(venv) PS C:\upwork> pip install requests⚠️ Note:In VSCode, opening a new terminal can sometimes deactivate the virtual environment. You can add a setting to .vscode/settings.json to auto-activate it.
③ Forgetting playwright install causes a browser error
pip install playwright alone does not install the browser, causing an error at runtime.
# ❌ Running without a browser installed causes an error
# playwright._impl._errors.Error: Executable doesn't exist
# ✅ The browser must also be installed separately
pip install playwright
playwright install chromium ← This is also required!💡 Key Takeaway:playwright install is not needed for API-only testing. You can run it when UI testing becomes necessary.
④ Using –html without pytest-html installed
Even if you write --html=report.html in pytest.ini, you’ll get an error if pytest-html is not installed.
# ❌ Error when running without pytest-html
# ERROR: unrecognized arguments: --html=report.html
# ✅ Install pytest-html first
pip install pytest-html
pytest test_api.py -v💡 Key Takeaway:Install everything in one go to avoid forgetting. Run pip install requests pytest pytest-html all at once.
⑤ Sharing your environment with requirements.txt
If you’re working with a team or want to reproduce the same environment on a different PC, use requirements.txt.
# Export current environment to requirements.txt
pip freeze > requirements.txt
# Install all packages from requirements.txt
pip install -r requirements.txt💡 Key Takeaway:When uploading code to GitHub, committing requirements.txt alongside your code lets anyone instantly reproduce your environment. This also adds polish to your portfolio.
07. Frequently Asked Questions (FAQ)
Q. What Python version should I use?
A. Python 3.9 or higher is required. Using Python 3.11 or higher is recommended. Note that pytest 9 requires Python 3.9 or higher. Run python --version to check your current version.
Q. Is a virtual environment absolutely necessary?
A. It’s not strictly required, but strongly recommended. Without one, library version conflicts are common and can break your environment later. Getting into the habit from the start saves headaches down the line.
Q. Should I use requests or Playwright?
A. Use requests for API testing only, and Playwright if you want to handle both UI and API testing. If you’re building a portfolio that covers both, using Playwright consistently keeps the codebase simpler.
Q. pip install is very slow. What can I do?
A. This can depend on your network environment. Running pip install --upgrade pip to update pip itself sometimes helps. You can also try specifying the repository explicitly with pip install -i https://pypi.org/simple/ requests.
Q. Should I use requirements.txt or pyproject.toml?
A. For personal projects and learning, requirements.txt is simple and sufficient. Consider pyproject.toml (with Poetry etc.) for team development or more advanced package management. For portfolio work, requirements.txt is more than enough.
08. Summary
When starting API testing with Python, getting the environment set up correctly is the critical first step. Install pytest and requests, configure a virtual environment, and you’ll be ready to start writing API tests right away.
This article covered the steps to set up an API testing environment in Python.
| Step | Command |
|---|---|
| Create virtual environment | python -m venv venv |
| Activate virtual environment | venv\Scripts\activate (Windows) |
| Install libraries | pip install requests pytest pytest-html |
| Configure pytest.ini | addopts = --html=report.html --self-contained-html |
| Verify setup | pytest test_check.py -v -s |
Once the environment is set up, the rest is smooth sailing. Master these three steps — creating a virtual environment, installing libraries, and configuring pytest.ini — and you’ll be ready to start writing API tests right away.

