Skip to main content

Pytest CMake

Test License CMake

Discover and run Python tests for Pytest with CMake. This module simplify Python automatic testing for C++ project with Python Bindings.

It works on Linux, macOS and Windows.

Usage

First, ensure that Pytest is installed on your platform by adding the following dependency in your project:

find_package(Pytest REQUIRED)

It is recommended to specify a minimum version for safety:

find_package(Pytest 4.6.11 REQUIRED)

Then add a CMake configuration file within your Python test directory with the following function:

pytest_discover_tests(
    PythonTest
    LIBRARY_PATH_PREPEND
        $<TARGET_FILE_DIR:example>
    PYTHON_PATH_PREPEND
        $<TARGET_FILE_DIR:example>
    TRIM_FROM_NAME "^test_"
    DEPENDS example
)

The first argument represent the name of the CMake target which will be created. It will also be used as a prefix for all tests displayed within CTest.

Expected environment can be defined with the LIBRARY_PATH_PREPEND and PYTHON_PATH_PREPEND arguments, which both accept multiple values. In our example, we use an expression generator to link to the target used for building the Python library.

Pytest usually requires tests to start with a specific prefix, which can be trimmed using the TRIM_FROM_NAME argument.

Targets dependency can be defined with the DEPENDS argument, which accept multiple values.

By default, running CTest for the project will display the tests as follows:

    Start 1: PythonTest.greet_world
1/4 Test #1: PythonTest.greet_world ...........   Passed    0.47 sec
    Start 2: PythonTest.greet_john
2/4 Test #2: PythonTest.greet_john ............   Passed    0.47 sec
    Start 3: PythonTest.greet_julia
3/4 Test #3: PythonTest.greet_julia ...........   Passed    0.47 sec
    Start 4: PythonTest.greet_michael
4/4 Test #4: PythonTest.greet_michael .........   Passed    0.54 sec

By setting the BUNDLE_TESTS argument to True, all tests will be grouped into one CMake test:

    Start 1: PythonTest
1/1 Test #1: PythonTest .......................   Passed    0.51 sec

This argument can also be set by the BUNDLE_PYTHON_TESTS environment variable. Bundling the tests will limit the visibility of all discovered test with CTest, but it will ensure that the tests can run faster in case you are using fixture with a wider scope.

Finally, ensure that FindPytest.cmake and PytestAddTests.cmake are available via CMAKE_MODULE_PATH and do not forget to install Pytest with the required version.

An example project is available for more details.

Release files for pytest-cmake 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-cmake 0.0.1
File Size Uploaded
pytest-cmake-0.0.1.tar.gz 6.4 kB Details

Release files / pytest-cmake-0.0.1.tar.gz

Download URL pytest-cmake-0.0.1.tar.gz
Size 6.4 kB
Tags Source
SHA-256 checksum
How to use checksums
63fba6be92785c9a784ffe276a70c9b0e173a6d7978ee71dae62d04343f2a507
BLAKE2b-256 checksum
How to use checksums
aa4ce96de782f8338f4c1082311d35380288737327ef99da1a6472ea16c70d79
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release history Release notifications | RSS feed

1.6.0

2 release files

1.5.0

2 release files

1.4.1

2 release files

1.3.0

2 release files

1.2.0

1 release file

1.1.0

1 release file

1.0.0

1 release file

0.13.0

1 release file

0.12.0

1 release file

0.11.4

1 release file

0.11.3

1 release file

0.11.2

1 release file

0.11.1

1 release file

0.11.0

1 release file

0.10.0

1 release file

0.9.0

1 release file

0.8.4

1 release file

0.8.3

1 release file

0.8.2

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.0

1 release file

0.6.0

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.1.0

1 release file

This release

0.0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page