csspin_python.pytest

The pytest plugin is a thin wrapper around the pytest package for testing python-based projects. It makes use of csspin’s configuration tree and thus allows the usage of pre-defined settings while keeping the possibility to fully adjust all options and flags passed to pytest.

How to setup the pytest plugin?

The configuration of the pytest plugin is as easy as those of the python plugin.

Minimal configuration of spinfile.yaml to leverage pytest
plugin_packages:
    - csspin-python
plugins:
    - csspin_python.pytest
python:
    version: "3.11.9"

The provisioning of the required virtual environment as well as the plugins dependencies can be done via the well-known spin provision-task.

The plugin is now ready to use: spin pytest --help.

How to run tests against a CE16 instance using the pytest plugin?

Running tests against a CE16 instance requires the existence those. For the creation of an instance, the csspin_ce.mkinstance plugin can be used. To do so, we have to define it within the project’s spinfile.yaml (see documentation of csspin_ce.mkinstance).

Excerpt of spinfile.yaml for testing against a CE16 instance
plugin_packages:
    - csspin-ce
    - csspin-python
plugins:
    - csspin_ce.mkinstance
    - csspin_python.pytest
...

With a proper configuration and the mandatory provision, tests can be run by executing the pytest task. Here we explicitly define the DBMS backend to be “sqlite” while passing the path to the created instance as an argument to the pytest task.

Creating an instance and running tests against it
spin provision
spin -p mkinstance.dbms=sqlite mkinstance
spin pytest --instance sqlite

How to run the pytest plugin in order to collect coverage?

Running tests and collecting coverage using the pytest plugin can’t be easier, since the plugin’s default configuration already contains the necessary flags and options to do so. For this reason, one just have to pass the -c or --coverage flag, to run tests and generate coverage reports.

spin provision
spin pytest --coverage

How to debug tests?

Debugging tests executed by the pytest plugin using debugpy can be achieved by passing the --debug option when calling pytest. To diverge from the default configuration used to run debugpy, one can adjust the {debugpy.opts}.

Debugging tests using the pytest plugin
spin pytest --debug

As soon as the debugpy server is listening, one can attach to it using the desired IDE or debugger.

How to use playwright?

The pytest plugin can also run end to end tests by using pytest-playwright. Note, that the usage of playwright must be enabled explicitly. This can be done with the following spinfile.yaml

Minimal configuration of spinfile.yaml to leverage playwright
plugin_packages:
    - csspin-python
plugins:
    - csspin_python.pytest
python:
    version: "3.11.9"
pytest:
    playwright:
        enabled: True

Doing so will make sure that the required python packages to use pytest and playwright together will be installed during spin provision. Since the installed packages contain a pytest plugin, executing the tests for playwright is as easy as calling spin pytest.

pytest schema reference

pytest: 'object'

The pytest plugin provides the full pytest experience for spin.

pytest.coverage: 'bool'

Enable or disable code coverage analysis.

pytest.opts: 'list'

Additional options for the pytest command.

pytest.test_report: 'path'

Path to write the test report to.

pytest.coverage_opts: 'list'

Additional options for the coverage run.

pytest.coverage_report: 'str'

File to write the coverage report to.

pytest.tests: 'list'

List of test files or directories to include.

pytest.playwright: 'object'

Settings necessary to also run playwright tests with the pytest plugin.

pytest.playwright.enabled: 'bool'

Should the pytest-playwright plugin be installed and initialized.

pytest.playwright.browsers_path: 'path'

Path for playwright to install the browsers.

pytest.playwright.browsers: 'list'

The browsers to install and to use for running the playwright tests.

pytest.schema: 'object'
pytest.requires: 'object'
pytest.requires.spin: 'list'
pytest.requires.python: 'list'
pytest._requires: 'list'