{"id":10138,"library":"pytest-cmake","title":"Pytest CMake Integration","description":"pytest-cmake is a Pytest plugin that integrates Pytest into the CMake build system, allowing `ctest` to discover and run Python tests defined in `CMakeLists.txt`. It extends Pytest with options to scan CMake build directories and manage test execution within a CMake context. The current version is 1.3.0, and the project maintains an active release cadence, with minor versions released every 1-2 months.","status":"active","version":"1.3.0","language":"en","source_language":"en","source_url":"https://github.com/python-cmake/pytest-cmake","tags":["pytest","cmake","testing","plugin","ci/cd"],"install":[{"cmd":"pip install pytest-cmake","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"pytest-cmake is a plugin for pytest and requires it to function. It specifies `pytest >=7.0`.","package":"pytest","optional":false}],"imports":[],"quickstart":{"code":"import pytest\nimport os\nimport shutil\n\n# Create a dummy test file for pytest to run\ntest_file_content = \"\"\"\n# test_example.py\ndef test_success_with_plugin_active():\n    assert True\n\"\"\"\nwith open(\"test_example.py\", \"w\") as f:\n    f.write(test_file_content)\n\n# Create a dummy CMake build directory for pytest-cmake to scan\ncertified_cmake_build_dir = \"my_cmake_build_dir\"\nos.makedirs(certified_cmake_build_dir, exist_ok=True)\nwith open(os.path.join(certified_cmake_build_dir, \"CMakeLists.txt\"), \"w\") as f:\n    f.write(\"cmake_minimum_required(VERSION 3.15)\\nproject(DummyProject)\\n\")\n\nprint(f\"\\nRunning pytest with --py_cmake pointing to '{certified_cmake_build_dir}'...\")\nprint(\"This demonstrates the plugin is loaded and its options are recognized.\")\ntry:\n    # Invoke pytest programmatically. We ignore the build directory itself\n    # to prevent pytest from trying to run python files directly from there,\n    # and include our dummy test_example.py.\n    # The key is passing '--py_cmake' to show the plugin is active.\n    exit_code = pytest.main([\n        f\"--py_cmake={certified_cmake_build_dir}\",\n        \"--ignore\", certified_cmake_build_dir, # Prevent pytest scanning the build dir for tests\n        \"test_example.py\", # Run our simple test to ensure pytest executes\n        \"-v\"\n    ])\n\n    if exit_code == 0:\n        print(\"\\nSUCCESS: pytest ran successfully, and the `pytest-cmake` plugin's options were recognized.\")\n    else:\n        print(f\"\\nPytest finished with exit code {exit_code}. Check output above for details.\")\nexcept Exception as e:\n    print(f\"\\nAn unexpected error occurred during pytest execution: {e}\")\nfinally:\n    # Clean up created files/directories\n    os.remove(\"test_example.py\")\n    shutil.rmtree(certified_cmake_build_dir)\n    print(\"\\nCleaned up temporary files and directories.\")\n","lang":"python","description":"This quickstart demonstrates how to programmatically invoke Pytest using `pytest.main()` while activating the `pytest-cmake` plugin's `--py_cmake` option. It shows that the plugin is correctly installed and its command-line arguments are recognized by Pytest. For actual integration with CMake tests, your `CMakeLists.txt` would need to use `find_package(Pytest)` and `pytest_add_test()`."},"warnings":[{"fix":"The plugin is now loaded automatically by Pytest. Interact with its features via Pytest command-line options (e.g., `--py_cmake`) or through CMake functions like `pytest_add_test()` within your `CMakeLists.txt`.","message":"The programmatic API for `pytest-cmake` was removed in version 1.0.0. If you were previously interacting with `pytest-cmake` directly via Python imports (e.g., `import pytest_cmake`) or specific functions, this functionality is no longer available.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure the argument provided to `--py_cmake` points to a valid, existing CMake build directory (the directory containing `CMakeCache.txt` and other CMake build artifacts).","message":"The `--py_cmake` option requires a path to an existing CMake build directory. If the specified path does not exist or is not a directory, `pytest-cmake` will raise an error.","severity":"gotcha","affected_versions":"All"},{"fix":"Ensure `pytest-cmake` is installed in the Python environment that CMake is configured to use. CMake typically finds installed Python packages' modules automatically, but in complex environments, you might need to explicitly set `CMAKE_PREFIX_PATH` or `Python_FIND_PATH`.","message":"`pytest-cmake` relies on the CMake module `FindPytest.cmake` being discoverable by CMake within your build system. If CMake cannot find this module, your `find_package(Pytest)` call in `CMakeLists.txt` will fail.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Ensure the plugin is installed by running `pip install pytest-cmake` in the active Python environment. Verify `pytest --version` lists the plugin, or `pytest --help` shows `--py_cmake`.","cause":"The `pytest-cmake` plugin is not installed, or Pytest cannot discover it in your current environment.","error":"ERROR: unrecognized arguments: --py_cmake"},{"fix":"Provide a correct path to an existing CMake build directory. This directory should typically contain `CMakeCache.txt` and other build artifacts generated by CMake.","cause":"The path provided to the `--py_cmake` argument does not point to an existing directory.","error":"pytest_cmake.plugin.PytestCmakeError: CMake build directory '/path/to/nonexistent_build' does not exist."},{"fix":"Remove any `import pytest_cmake` statements. The plugin's functionality is exposed via Pytest command-line options (e.g., `--py_cmake`) or through CMake's `find_package(Pytest)` and `pytest_add_test()` functions.","cause":"You are attempting to directly import `pytest_cmake` in your Python code. `pytest-cmake` is a plugin designed for automatic discovery by Pytest, not for direct programmatic imports.","error":"ModuleNotFoundError: No module named 'pytest_cmake'"}]}