Tests¶
This page should give you an overview over all tests for this plugin.
Global conftest.py
¶
Module: tests.conftest
Project global fixtures, plugins etc.
Standard Tests¶
Test for header cleaning¶
Module: tests.test_clean_headers
- tests.test_clean_headers.test_header_cleaning(current_headers, replacing, expect_exception, expect_message, expect_result)¶
This test case tests mainly the
_ptcsvp.parametrize.clean_headers()
method.There are many single test cases built with parametrization.
- Parameters:
- Return type:
Test for the internal parametrization feature¶
Module: tests.test_parametrize
- tests.test_parametrize.test_parametrization(csv_file, id_col, result, ids, expect_exception, expect_message)¶
This test case tests mainly the internal
_ptcsvp.parametrize.add_parametrization()
method, which is the backbone of the publiccsv_params()
decorator.The test is heavily parametrized. See source code for detail.
- Parameters:
csv_file (
str
) – CSV file for the testid_col (
Optional
[str
]) – The ID column name of the CSV fileresult (
Optional
[Tuple
[List
[str
],List
[List
[str
]]]]) – Expected result, as it would be handed over to thepytest.mark.parametrize()
mark decoratorexpect_exception (
Optional
[Type
[Exception
]]) – Expected exception during callexpect_message (
Optional
[str
]) – Expected exception message during call
- Return type:
Test the reading of CSV files¶
Module: tests.test_read_csv
- tests.test_read_csv.test_csv_reader(csv_file, base_dir, expect_lines, expect_exception, expect_message)¶
This test case tests several CSV reading scenarios (by parametrization). CSV test files are in the
tests/assets
folder. The tests target the_ptcsvp.parametrize.read_csv()
method.- Parameters:
csv_file (
str
) – The file to test withbase_dir (
Optional
[str
]) – The base dir to load thecsv_file
fromexpect_lines (
Optional
[int
]) – Expected read lines from the CSV fileexpect_exception (
Optional
[Type
[Exception
]]) – Expected exception when running the methodexpect_message (
Optional
[str
]) – Expected exception message when running the method
- Return type:
Test the header name handling¶
Module: tests.test_varname
The tests in this module aim at testing the validation and cleaning of header/column names of CSV files. Those names serve as arguments to test methods, and must therefore be valid and not shadow builtin names. Reserved names are checked also.
- tests.test_varname.test_310_names(name)¶
There are a few names that are not valid when using python 3.10 and above. This parametrized test checks if they are marked as invalid by the method
_ptcsvp.varname.is_valid_name()
.This test will be skipped on python versions below 3.10.
- tests.test_varname.test_is_valid_name(var_name, is_valid)¶
This test case checks that the method
_ptcsvp.varname.is_valid_name()
gives the right results. The test method is parametrized.
- tests.test_varname.test_make_name_valid(var_name, valid_var_name, raises_error)¶
This test case checks the method
_ptcsvp.varname.make_name_valid()
builds valid names or throws matching exceptions if not possible. Therefore, it is parametrized.
Test the checks for required versions¶
Module: tests.test_version_checks
Checking the versions this plugin depends on is crucial for the correct function.
- tests.test_version_check.build_version(p_version)¶
Test helper method: Build a version Tuple of a given version string. It is used by the
test_python_version()
test case.
- tests.test_version_check.test_pytest_version(mocker, p_version, expect_error)¶
Test if the pytest version is correctly recognized and if a too old version raises an exception. This test focuses on the
_ptcsvp.version.check_pytest_version()
method and is parametrized with a lot of different version strings.This test uses mocking.
- tests.test_version_check.test_python_version(mocker, p_version, expect_error)¶
Test if the python version is correctly recognized and if old versions raise an exception. This tests mainly the
_ptcsvp.version.check_python_version()
method and is parametrized with a lot of different version combos.This test uses mocking, and the
build_version()
helper method.
Plugin Tests¶
These tests test the plugin code by inserting the plugin into a test pytest instance.
Plugin conftest.py
¶
Module: tests.plugin.conftest
Local configuration and fixture providing for the Plugin tests
- tests.plugin.conftest.bad_test_csv()¶
Test fixture: Bad CSV
- Return type:
- Returns:
Bad CSV data as string
- tests.plugin.conftest.get_csv(csv)¶
Helper Method: Read CSV file from the tests assets directory under
tests/plugin/assets
.
- tests.plugin.conftest.install_plugin_locally(pytestconfig)¶
Auto-use Test Fixture to install our plugin in the test environment, so that it can be used with the
pytester
fixture. The package is removed after the test session automatically.
- tests.plugin.conftest.simple_fruit_test()¶
Test Fixture: Template of a simple test case
- tests.plugin.conftest.simple_test_csv()¶
Test fixture: Good simple CSV
- Return type:
- Returns:
CSV data as string
- tests.plugin.conftest.simple_text_test()¶
Test Fixture: Template of a simple text test case
Tests¶
Command line argument handling¶
Module: tests.plugin.test_cmd_line
- tests.plugin.test_cmd_line.test_base_dir_param(pytester, base_dir, simple_test_csv, simple_fruit_test)¶
Test if the
--csv-params-base-dir
command line argument is valued. For laziness, it uses a poor parametrization.- Parameters:
pytester (
Pytester
) – Pytester fixturebase_dir (
bool
) – Shall the base dir parameter be set?simple_test_csv (
str
) – Fixturesimple_test_csv()
simple_fruit_test (
Callable
[[str
],str
]) – Fixturesimple_fruit_test()
- Return type:
- tests.plugin.test_cmd_line.test_help(pytester)¶
Test that the pytest help now contains our command line argument with our help text.
- Parameters:
pytester (
Pytester
) – Pytester fixture- Return type:
Plugin Calls¶
Module: tests.plugin.test_plugin
- tests.plugin.test_plugin.test_plugin_all_tests_at_once(pytester, text_test_csv, bad_test_csv, simple_test_csv, simple_fruit_test, simple_text_test)¶
This is a meta test to check if multiple files would work also. Basically, it’s a combination of all the other plugin invocation tests of the module
tests.plugin.test_plugin
.We can’t run the error-prone test here, because it would stop all tests.
- Parameters:
pytester (
Pytester
) – Pytester fixturetext_test_csv (
str
) – Fixturetext_test_csv()
bad_test_csv (
str
) – Fixturebad_test_csv()
text_test_csv – Fixture
text_test_csv()
simple_fruit_test (
Callable
[[str
,str
],str
]) – Fixturesimple_fruit_test()
simple_text_test (
Callable
[[str
],str
]) – Fixturesimple_text_test()
- Return type:
- tests.plugin.test_plugin.test_plugin_test_error(pytester, bad_test_csv, simple_fruit_test)¶
Test if a test error is correctly recognized
- Parameters:
pytester (
Pytester
) – Pytester fixturebad_test_csv (
str
) – Fixturebad_test_csv()
simple_fruit_test (
Callable
[[str
],str
]) – Fixturesimple_fruit_test()
- Return type:
- tests.plugin.test_plugin.test_plugin_test_multiplication(pytester, simple_test_csv, simple_fruit_test)¶
Test a simple round trip (positive test case)
- Parameters:
pytester (
Pytester
) – Pytester fixturesimple_test_csv (
str
) – Fixturesimple_test_csv()
simple_fruit_test (
Callable
[[str
],str
]) – Fixturesimple_fruit_test()
- Return type:
- tests.plugin.test_plugin.test_plugin_test_text_shorthand(pytester, text_test_csv, simple_text_test)¶
Test the shorthand version of the plugin’s decorator
- Parameters:
pytester (
Pytester
) – Pytester fixturetext_test_csv (
str
) – Fixturetext_test_csv()
simple_text_test (
Callable
[[str
],str
]) – Fixturesimple_text_test()
- Return type:
POC Tests¶
POC conftest.py
¶
Module: tests.poc.conftest
Local configuration and fixture providing for POC tests
- class tests.poc.conftest.CheapCounter¶
A simple cheap counter that is required for counting executions
- classmethod get_value(counter)¶
Get the value of the counter
- tests.poc.conftest.cheap_counter()¶
Deliver a simple counter as fixture
- Return type:
- Returns:
The Cheap Counter Class
Tests¶
Pytest feature: Parametrization¶
Module: tests.poc.test_parametrize_with_generator
We are using a pytest feature heavily: Parametrization. These tests make sure this feature works still as expected.
Tests in this module run in a predefined order!
- tests.poc.test_parametrize_with_generator.data_generator()¶
Helper method: Create Test Data, but keep them as a generator
This helper is used by
test_2_generator_parametrize()
.
- tests.poc.test_parametrize_with_generator.test_1_simple_parametrize(val_a, val_b, val_c, cheap_counter)¶
Test the simple parametrization from pytest.
- Parameters:
val_a (
str
) – Test value Aval_b (
str
) – Test value Bval_c (
str
) – Test value Ccheap_counter (
Type
[CheapCounter
]) – Fixturecheap_counter()
- Return type:
- tests.poc.test_parametrize_with_generator.test_2_generator_parametrize(val_a, val_b, val_c, cheap_counter)¶
Test the generator parametrization from pytest.
- Parameters:
val_a (
str
) – Test value Aval_b (
str
) – Test value Bval_c (
str
) – Test value Ccheap_counter (
Type
[CheapCounter
]) – Fixturecheap_counter()
- Return type:
- tests.poc.test_parametrize_with_generator.test_3_evaluation(cheap_counter)¶
Evaluate the values of the
cheap_counter()
fixture.- Parameters:
cheap_counter (
Type
[CheapCounter
]) – Fixturecheap_counter()
- Return type:
Examples¶
Example Code for a test case for the documentation site¶
Module: tests.test_docs_example
This is the test code for the documentation site’s User guide. It contains everything that’s needed to follow the example – and makes sure the code example is working.
- tests.test_docs_example.get_dimensions(dimensions_str)¶
Read the dimensions from a string. A helper method to build the dimensions tuple.
- tests.test_docs_example.get_smallest_possible_container(number_of_items, dimensions_of_item, available_container_sizes=(1000, 2500, 7500))¶
This is the method to be tested. It searches for the smallest possible container after calculating the volume of the things to be loaded into the container. A container can only contain items of one product, so it is enough to know about the size of a single product, and how many of them need to be stored in a container.
The method raises a
ValueError
when the items do not fit any container.- Parameters:
- Raises:
ValueError – When no matching container can be found
- Return type:
- tests.test_docs_example.test_get_smallest_possible_container(number_of_items, dimensions_of_item, expected_container_size, expect_exception, expected_message)¶
This is the test method for the documentation.
- Parameters:
- Return type:
Example Code for a blog post on juergen.rocks¶
Module: tests.test_blog_example
This is a test example for a blog post on juergen.rocks.
The example consists of serval helper methods and a lot of configuration for the csv_params()
decorator.
The CSV file looks like this:
"Order-Ref #", "Anz. Schrauben-Päck.", "Dim. Schrauben-Päck.", "Anz. Scheiben-Päck.", "Dim. Scheiben-Päck.", "Volumen Container"
"221-12-A-24", "670", "30 x 50 x 70 mm", "150", "40 x 50 x 70 mm", "1 mÂł"
"281-13-C-15", "5000", "30 x 50 x 70 mm", "10000", "40 x 50 x 70 mm", "5 mÂł"
"281-13-C-76", "50000", "35 x 55 x 75 mm", "5000", "50 x 60 x 90 mm", "10 mÂł"
You find the CSV file in tests/assets/blog-example.csv
.
- tests.test_blog_example.get_container_volume(container_size)¶
Get the container size (remove the unit, as mmÂł).
Helper method, will be used as a data caster.
- tests.test_blog_example.get_volume(size_data)¶
Get the volume from size data, return it as mmÂł.
Helper method, will be used as a data caster.
- tests.test_blog_example.test_does_it_fit(anz_schrauben, vol_schrauben, anz_scheiben, vol_scheiben, vol_container)¶
A test example that tries to figure out if all the Schrauben and Scheiben fit in the container, and if the smallest possible container is chosen.
- Parameters:
anz_schrauben (
int
) – Number of Schraubenpäckchenvol_schrauben (
int
) – Volume (mm³) of a single Schraubenpäckchenanz_scheiben (
int
) – Number of Scheibenpäckchenvol_scheiben (
int
) – Volume (mm³) of a single Scheibenpäckchenvol_container (
int
) – Volume (mm³) of the selected container
- Return type:
Example Code for a test case for the README.md documentation¶
Module: tests.test_complex_example
This module contains a quite simple, yet complex configured test to show what’s possible with the plugin.
The example uses this CSV data, as found under 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 idea here is much the same as the tests.test_blog_example
test case.
Why is such a test case here? That’s simple: To make sure, the code samples in the documentation still work as designed.
- tests.test_complex_example.test_container_size_is_big_enough(bananas_shipped, banana_weight, apples_shipped, apple_weight, container_size)¶
This is just an example test case for the documentation.
- Parameters:
- Return type: