How to run tests?


I’d like to run xgboost’s tests. I cloned the Github repo and checked out the release_0.81 branch. After following the instructions here, I was able to build xgboost from source using make -j4.

Is there a way to just run the tests? I tried doing make test but ran into a “fatal error”.

I am on Ubuntu 16.04.


FYI, when I run make test, the error I get is:
g++ -DDMLC_LOG_CUSTOMIZE=1 -std=c++11 -Wall -Wno-unknown-pragmas -Iinclude -Idmlc-core/include -Irabit/include -I/include -O3 -funroll-loops -msse2 -fPIC -fopenmp -I/include/ -o build_tests/cpp/helpers.o -c tests/cpp/ In file included from tests/cpp/ tests/cpp/./helpers.h:15:25: fatal error: gtest/gtest.h: No such file or directory compilation terminated. tests/cpp/ recipe for target 'build_tests/cpp/helpers.o' failed make: *** [build_tests/cpp/helpers.o] Error 1


In all honesty I couldn’t get CMake to find the GoogleTest libraries even though I did the local install, and installed in Ubuntu, or using Google’s own instructions. Hopefully someone has better instructions, it’s been on the roadmap to document this.

What I did is install gtest locally on Ubuntu: sudo apt install libgtest-dev. You can also follow this guide.

Then remove the GTest requirement in CMakeLists.txt because it can’t seem to find gtest regardless:

# Test
  # Disable this because I couldn't get it to find the libraries
#  find_package(GTest REQUIRED)


    file(GLOB_RECURSE CUDA_TEST_SOURCES "tests/cpp/*.cu")
    set(CUDA_TEST_OBJS "")

  add_executable(testxgboost ${TEST_SOURCES} ${CUDA_TEST_OBJS} $<TARGET_OBJECTS:objxgboost>)
  set_output_directory(testxgboost ${PROJECT_SOURCE_DIR})
    # Add gtest manually, the variable is no longer available
  target_link_libraries(testxgboost gtest ${LINK_LIBRARIES})

  add_test(TestXGBoost testxgboost)

Then you can run make testxgboost in your cmake build directory and run the tests using ../testxgboost. That is, after you’ve installed Gtest and made the changes to CMakeLists.txt:

mkdir build
cd build
cmake  -DGOOGLE_TEST=ON ..
make -j4 testxgboost


Hi Theodore. Thank you for your detailed reply! I will try your above instructions and let you know if it works.


That’s odd to me. I deprecated the customized gtest CMake script and Linux test farms are running gtest. By the way, location of GTest can be specified by CMake flag -DGTEST_ROOT=/path/to/gtest.


@jiaming could you give directions on how to run the tests on a fresh clone without a local installation of Gtest then?

I’ve tried running cmake -DGTEST_ROOT=googletest-src -DGOOGLE_TEST=ON .. but it still doesn’t find Gtest.


The GTEST_ROOT specifies the installation path of GoogleTests. It’s a parameter from CMake’s own FindGtest script:

That being said, you have to install googletest somewhere first. We made an ugly hack to run googletest on Travis due to Ubuntu is not distributing it:

But I think the same thing can also be achieved by specifying -DCMAKE_INSTALL_PREFIXwhen compiling googletest. Hope that helps.