Skip to main content

pytest-csv-params

pytest-csv-params

A pytest plugin to parametrize data-driven tests by CSV files.

PyPI - Downloads PyPI - Version PyPI - Status PyPI - Format

Requirements

  • Python 3.10, 3.11, 3.12, 3.13, 3.14, 3.15
  • pytest >=8.3,<=9.2

There's no operating system dependent 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 --group dev pytest-csv-params

... or uv ...

uv add --group dev pytest-csv-params

Documentation / User Guide

Detailed documentation can be found under docs.codebau.dev/pytest-plugins/pytest-csv-params/

Usage: Command Line Argument

Argument Required Description Example
--csv-params-base-dir no (optional) Define a base dir for all relative-path CSV data files (since 0.1.0) pytest --csv-params-base-dir /var/testdata
--csv-params-skip-missing no (optional) Let tests automatically skip when CSV source is missing (since 2.1.0) pytest --csv-params-skip-missing
--csv-params-skip-empty no (optional) Let tests automatically skip when CSV source does not contain any test data (since 2.1.0) pytest --csv-params-skip-empty

Usage: Decorator

Simply decorate your test method with @csv_params (pytest_csv_params.decorator.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); overrides the command line argument 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 Python CSV Documentation) csv.excel_tab
data_casts dict (optional) Cast Methods for the CSV Data (see "Data Casting" below) { "a": int, "b": float }
header_renames dict (optional) Replace headers from the CSV file, so that they can be used as parameters for the test function (since 0.3.0) { "Annual Amount of Bananas": "banana_count", "Cherry export price": "cherry_export_price" }
reuse_id_col bool (optional) Allows to re-use the ID column as test data column (since 1.3.0), defaults to False for backwards compatibility True
skip_when_missing_csv_file bool (optional) When the CSV data cannot be loaded, the test will be skipped (since 2.1.0), defaults to False for backwards compatibility True
skip_when_empty_csv_file bool (optional) When the CSV data file is empty, the test will be skipped (since 2.1.0), defaults to False for backwards compatibility True

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 (,)

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

Shorthand example (no ID col, only string values):

from pytest_csv_params.decorator import csv_params

@csv_params("/data/test-lib/cases/texts.csv")
def test_texts(text_a, text_b, text_c):
    assert f"{text_a}:{text_b}" == text_c

More complex example

This example features nearly all things the plugin has to offer. You find this example also in the test cases, see tests/test_complex_example.py.

The CSV file (tests/assets/example.csv):

"Test ID","Bananas shipped","Single Banana Weight","Apples shipped","Single Apple Weight","Container Size"
"Order-7","1503","0.5","2545","0.25","1500"
"Order-15","101","0.55","1474","0.33","550"

The Test (tests/test_complex_example.py):

from math import ceil
from os.path import join, dirname

from pytest_csv_params.decorator import csv_params


@csv_params(
    data_file="example.csv",
    base_dir=join(dirname(__file__), "assets"),
    id_col="Test ID",
    header_renames={
        "Bananas shipped": "bananas_shipped",
        "Single Banana Weight": "banana_weight",
        "Apples shipped": "apples_shipped",
        "Single Apple Weight": "apple_weight",
        "Container Size": "container_size",
    },
    data_casts={
        "bananas_shipped": int,
        "banana_weight": float,
        "apples_shipped": int,
        "apple_weight": float,
        "container_size": int,
    },
)
def test_container_size_is_big_enough(
    bananas_shipped: int, banana_weight: float, apples_shipped: int, apple_weight: float, container_size: int
) -> None:
    """
    This is just an example test case for the documentation.
    """

    gross_weight = (banana_weight * bananas_shipped) + (apple_weight * apples_shipped)
    assert ceil(gross_weight) <= container_size

If you decide not to rename the columns, the test would look like this:

@csv_params(
    data_file="example.csv",
    base_dir=join(dirname(__file__), "assets"),
    id_col="Test ID",
    data_casts={
        "Bananas_Shipped": int,
        "Single_Banana_Weight": float,
        "Apples_Shipped": int,
        "Single_Apple_Weight": float,
        "Container_Size": int,
    },
)
def test_container_size_is_big_enough(
    Bananas_Shipped: int, Single_Banana_Weight: float, Apples_Shipped: int, Single_Apple_Weight: float, Container_Size: int
) -> None:
    ...

Changelog

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.

See CONTRIBUTING.md for details.

Pull Requests

Pull requests are always welcome. Since this Gitea instance is not open to public, just send an e-mail to discuss options.

Any changes that are made are to be backed by tests. Please give me a sign if you're going to break the existing API and let us discuss ways to handle that.

See CONTRIBUTING.md for details.

Where are the sources?

The source code is available under git.codebau.dev/pytest-plugins/pytest-csv-params.

Metadata

Release files for pytest-csv-params 2.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-csv-params 2.1.1
File Size Uploaded
pytest_csv_params-2.1.1.tar.gz 13.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-csv-params 2.1.1
File Interpreter ABI Platform
pytest_csv_params-2.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 29.3 kB

Release files / pytest_csv_params-2.1.1.tar.gz

Download URL pytest_csv_params-2.1.1.tar.gz
Size 13.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f34b494491f08017b93db04aaf94d81b3f0a29ce6b1ea0a185a0fe40b613003c
BLAKE2b-256 checksum
How to use checksums
ddc866c1ac8e9c466b0e463b49aa14d557892fa261fc0bfd405e466afa62763b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / pytest_csv_params-2.1.1-py3-none-any.whl

Download URL pytest_csv_params-2.1.1-py3-none-any.whl
Size 15.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fbd7a94068798bac771d78e2bbecb2ac504040332cdb672838122031fe0779a1
BLAKE2b-256 checksum
How to use checksums
609a10911b89c4a63cb738057e7b871a906a686835456c1fedeeb06115bf91d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.22 {"installer":{"name":"uv","version":"0.12.22","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

2.4.0

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.6

2 release files

2.2.5

2 release files

2.2.3

2 release files

2.2.2

2 release files

2.2.0

2 release files

This release

2.1.1 This release

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.3.2

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.4

2 release files

0.0.3

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page