{"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.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install pytest-cmake"],"cli":null},"imports":[],"auth":{"required":false,"env_vars":[]},"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()`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}