From 6ed84c078f1291899fa341625e8de32840511b10 Mon Sep 17 00:00:00 2001 From: "Gernot J. Kraberger" <16017581+gkraberger@users.noreply.github.com> Date: Thu, 6 Sep 2018 15:09:30 +0200 Subject: [PATCH] Add option to measure python test coverage --- CMakeLists.txt | 19 +++++++++++++++++-- doc/install.rst | 4 ++++ test/CMakeLists.txt | 9 +++++++-- test/plovasp/CMakeLists.txt | 2 +- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cd1703d8..2b4457e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,9 +51,24 @@ add_subdirectory(shells) #------------------------ # tests #------------------------ - + +option(TEST_COVERAGE "Analyze the coverage of tests" OFF) + +# perform tests with coverage info +if (${TEST_COVERAGE}) + # we try to locate the coverage program + find_program(PYTHON_COVERAGE python-coverage) + find_program(PYTHON_COVERAGE coverage) + if(NOT PYTHON_COVERAGE) + message(FATAL_ERROR "Program coverage (or python-coverage) not found.\nEither set PYTHON_COVERAGE explicitly or disable TEST_COVERAGE!\nYou need to install the python package coverage, e.g. with\n pip install coverage\nor with\n apt install python-coverage") + endif() + + message(STATUS "Setting up test coverage") + add_custom_target(coverage ${PYTHON_COVERAGE} combine --append .coverage plovasp/.coverage || true COMMAND ${PYTHON_COVERAGE} html COMMAND echo "Open ${CMAKE_BINARY_DIR}/test/htmlcov/index.html in browser!" WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/test) +endif() + enable_testing() - + option(Build_Tests "Build the tests of the library " ON) if (Build_Tests) message(STATUS "-------- Preparing tests -------------") diff --git a/doc/install.rst b/doc/install.rst index 1b8c447e..72322a49 100644 --- a/doc/install.rst +++ b/doc/install.rst @@ -124,3 +124,7 @@ Functionality of ``dft_tools`` can be tweaked using extra compile-time options p +---------------------------------------------------------------+-----------------------------------------------+ | Build the documentation locally | -DBuild_Documentation=ON | +---------------------------------------------------------------+-----------------------------------------------+ +| Check test coverage when testing | -DTEST_COVERAGE=ON | +| (run ``make coverage`` to show the results; requires the | | +| python ``coverage`` package) | | ++---------------------------------------------------------------+-----------------------------------------------+ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index c5b33ea6..758ea139 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -7,8 +7,14 @@ FILE(COPY SrVO3.pmat SrVO3.struct SrVO3.outputs SrVO3.oubwin SrVO3.ctqmcout SrVO # List all tests set(all_tests wien2k_convert hk_convert w90_convert sumkdft_basic srvo3_Gloc srvo3_transp sigma_from_file blockstructure analyse_block_structure_from_gf analyse_block_structure_from_gf2) +set(python_executable python) + +if(${TEST_COVERAGE}) + set(python_executable ${PYTHON_COVERAGE} run --append --source "${CMAKE_BINARY_DIR}/python" ) +endif() + foreach(t ${all_tests}) - add_test(NAME ${t} COMMAND python ${CMAKE_CURRENT_SOURCE_DIR}/${t}.py) + add_test(NAME ${t} COMMAND ${python_executable} ${CMAKE_CURRENT_SOURCE_DIR}/${t}.py) endforeach() # Set the PythonPath : put the build dir first (in case there is an installed version). @@ -17,4 +23,3 @@ set_property(TEST ${all_tests} PROPERTY ENVIRONMENT PYTHONPATH=${CMAKE_BINARY_DI # VASP converter tests add_subdirectory(plovasp) - diff --git a/test/plovasp/CMakeLists.txt b/test/plovasp/CMakeLists.txt index 57e6877c..aa3f6a4e 100644 --- a/test/plovasp/CMakeLists.txt +++ b/test/plovasp/CMakeLists.txt @@ -12,7 +12,7 @@ FILE(COPY ${all_tests} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) FILE(COPY run_suite.py DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) foreach(t ${all_tests}) - add_test(NAME ${t} COMMAND python run_suite.py ${t}) + add_test(NAME ${t} COMMAND ${python_executable} run_suite.py ${t}) endforeach() set_property(TEST ${all_tests} PROPERTY ENVIRONMENT PYTHONPATH=${CMAKE_BINARY_DIR}/python:$ENV{PYTHONPATH} )