Skip to main content

Pytest plugin for Test Case Parametrization with CSV files

Project description

pytest-csv-params

A pytest plugin to parametrize tests by CSV files.

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

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: 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

Usage: Decorator

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); 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 }

CSV File Lookup Order

CSV files are looked up following this rules:

  • If the data_file parameter is an absolute path, this is used, regardless of the base_dir parameter or command line argument.
  • If the data_file parameter is relative:
    • If the base_dir parameter is set, the file is looked up there, regardless of the command line argument
    • If the base_dir parameter is not set (or None):
      • If the command line argument is set, the file is looked up there
      • If the command line argument is not set, the file is looked up in the current working directory

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

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

Breaking Changes

Version 0.2.0

  • The parameter order for pytest_csv_params.decorator.csv_params changed to allow the shorthand usage with only a data_file as positional argument. If you used keyword arguments only (like the docs recommend), you will not run into trouble.

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) or poetry 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.

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.

Quality Measures

Backed with pytest plugins:

  • Pylint (static code analysis and best practises)
  • black (code formatting standards)
  • isort (keep imports sorted)
  • Bandit (basic static security tests)
  • mypy (typechecking)

Please to a complete pytest run (poetry run pytest), and consider running it on all supported platforms with (poetry run tox).

License

Code is under MIT license. See LICENSE.txt for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-csv-params-0.2.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

pytest_csv_params-0.2.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file pytest-csv-params-0.2.0.tar.gz.

File metadata

  • Download URL: pytest-csv-params-0.2.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.6 Linux/4.4.180+

File hashes

Hashes for pytest-csv-params-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1a275872a395da42471b560925285a061eeb2fca757f0870b19f4f1930170350
MD5 826533221d632607805080ab973596f9
BLAKE2b-256 f67a82d569320de38aeb0d554c7ad4213948f8bff38892df130dd96cbe334ae2

See more details on using hashes here.

File details

Details for the file pytest_csv_params-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_csv_params-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1986502ae44b55a2fb19eaa110ec422170d352eb9381ff0e8a74011e464de6c6
MD5 e36897b04b5056ce18e9310ba7742806
BLAKE2b-256 32f4d3687df90efbb5f4cc7fc70bf8498818ca34526b144dbda016be17ab85e6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page