Command Line Argument for setting the base dir
This commit is contained in:
parent
01a0764c1f
commit
bcc67be373
|
@ -443,7 +443,7 @@ max-attributes=7
|
|||
max-bool-expr=5
|
||||
|
||||
# Maximum number of branch for function / method body.
|
||||
max-branches=12
|
||||
max-branches=16
|
||||
|
||||
# Maximum number of locals for function / method body.
|
||||
max-locals=20
|
||||
|
|
35
README.md
35
README.md
|
@ -2,6 +2,8 @@
|
|||
|
||||
A pytest plugin to parametrize tests by CSV files.
|
||||
|
||||
[:toc:]
|
||||
|
||||
## Requirements
|
||||
|
||||
- Python 3.8, 3.9 or 3.10
|
||||
|
@ -23,19 +25,34 @@ pip install pytest-csv-params
|
|||
poetry add --dev pytest-csv-params
|
||||
```
|
||||
|
||||
## Usage
|
||||
## 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`) | `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 }` |
|
||||
| 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](https://docs.python.org/3/library/csv.html#dialects-and-formatting-parameters)) | `csv.excel_tab` |
|
||||
| `data_casts` | `dict` (optional) | Cast Methods for the CSV Data (see "Data Casting" below) | `{ "a": int, "b": float }` |
|
||||
|
||||
[1] [Python CSV Documentation](https://docs.python.org/3/library/csv.html#dialects-and-formatting-parameters)
|
||||
### 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
|
||||
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
"""
|
||||
Command Line Options
|
||||
"""
|
||||
|
||||
|
||||
HELP_TEXT = "set base dir for getting CSV data files from"
|
||||
|
||||
|
||||
def pytest_addoption(parser, plugin_name="csv-params"):
|
||||
"""
|
||||
Add Command Line Arguments for pytest execution
|
||||
"""
|
||||
|
||||
group = parser.getgroup(plugin_name)
|
||||
group.addoption(
|
||||
f"--{plugin_name}-base-dir",
|
||||
action="store",
|
||||
type=str,
|
||||
default=None,
|
||||
required=False,
|
||||
help=HELP_TEXT,
|
||||
)
|
|
@ -7,6 +7,7 @@ from typing import List, Optional, TypedDict
|
|||
|
||||
import pytest
|
||||
|
||||
from _ptcsvp.plugin import BASE_DIR_KEY, Plugin
|
||||
from pytest_csv_params.dialect import CsvParamsDefaultDialect
|
||||
from pytest_csv_params.exception import (
|
||||
CsvParamsDataFileInaccessible,
|
||||
|
@ -61,6 +62,8 @@ def add_parametrization(
|
|||
"""
|
||||
Get data from the files and add things to the tests
|
||||
"""
|
||||
if base_dir is None:
|
||||
base_dir = getattr(Plugin, BASE_DIR_KEY, None)
|
||||
csv_lines = read_csv(base_dir, data_file, dialect)
|
||||
if len(csv_lines) < 2:
|
||||
raise CsvParamsDataFileInvalid("File does not contain a single data row") from None
|
||||
|
|
|
@ -3,6 +3,9 @@ The main Plugin implementation
|
|||
"""
|
||||
|
||||
|
||||
BASE_DIR_KEY = "__pytest_csv_plugins__config__base_dir"
|
||||
|
||||
|
||||
class Plugin: # pylint: disable=too-few-public-methods
|
||||
"""
|
||||
Plugin Class
|
||||
|
@ -12,4 +15,4 @@ class Plugin: # pylint: disable=too-few-public-methods
|
|||
"""
|
||||
Hold the pytest config
|
||||
"""
|
||||
self.config = config
|
||||
setattr(Plugin, BASE_DIR_KEY, config.option.csv_params_base_dir)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[tool.poetry]
|
||||
name = "pytest-csv-params"
|
||||
version = "0.0.4"
|
||||
version = "0.1.0"
|
||||
description = "Pytest plugin for Test Case Parametrization with CSV files"
|
||||
authors = ["Juergen Edelbluth <csv_params@jued.de>"]
|
||||
license = "MIT"
|
||||
|
@ -11,7 +11,7 @@ keywords = [
|
|||
"py.test", "pytest", "csv", "params", "parametrize", "pytest-plugin",
|
||||
]
|
||||
classifiers = [
|
||||
"Development Status :: 3 - Alpha",
|
||||
"Development Status :: 4 - Beta",
|
||||
"Environment :: Plugins",
|
||||
"Framework :: Pytest",
|
||||
"Intended Audience :: Developers",
|
||||
|
@ -80,6 +80,7 @@ omit = [
|
|||
"*pip-build-env*",
|
||||
"*/test_plugin_test_multiplication.py",
|
||||
"*/test_plugin_test_error.py",
|
||||
"*/test_base_dir_param.py",
|
||||
]
|
||||
relative_files = true
|
||||
|
||||
|
@ -97,11 +98,16 @@ exclude_lines = [
|
|||
legacy_tox_ini = """
|
||||
[tox]
|
||||
minversion = 3.25.0
|
||||
envlist = py38,py39,py310
|
||||
envlist = clean,py38,py39,py310
|
||||
|
||||
[testenv]
|
||||
commands =
|
||||
pytest
|
||||
pytest --cov-append
|
||||
|
||||
[testenv:clean]
|
||||
skip_install = true
|
||||
commands =
|
||||
coverage erase
|
||||
"""
|
||||
|
||||
[tool.black]
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
Pytest Plugin Entrypoint
|
||||
"""
|
||||
|
||||
from _ptcsvp.cmdline import pytest_addoption as _pytest_addoption
|
||||
from _ptcsvp.configure import pytest_configure as _pytest_configure
|
||||
from _ptcsvp.configure import pytest_unconfigure as _pytest_unconfigure
|
||||
from _ptcsvp.version import check_pytest_version, check_python_version
|
||||
|
@ -13,3 +14,6 @@ check_pytest_version()
|
|||
# Basic config
|
||||
pytest_configure = _pytest_configure
|
||||
pytest_unconfigure = _pytest_unconfigure
|
||||
|
||||
# Command Line Arguments
|
||||
pytest_addoption = _pytest_addoption
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
"""
|
||||
Test the usage of the Command Line
|
||||
"""
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from _ptcsvp.cmdline import HELP_TEXT
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
["base_dir"],
|
||||
[
|
||||
(True,),
|
||||
(False,),
|
||||
],
|
||||
)
|
||||
def test_base_dir_param(pytester, base_dir, simple_test_csv, simple_fruit_test):
|
||||
"""
|
||||
Test that the cmd arg is valued
|
||||
"""
|
||||
|
||||
csv_file = str(pytester.makefile(".csv", simple_test_csv).absolute())
|
||||
|
||||
parameters = ["-p", "no:bandit"]
|
||||
if base_dir:
|
||||
path = Path(csv_file)
|
||||
parameters.extend(["--csv-params-base-dir", str(path.parent.absolute())])
|
||||
csv_file = path.name
|
||||
|
||||
pytester.makepyfile(simple_fruit_test(csv_file))
|
||||
|
||||
result = pytester.runpytest(*parameters)
|
||||
result.assert_outcomes(passed=3, failed=1)
|
||||
|
||||
|
||||
def test_help(pytester):
|
||||
"""
|
||||
Test if the plugin is in the help
|
||||
"""
|
||||
|
||||
result = pytester.runpytest("--help")
|
||||
index_csv_params = -1
|
||||
index_minus_minus = -1
|
||||
index_help = -1
|
||||
|
||||
for index, line in enumerate(result.stdout.lines):
|
||||
if "csv-params:" in line and index_csv_params < 0:
|
||||
index_csv_params = index
|
||||
continue
|
||||
if "--csv-params-base-dir" in line and index_minus_minus < 0:
|
||||
index_minus_minus = index
|
||||
if HELP_TEXT in line and index_help < 0:
|
||||
index_help = index
|
||||
|
||||
assert index_csv_params >= 0
|
||||
assert index_csv_params < index_minus_minus <= index_help
|
Loading…
Reference in New Issue
Block a user