This repository is an example project designed to demonstrate how to set up and use pipenv
for dependency management and pytest
for testing in Python. It includes configuration for code coverage, linting, and formatting, making it an ideal starting point for Python projects with best practices.
- Introduction
- Project Structure
- Requirements
- Setup and Installation
- Running Tests
- Code Coverage
- Linting and Formatting
- Example Usage
- Troubleshooting
This repository is intended as a tutorial and starting point for those looking to implement pytest
and pipenv
in Python projects. By following this guide, you'll learn:
- How to use
pipenv
to manage dependencies and virtual environments. - How to set up and configure
pytest
for running tests. - How to configure code coverage using
pytest-cov
. - How to use
flake8
andblack
for linting and formatting, ensuring code quality.
This project includes an example weather service API function (weather_service.py
) and corresponding unit tests with mocked responses, showing how to handle external API dependencies in tests.
The project is organized as follows:
python-test/
├── src/
│ └── weather_service.py # Main module for fetching weather data
├── tests/
│ └── test_weather_service.py # Unit tests for the weather service
├── Pipfile # Pipenv configuration for dependencies
└── README.md # Project documentation
- src/: Contains the main module (
weather_service.py
) that simulates fetching weather data from an API. - tests/: Contains unit tests with mocked API responses for testing
weather_service.py
. - Pipfile: Defines project dependencies, dev dependencies, and script commands for pipenv.
- Python 3.10 or higher
- Pipenv for dependency management
-
Install Pipenv If Pipenv is not installed, install it globally using:
pip install pipenv
-
Clone the Repository Clone this project repository to your local machine:
git clone [email protected]:jonmatum/pipenv-pytest-example.git cd pipenv-pytest-example
-
Install Dependencies Use Pipenv to install all necessary packages:
pipenv install --dev
This installs both regular and development dependencies (such as
pytest
,flake8
, andblack
). -
Activate the Virtual Environment Activate the virtual environment created by Pipenv:
pipenv shell
The project uses pytest
for testing and pytest-cov
for code coverage. Tests are written with mocks to simulate API responses and handle exceptions.
-
Run All Tests Run the tests using the following command:
pipenv run test
-
Test Command Explanation The test command in the
Pipfile
is configured as follows:[scripts] test = "env PYTHONPATH=src pytest --cov=src --cov-report=term-missing --cov-report=html"
--cov=src
: Specifies thesrc
directory for measuring test coverage.--cov-report=term-missing
: Displays a report in the terminal, showing lines that are not covered.--cov-report=html
: Generates an HTML report that can be found in thehtmlcov
directory after running the command.
-
Run Coverage Report Run the tests with coverage by executing:
pipenv run test
-
View HTML Coverage Report After running the test command, open the coverage report by navigating to
htmlcov/index.html
in your browser to view a detailed, line-by-line report.
This project uses flake8
for linting and black
for formatting to maintain code quality and consistency.
-
Run Linting (flake8) Check the code for PEP 8 compliance and common issues:
pipenv run lint
The lint command in the
Pipfile
is configured as:lint = "flake8 ."
-
Run Formatting (black) Format the code automatically using
black
:pipenv run format
The format command in the
Pipfile
is configured as:format = "black ."
To fetch weather data from the API in weather_service.py
, you can call the get_weather
function with a city name:
from src.weather_service import get_weather
temperature = get_weather("New York", api_key="your_api_key")
print(f"The temperature in New York is {temperature}°C")
This example demonstrates handling external API dependencies by using mock data in the tests, allowing you to understand how to structure tests for functions with external dependencies.
- Module Not Found Error: Ensure
PYTHONPATH=src
is correctly set in thePipfile
test script. - Request Timeout: The API call in
weather_service.py
has a timeout set; adjust this as necessary depending on network conditions.