diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000000..432fc5dff0 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,8 @@ +[run] +parallel = True +source = + tests + +[paths] +source = + tests/ diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3e67c8f924..645ec84d51 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -125,6 +125,8 @@ jobs: -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON -DDOWNLOAD_EIGEN=ON + -DCMAKE_BUILD_TYPE=Debug + -DPYBIND11_TEST_COVERAGE=ON -DCMAKE_CXX_STANDARD=17 ${{ matrix.args }} ${{ matrix.args2 }} @@ -135,6 +137,9 @@ jobs: - name: Python tests run: cmake --build build2 --target pytest + - name: Upload to Codecov + run: codecov --token "${{ secrets.CODECOV_TOKEN }}" --name "${{ matrix.python }}-${{ matrix.runs-on }}-${{ matrix.args }}" + - name: C++ tests # TODO: Figure out how to load the DLL on Python 3.8+ if: "!(runner.os == 'Windows' && (matrix.python == 3.8 || matrix.python == 3.9 || matrix.python == '3.10-dev'))" @@ -273,7 +278,7 @@ jobs: - uses: actions/checkout@v2 - name: Add wget and python3 - run: apt-get update && apt-get install -y python3-dev python3-numpy python3-pytest libeigen3-dev + run: apt-get update && apt-get install -y python3-dev python3-numpy python3-pytest libeigen3-dev python3-pytest-cov - name: Configure shell: bash @@ -308,7 +313,7 @@ jobs: # tzdata will try to ask for the timezone, so set the DEBIAN_FRONTEND - name: Install 🐍 3 - run: apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install -y cmake git python3-dev python3-pytest python3-numpy + run: apt-get update && DEBIAN_FRONTEND="noninteractive" apt-get install -y cmake git python3-dev python3-pytest python3-numpy python3-pytest-cov - name: Configure run: cmake -S . -B build -DPYBIND11_CUDA_TESTS=ON -DPYBIND11_WERROR=ON -DDOWNLOAD_CATCH=ON @@ -331,7 +336,7 @@ jobs: # - uses: actions/checkout@v2 # # - name: Add Python 3 and a few requirements -# run: yum update -y && yum install -y git python3-devel python3-numpy python3-pytest make environment-modules +# run: yum update -y && yum install -y git python3-devel python3-numpy python3-pytest make environment-modules python3-pytest-cov # # - name: Install CMake with pip # run: | @@ -435,7 +440,7 @@ jobs: - uses: actions/checkout@v1 - name: Add Python 3 - run: apt-get update; apt-get install -y python3-dev python3-numpy python3-pytest python3-pip libeigen3-dev + run: apt-get update; apt-get install -y python3-dev python3-numpy python3-pytest python3-pip libeigen3-dev python3-pytest-cov - name: Update pip run: python3 -m pip install --upgrade pip diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 3bfd5f1cae..a527fa28d0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -16,6 +16,9 @@ else() cmake_policy(VERSION 3.18) endif() +if(MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") +endif() # Only needed for CMake < 3.5 support include(CMakeParseArguments) @@ -308,6 +311,10 @@ if(PYBIND11_CUDA_TESTS) set_property(SOURCE ${PYBIND11_TEST_FILES} PROPERTY LANGUAGE CUDA) endif() +set(PYBIND11_TEST_COVERAGE + OFF + CACHE BOOL "Collect test coverage for C++ and Python") + foreach(target ${test_targets}) set(test_files ${PYBIND11_TEST_FILES}) if(NOT "${target}" STREQUAL "pybind11_tests") @@ -349,6 +356,11 @@ foreach(target ${test_targets}) target_compile_definitions(${target} PRIVATE -DPYBIND11_TEST_BOOST) endif() + if(PYBIND11_TEST_COVERAGE AND NOT MSVC) + target_compile_options(${target} PRIVATE -coverage) + target_link_libraries(${target} PRIVATE -coverage) + endif() + # Always write the output file directly into the 'tests' directory (even on MSVC) if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY) set_target_properties(${target} PROPERTIES LIBRARY_OUTPUT_DIRECTORY @@ -385,11 +397,17 @@ set(PYBIND11_TEST_PREFIX_COMMAND "" CACHE STRING "Put this before pytest, use for checkers and such") +if(PYBIND11_TEST_COVERAGE) + set(PYBIND11_PYTEST_EXTRA_FLAGS "--cov=pybind11") +else() + set(PYBIND11_PYTEST_EXTRA_FLAGS "") +endif() + # A single command to compile and run the tests add_custom_target( pytest COMMAND ${PYBIND11_TEST_PREFIX_COMMAND} ${PYTHON_EXECUTABLE} -m pytest - ${PYBIND11_ABS_PYTEST_FILES} + ${PYBIND11_PYTEST_EXTRA_FLAGS} ${PYBIND11_ABS_PYTEST_FILES} DEPENDS ${test_targets} WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}" USES_TERMINAL) diff --git a/tests/requirements.txt b/tests/requirements.txt index 0b383bf5c3..a0fb0e4bbb 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,9 +1,12 @@ --extra-index-url https://antocuni.github.io/pypy-wheels/manylinux2010/ +codecov>=2.0.5 +coverage>=4.2 numpy==1.16.6; python_version<"3.6" and sys_platform!="win32" numpy==1.18.0; platform_python_implementation=="PyPy" and sys_platform=="darwin" and python_version>="3.6" numpy==1.19.3; (platform_python_implementation!="PyPy" or sys_platform=="linux") and python_version>="3.6" and python_version<"3.10" pytest==4.6.9; python_version<"3.5" pytest==6.1.2; python_version=="3.5" pytest==6.2.1; python_version>="3.6" +pytest-cov scipy==1.2.3; (platform_python_implementation!="PyPy" or sys_platform=="linux") and python_version<"3.6" scipy==1.5.4; (platform_python_implementation!="PyPy" or sys_platform=="linux") and python_version>="3.6" and python_version<"3.10"