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_BASE_DIR = 'set base dir for getting CSV data files from'¶
This is the help text for the base dir command line argument that is added by
pytest_addoption().
- _ptcsvp.cmdline.HELP_TEXT_SKIP_EMPTY = 'skip tests when csv source is empty'¶
This is the help text for the skip empty command line argument that is added by
pytest_addoption().
- _ptcsvp.cmdline.HELP_TEXT_SKIP_MISSING = 'skip tests when csv source is missing'¶
This is the help text for the skip missing command line argument that is added by
pytest_addoption().
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
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_idand the testdata.
- _ptcsvp.parametrize.add_parametrization(data_file, base_dir=None, id_col=None, data_casts=None, dialect=CsvParamsDefaultDialect, header_renames=None, reuse_id_col=False, skip_when_missing_csv_file=None, skip_when_empty_csv_file=None, parameters_as_dictionary=None, parameters_as_object=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 frombase_dir (
Optional[str]) – Optional base directory to look for non-absolutedata_fileinid_col (
Optional[str]) – Optional name of a column that shall be used to make the test IDs fromdata_casts (
Optional[dict[str,Callable[[str],Any]]]) – Methods to cast a column’s data into a format that is required for the testdialect (
type[Dialect]) – The CSV file dialect (CSV file format) to useheader_renames (
Optional[dict[str,str]]) – A dictonary mapping the header names from the CSV file to usable names for the testsreuse_id_col (
bool) – Allows re-using the ID col as test data column; defaults to False for backwards compatibilityskip_when_missing_csv_file (
Optional[bool]) – Let tests automatically skip when CSV source is missing (since 2.1.0)skip_when_empty_csv_file (
Optional[bool]) – Let tests automatically skip when CSV source does not contain any test data (since 2.1.0)parameters_as_dictionary (
Optional[str]) – Instead of using one argument per field, all arguments are passed as a dictionary with the given name (since 2.2.0)parameters_as_object (
UnionType[str,tuple[str,Callable[[],TestCaseDataElementType]],None]) – Instead of using one argument per field, all arguments are passed as an object with the given name, or with the given name and created by the given factory (since 2.5.0)
- Raises:
ParametersObjectConfiguration – When parameters_as_dictionary and parameters_as_object are used at the same time
- Return type:
- 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:
- Return type:
- 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 asdata_file)data_file (
str) – The CSV file to read. If this is an absolute path,base_dirwill be ignored; if not, thebase_diris prepended.dialect (
type[Dialect]) – The CSV file dialect (definition of the format of a CSV file).
- Return type:
- 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
Pluginto 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
- _ptcsvp.varname.make_name_valid(name, replacement_char='_')¶
Make a name a valid name by replacing invalid chars with the as
replacement_chargiven char- Parameters:
- Return type:
- 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=(8, 3), deprecation_below=(9, 0))¶
Check if the current version is at least 8.3
- Parameters:
- Raises:
PytestVersionTooOldException – When the pytest version is too old/unsupported
- Return type:
- _ptcsvp.version.check_python_version(min_version=(3, 10), deprecation_below=(3, 11))¶
Check if the current version is at least 3.10
- Parameters:
- Raises:
PythonVersionTooOldException – When the python version is too old/unsupported
- Return type: