Private Elements

Command Line Arguments

Module: _ptcsvp.cmdline

This pytest plugin requires command line arguments that are parsed from the pytest framework. This module contains code to instruct pytest to deliver the required values.

_ptcsvp.cmdline.HELP_TEXT = 'set base dir for getting CSV data files from'

This is the help text for the command line arguments that is added by pytest_addoption().

_ptcsvp.cmdline.pytest_addoption(parser, plugin_name='csv-params')

Entrypoint for pytest to extend the own Parser with the things we need extra.

Parameters:
  • parser (Parser) – The pytest command line argument parser

  • plugin_name (str) – The name of our plugin, with default value

Return type:

None

Plugin Configuration

Module: _ptcsvp.configure

The pytest plugin needs a setup (pytest_configure()) and a teardown (pytest_unconfigure()) method registered. This module contains the required methods for that.

_ptcsvp.configure.pytest_configure(config, plugin_name='csv_params')

Register our Plugin

Parameters:
  • config (Config) – Pytets configuration class

  • plugin_name (str) – The name of the pytest plugin, with default value

Return type:

None

_ptcsvp.configure.pytest_unconfigure(config, plugin_name='csv_params')

Remove our Plugin

Parameters:
  • config (Config) – Pytest configuration class

  • plugin_name (str) – The name of the pytest plgin, with default value

Return type:

None

The Parametrization

Module: _ptcsvp.parametrize

Parametrize a test function by CSV file

class _ptcsvp.parametrize.TestCaseParameters

Type for a single test case. Contains the optional test_id and the test data.

data: List[Any]
test_id: Optional[str]
_ptcsvp.parametrize.add_parametrization(data_file, base_dir=None, id_col=None, data_casts=None, dialect=CsvParamsDefaultDialect, header_renames=None)

Parametrize a test function with data from a CSV file.

For the public decorator, see pytest_csv_params.decorator.csv_params().

Parameters:
  • data_file (str) – The CSV file to read the data from

  • base_dir (Optional[str]) – Optional base directory to look for non-absolute data_file in

  • id_col (Optional[str]) – Optional name of a column that shall be used to make the test IDs from

  • data_casts (Optional[Dict[str, Callable[[str], Any]]]) – Methods to cast a column’s data into a format that is required for the test

  • dialect (Type[Dialect]) – The CSV file dialect (CSV file format) to use

  • header_renames (Optional[Dict[str, str]]) – A dictonary mapping the header names from the CSV file to usable names for the tests

Return type:

MarkDecorator

Returns:

pytest.mark.parametrize() mark decorator, filled with all the data from the CSV.

_ptcsvp.parametrize.clean_headers(current_headers, replacing)

Clean the CSV file headers

Parameters:
  • current_headers (List[str]) – List of the current headers, as read from the CSV file (without the ID column, as far as it exists)

  • replacing (Optional[Dict[str, str]]) – Dictionary of replacements for headers

Return type:

List[str]

Returns:

List of cleaned header names

Raises:

CsvHeaderNameInvalid – When non-unique names appear

_ptcsvp.parametrize.read_csv(base_dir, data_file, dialect)

Get Data from CSV

Parameters:
  • base_dir (Optional[str]) – Optional directory to look up non-absolute CSV files (given as data_file)

  • data_file (str) – The CSV file to read. If this is an absolute path, base_dir will be ignored; if not, the base_dir is prepended.

  • dialect (Type[Dialect]) – The CSV file dialect (definition of the format of a CSV file).

Return type:

List[List[str]]

Returns:

A list of rows, each row contains a list of columns; all type str

The Plugin Class

Module: _ptcsvp.plugin

This module contains the main plugin class. By the time of writing, it is quite unspectacular.

_ptcsvp.plugin.BASE_DIR_KEY = '__pytest_csv_params__config__base_dir'

The class attribute key for Plugin to store the base dir command line argument value.

class _ptcsvp.plugin.Plugin(config)

The main plugin class

Currently, this class is nothing more than the keeper of the value of the command line argument (as defined by _ptcsvp.cmdline.pytest_addoption().

The Variable Name Validation

Module: _ptcsvp.varname

This module contains code to validate variable/argument/parameter names or to make them valid ones.

_ptcsvp.varname.VALID_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'

Valid characters a variable/parameter/argument name can consist of

_ptcsvp.varname.VARIABLE_NAME = re.compile('^[a-zA-Z_][A-Za-z0-9_]{0,1023}$')

Regular expression that defines a valid variable/parameter/argument name

_ptcsvp.varname.is_valid_name(name)

Checks if the variable name is valid

Parameters:

name (str) – The name to be checked

Return type:

bool

Returns:

True, when the name is valid

_ptcsvp.varname.make_name_valid(name, replacement_char='_')

Make a name a valid name by replacing invalid chars with the as replacement_char given char

Parameters:
  • name (str) – The name to make a valid one

  • replacement_char (str) – The char to replace invalid chars with, default is an underscore _

Return type:

str

Returns:

A valid name

Raises:

CsvHeaderNameInvalid – If the fixed name is still an invalid name

The Version Checks

Module: _ptcsvp.version

This module contains two methods to check if the python version is recent enough (check_python_version()) and if the pytest version is recent enough (check_pytest_version()).

During the setup phase of the plugin (see pytest_csv_params.plugin) these methods are called.

_ptcsvp.version.check_pytest_version(min_version=(7, 4))

Check if the current version is at least 7.4

Parameters:

min_version (Tuple[int, int]) – The minimum version required, as tuple, default is 7.4

Raises:

RuntimeError – When the pytest version is too old/unsupported

Return type:

None

_ptcsvp.version.check_python_version(min_version=(3, 8))

Check if the current version is at least 3.8

Parameters:

min_version (Tuple[int, int]) – The minimum version required, as tuple, default is 3.8

Raises:

PythonTooOldError – When the python version is too old/unsupported

Return type:

None