Package: pytest-csv-params
pytest-csv-paramsA pytest plugin to parametrize data-driven tests by CSV files.RequirementsPython 3.8, 3.9, 3.10, 3.11pytest >= 7.4There's no operating system dependent code in this plugin, so it should run anywhere where pytest runs.InstallationSimply install it with pip...pipinstallpytest-csv-params... or poetry ...poetryadd--devpytest-csv-paramsDocumentation / User GuideDetailed documentation can be found underdocs.codebau.dev/pytest-plugins/pytest-csv-params/Usage: Command Line ArgumentArgumentRequiredDescriptionExample--csv-params-base-dirno (optional)Define a base dir for all relative-path CSV data files (since 0.1.0)pytest --csv-params-base-dir /var/testdataUsage: DecoratorSimply decorate your test method with@csv_params(pytest_csv_params.decorator.csv_params) and the following parameters:ParameterTypeDescriptionExampledata_filestrThe CSV file to use, relative or absolute path"/var/testdata/test1.csv"base_dirstr(optional)Directory to look up relative CSV files (seedata_file); overrides the command line argumentjoin(dirname(__file__), "assets")id_colstr(optional)Column name of the CSV that contains test case IDs"ID#"dialectcsv.Dialect(optional)CSV Dialect definition (seePython CSV Documentation)csv.excel_tabdata_castsdict(optional)Cast Methods for the CSV Data (see "Data Casting" below){ "a": int, "b": float }header_renamesdict(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" }CSV FormatThe default CSV format is:\r\nas line endingAll non-numeric fields are surrounded by"If you need a"in the value, use""(double quote)Fields are separated by comma (,)Usage ExampleThis example uses the CSV example from above.frompytest_csv_params.decoratorimportcsv_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,},)deftest_addition(part_a,part_b,expected_result):assertpart_a+part_b==expected_resultShorthand example (no ID col, only string values):frompytest_csv_params.decoratorimportcsv_params@csv_params("/data/test-lib/cases/texts.csv")deftest_texts(text_a,text_b,text_c):assertf"{text_a}:{text_b}"==text_cMore complex exampleThis example features nearly all things the plugin has to offer. You find this example also in the test cases, seetests/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):frommathimportceilfromos.pathimportjoin,dirnamefrompytest_csv_params.decoratorimportcsv_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,},)deftest_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)assertceil(gross_weight)<=container_sizeIf 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,},)deftest_container_size_is_big_enough(Bananas_Shipped:int,Single_Banana_Weight:float,Apples_Shipped:int,Single_Apple_Weight:float,Container_Size:int)->None:...ChangelogA detailed changelog is here:docs.codebau.dev/pytest-plugins/pytest-csv-params/pages/changelog.htmlBugs etc.Please send your issues tocsv-params_issues(at)jued.de. Please include the following:Plugin Version usedPytest versionPython version with operating systemIt would be great if you could include example code that clarifies your issue.SeeCONTRIBUTING.mdfor details.Pull RequestsPull 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.SeeCONTRIBUTING.mdfor details.Where are the sources?The source code is available undergit.codebau.dev/pytest-plugins/pytest-csv-params.