Skip to main content

Provide CMake module for Pytest

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-cmake-0.0.1.tar.gz (6.4 kB view details)

Uploaded Source

File details

Details for the file pytest-cmake-0.0.1.tar.gz.

File metadata

  • Download URL: pytest-cmake-0.0.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for pytest-cmake-0.0.1.tar.gz
Algorithm Hash digest
SHA256 63fba6be92785c9a784ffe276a70c9b0e173a6d7978ee71dae62d04343f2a507
MD5 e4891218eb18653796ae740b23047b6a
BLAKE2b-256 aa4ce96de782f8338f4c1082311d35380288737327ef99da1a6472ea16c70d79

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page