Skip to content

Commit 7edc764

Browse files
Benedikt Volkelbenedikt-voelkel
authored andcommitted
Add test
* add test * for basic analysis workflow * to check sanity of analysis output file * fetch tests data from GitLab (temporarily at https://gitlab.cern.ch/bvolkel/MCStepLoggerTestData/) * building the tests switched of by default, enable by passing MCStepLogger_BUILD_TESTS=ON to CMake
1 parent 25207d9 commit 7edc764

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ set(CMAKE_MODULE_PATH
2222
${CMAKE_MODULE_PATH}
2323
${CMAKE_SOURCE_DIR}/cmake)
2424

25+
option(MCStepLogger_BUILD_TESTS "Control whether tests are built" OFF)
26+
27+
include(CTest)
28+
2529
# Install directories
2630
SET(INSTALL_BIN_DIR ${CMAKE_INSTALL_PREFIX}/bin)
2731
SET(INSTALL_MACRO_DIR ${CMAKE_INSTALL_PREFIX}/macro)
@@ -224,3 +228,12 @@ configure_file(
224228
install(FILES
225229
"${PROJECT_BINARY_DIR}/MCStepLoggerConfig.cmake"
226230
DESTINATION ${INSTALL_CMAKE_DIR})
231+
232+
###############
233+
# Build tests #
234+
###############
235+
if(NOT MCStepLogger_BUILD_TESTS)
236+
message(WARNING "No tests are built.")
237+
else()
238+
add_subdirectory(test)
239+
endif(NOT MCStepLogger_BUILD_TESTS)

test/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Build tests
2+
3+
set(REPO_NAME_TEST MCStepLoggerTestData)
4+
5+
# Could potentially use CMake's FetchContent module but seems to be overhead since we only need this single line
6+
execute_process(COMMAND git clone https://gitlab.cern.ch/bvolkel/${REPO_NAME_TEST}.git WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} RESULT_VARIABLE res)
7+
8+
if(NOT res EQUAL "0")
9+
message(WARNING "Test data could not be fetched")
10+
return()
11+
endif(NOT res EQUAL "0")
12+
13+
# Find boost unit tests dependencies
14+
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
15+
include_directories(${Boost_INCLUDE_DIR})
16+
17+
# The basic test only checks core functionality without any backend.
18+
set(CORE_TARGET analysis)
19+
set(TEST_CORE "test${CORE_TARGET}")
20+
set(STEP_TEST_FILE MCStepLoggerOutput.root)
21+
add_executable(${CORE_TARGET} "${CORE_TARGET}.cxx")
22+
target_link_libraries(${CORE_TARGET} MCStepLoggerCore MCStepLoggerAnalysis ${Boost_LIBRARIES})
23+
add_test(NAME ${TEST_CORE} COMMAND ${CORE_TARGET} -- ${REPO_NAME_TEST}/${CORE_TARGET}/${STEP_TEST_FILE} "./")

test/analysis.cxx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Test analysis functionality
2+
3+
#define BOOST_TEST_MAIN
4+
#define BOOST_TEST_DYN_LINK
5+
#define BOOST_TEST_MODULE MCStepLoggerAnalysisCoreTest
6+
7+
#include <fstream>
8+
9+
#include <boost/test/unit_test.hpp>
10+
#include <boost/test/results_collector.hpp>
11+
12+
#include "MCStepLogger/MCAnalysisManager.h"
13+
#include "MCStepLogger/MCAnalysisFileWrapper.h"
14+
#include "MCStepLogger/SimpleStepAnalysis.h"
15+
#include "MCStepLogger/MCAnalysisFileWrapper.h"
16+
17+
using namespace o2::mcstepanalysis;
18+
19+
// checking meta info and whether there is a value container the analysis can be conpared with
20+
BOOST_AUTO_TEST_CASE(testAnalysis)
21+
{
22+
23+
// require input and output path
24+
BOOST_REQUIRE(boost::unit_test::framework::master_test_suite().argc == 3);
25+
26+
new SimpleStepAnalysis();
27+
auto& anamgr = MCAnalysisManager::Instance();
28+
anamgr.setLabel("testLabel");
29+
anamgr.setInputFilepath(boost::unit_test::framework::master_test_suite().argv[1]);
30+
31+
// everything loaded and ready to run?
32+
BOOST_REQUIRE(anamgr.checkReadiness());
33+
34+
// runs all events found and should produce an output file
35+
anamgr.run();
36+
37+
// write output
38+
std::string outputDir(boost::unit_test::framework::master_test_suite().argv[2]);
39+
std::string analysisFilepath = outputDir + "/SimpleStepAnalysis/Analysis.root";
40+
anamgr.write(outputDir);
41+
42+
// Now check if the analysis file was produced properly
43+
std::ifstream file(analysisFilepath);
44+
BOOST_REQUIRE(file);
45+
46+
MCAnalysisFileWrapper anaFile;
47+
anaFile.read(analysisFilepath);
48+
BOOST_REQUIRE(anaFile.isSane());
49+
50+
// one last check for meta information
51+
auto& anaMetaInfo = anaFile.getAnalysisMetaInfo();
52+
BOOST_REQUIRE(anaMetaInfo.analysisName.compare("SimpleStepAnalysis") == 0);
53+
}

0 commit comments

Comments
 (0)