Skip to content

Commit

Permalink
Add pytest tests to cmake
Browse files Browse the repository at this point in the history
Tests can be executed similar to test binaries from build folder by
calling wrapper shellscript `run_pytest.sh`. This wrapper forwards all
arguments to pytest and exports the requried environment variables to
locations as found during the cmake configure process.
  • Loading branch information
Ozaq committed Oct 10, 2024
1 parent 8f5d1f1 commit 06045d6
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
50 changes: 48 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if (HAVE_GRIBJUMP_LOCAL_EXTRACT)
set(GRIBJUMP_HAVE_FDB 1)

ecbuild_find_package( NAME eccodes VERSION 2.32.1 REQUIRED )

### AEC
# Override eccodes' aec with our own: we need a newer version.
unset(AEC_INCLUDE_DIRS CACHE)
Expand All @@ -46,14 +46,18 @@ if (HAVE_GRIBJUMP_LOCAL_EXTRACT)

endif()

ecbuild_add_option( FEATURE PYTHON_API_TESTS
DEFAULT ON
DESCRIPTION "Will execute python tests against pygribjump / libgribjump")

########################################################################################################################
# contents

include(cmake/compiler_warnings.cmake) # optionally handle compiler specific warnings

set( gribjump_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_BINARY_DIR}/src )

include_directories(
include_directories(
${AEC_INCLUDE_DIRS}
${gribjump_INCLUDE_DIRS}
${eckit_INCLUDE_DIRS}
Expand All @@ -70,6 +74,48 @@ if (HAVE_GRIBJUMP_LOCAL_EXTRACT)
add_subdirectory( tests )
endif()

if(HAVE_PYTHON_API_TESTS)
ecbuild_find_package( NAME Python
VERSION 3.10
COMPONENTS Interpreter
REQUIRED )

set(FDB5_DIR "${fdb5_DIR}/../../..")
configure_file(
${CMAKE_SOURCE_DIR}/cmake/run_pytest.sh.in
${CMAKE_BINARY_DIR}/run_pytest.sh
@ONLY
)

function(find_python_library python_lib)
execute_process(
COMMAND
${Python_EXECUTABLE} "-c" "import ${python_lib}; print(${python_lib}.__version__)"
RESULT_VARIABLE status
OUTPUT_VARIABLE python_lib_version
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT status)
message("Found Python module ${python_lib} ${python_lib_version}")
else()
message(FATAL_ERROR "Did not find package ${python_lib}")
endif()
endfunction()
find_python_library(cffi)
find_python_library(numpy)
find_python_library(pytest)
find_python_library(setuptools)
find_python_library(findlibs)

add_test(
NAME run_pytests
COMMAND ${CMAKE_BINARY_DIR}/run_pytest.sh -vv -s
--basetemp=${CMAKE_BINARY_DIR}/pytest-out
)

endif()

########################################################################################################################
# PLUGIN
# Write and install the manifest file for the plugin
Expand Down
8 changes: 8 additions & 0 deletions cmake/run_pytest.sh.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /usr/bin/env bash

export GRIBJUMP_HOME="@CMAKE_BINARY_DIR@"
export FDB5_DIR="@FDB5_DIR@"
export PYTHONPATH="${PYTHONPATH}:@CMAKE_SOURCE_DIR@/pygribjump/src"

@Python_EXECUTABLE@ -m pytest @CMAKE_SOURCE_DIR@ --basetemp="@CMAKE_BINARY_DIR@/test-results" "$@"

0 comments on commit 06045d6

Please sign in to comment.