Pytest plugin for Test Case Parametrization with CSV files
Project description
pytest-csv-params
A pytest plugin to parametrize tests by CSV files.
Requirements
- Python 3.8, 3.9 or 3.10
- pytest >= 7.1
There's no operating system dependend code in this plugin, so it should run anywhere where pytest runs.
Installation
Simply install it with pip...
pip install pytest-csv-params
... or poetry ...
poetry add --dev pytest-csv-params
Usage
Simply decorate your test method with @csv_params
and the following parameters:
Parameter | Type | Description | Example |
---|---|---|---|
data_file |
str |
The CSV file to use, relative or absolute path | /var/testdata/test1.csv |
base_dir |
str (optional) |
Directory to look up relative CSV files (see data_file ) |
join(dirname(__file__), "assets") |
id_col |
str (optional) |
Column name of the CSV that contains test case IDs | ID# |
dialect |
csv.Dialect (optional) |
CSV Dialect definition (see [1]) | csv.excel_tab |
data_casts |
dict (optional) |
Cast Methods for the CSV Data (see "Data Casting" below) | { "a": int, "b": float } |
Data Casting
When data is read from CSV, they are always parsed as str
. If you need them in other formats, you can set a method that should be called with the value.
These methods can also be lambdas, and are also good for further transformations.
Data Casting Example
from pytest_csv_params.decorator import csv_params
def normalize(x: str) -> str:
return x.strip().upper()
@csv_params(
data_file="/test/data.csv",
data_casts={
"col_x": normalize,
"col_y": float,
},
)
def test_something(col_x, col_y):
# Test something...
...
CSV Format
The default CSV format is:
\r\n
as line ending- All non-numeric fields are surrounded by
"
- If you need a
"
in the value, use""
(double quote) - Fields are separated by comma (
,
)
Example CSV
"ID#", "part_a", "part_b", "expected_result"
"first", 1, 2, 3
"second", 3, 4, 7
"third", 10, 11, 21
Usage Example
This example uses the CSV example from above.
from pytest_csv_params.decorator import csv_params
@csv_params(
data_file="/data/test-lib/cases/addition.csv",
id_col="ID#",
data_casts={
"part_a": int,
"part_b": int,
"expected_result": int,
},
)
def test_addition(part_a, part_b, expected_result):
assert part_a + part_b == expected_result
Contributing
Build and test
You need Poetry in order to build this project.
Tests are implemented with pytest
, and tox
is used to orchestrate them for the supported python versions.
- Checkout this repo
- Run
poetry install
- Run
poetry run tox
(for all supported python versions) orpoetry run pytest
(for your current version)
Bugs etc.
Please send your issues to csv-params_issues
(at) jued.de
. Please include the following:
- Plugin Version used
- Pytest version
- Python version with operating system
It would be great if you could include example code that clarifies your issue.
Pull Requests
Pull requests are always welcome. Since this Gitea instance is not open to public, just send an e-mail to discuss options.
License
Code is under MIT license. See LICENSE.txt
for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest-csv-params-0.0.4.tar.gz
.
File metadata
- Download URL: pytest-csv-params-0.0.4.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.6 Linux/4.4.180+
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 017197a62ced59d1a7c3bbf7f784a672116259ae920769f8f2e578473e47e3c2 |
|
MD5 | b5658b26c9e39fc785019a9ab4f16666 |
|
BLAKE2b-256 | 4852b2bb0b180b1ff4f5a1584669f25ff880e7294ab7513f68b5a62375eeeb3b |
File details
Details for the file pytest_csv_params-0.0.4-py3-none-any.whl
.
File metadata
- Download URL: pytest_csv_params-0.0.4-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.6 Linux/4.4.180+
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2ddf004db92969498ca1978eae9af3e20ddb3bf9b3aa6940d8b99af9558ee57 |
|
MD5 | 062abd4f41f65aef3e203c4fc712c12b |
|
BLAKE2b-256 | bcba94fcc7b27f33b4320c888ca3e519167f41e8ef853d3f32a26d2158caec2d |