Back to Doctest

Prepare doctest for other targets to use

doc/markdown/build-systems.md

2.5.26.1 KB
Original Source

Build systems

The latest released version of doctest can be obtained from here: https://raw.githubusercontent.com/doctest/doctest/master/doctest/doctest.h

You can substitute master with dev or a tag like v1.4.8 for a specific version in the URL above.

CMake

  • doctest is easiest to use as a single file inside your own repository. Then the following minimal example will work:
cmake
cmake_minimum_required(VERSION 3.14)
project(cmake_test VERSION 0.0.1 LANGUAGES CXX)

# Prepare doctest for other targets to use
find_package(doctest REQUIRED)

# Make test executable
add_executable(tests main.cpp)
target_compile_features(tests PRIVATE cxx_std_17) # or later version
target_link_libraries(tests PRIVATE doctest::doctest)
  • You can also use the following CMake snippet to automatically fetch the header from the GitHub repository and use it in your project (set DOCTEST_VERSION to the desired tag, branch or even commit SHA):
cmake
set(DOCTEST_VERSION v2.4.11)
FetchContent_Declare(doctest
    URL  https://raw.githubusercontent.com/doctest/doctest/${DOCTEST_VERSION}/doctest/doctest.h
    DOWNLOAD_NO_EXTRACT TRUE
)
FetchContent_MakeAvailable(doctest)

set(DOCTEST_INCLUDE_DIR ${doctest_SOURCE_DIR})

...

# add it globally
include_directories(${DOCTEST_INCLUDE_DIR})

# or per target
target_include_directories(my_target PUBLIC ${DOCTEST_INCLUDE_DIR})

  • If you want CTest integration, you can fetch and use doctest as a CMake sub-project:
cmake
include(FetchContent)
FetchContent_Declare(
  doctest
  GIT_REPOSITORY https://github.com/doctest/doctest
  GIT_TAG v2.4.11 # tag, branch or even commit SHA
)
FetchContent_MakeAvailable(doctest)

# Include doctest_discover_tests(<target>) function
include("${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake")

# Use doctest in a target
target_link_libraries(my_target PUBLIC doctest::doctest)

# Or use it globally
link_libraries(doctest::doctest)

  • An older CMake integration approach is using ExternalProject_Add. It is not recommended for new code, but known to work and is kept here for legacy reasons:
cmake
include(ExternalProject)
find_package(Git REQUIRED)

ExternalProject_Add(
    doctest
    PREFIX ${CMAKE_BINARY_DIR}/doctest
    GIT_REPOSITORY https://github.com/doctest/doctest.git
    TIMEOUT 10
    UPDATE_COMMAND ${GIT_EXECUTABLE} pull
    CONFIGURE_COMMAND ""
    BUILD_COMMAND ""
    INSTALL_COMMAND ""
    LOG_DOWNLOAD ON
)

# Expose required variable (DOCTEST_INCLUDE_DIR) to parent scope
ExternalProject_Get_Property(doctest source_dir)
set(DOCTEST_INCLUDE_DIR ${source_dir}/doctest CACHE INTERNAL "Path to include folder for doctest")

And later you'll be able to use the doctest include directory like this:

cmake
# add it globally
include_directories(${DOCTEST_INCLUDE_DIR})

# or per target
target_include_directories(my_target PUBLIC ${DOCTEST_INCLUDE_DIR})
  • If you have the entire doctest repository available (as a submodule or just as files) you could also include it in your CMake build by using add_subdirectory(path/to/doctest) and then you could use it like this:
cmake
add_executable(my_tests src_1.cpp src_2.cpp ...)
target_link_libraries(my_tests doctest)

Package managers

doctest is available through the following package managers:

  • vcpkg

    • You can download and install doctest using the vcpkg dependency manager:
      sh
        git clone https://github.com/Microsoft/vcpkg.git
        cd vcpkg
        ./bootstrap-vcpkg.sh #.\bootstrap-vcpkg.bat(for windows)
        ./vcpkg integrate install
        ./vcpkg install doctest
      
      The doctest port in vcpkg is kept up to date by Microsoft team members and community contributors. If the version is out of date, please create an issue or pull request on the vcpkg repository.
  • hunter

  • conan

  • Homebrew (brew install doctest)

  • build2 and cppget.org

    • You can automatically download and use a specific version of doctest from the stable section of the cppget.org package repositiory (cppget.org/doctest) in each of your build2 projects.
    • For that, make sure to add the stable section of the cppget.org repository to your project's repositories.manifest file to be able to fetch the package list.
      :
      role: prerequisite
      location: https://pkg.cppget.org/1/stable
      #trust: ...
      
    • Add the dependency on doctest with your optional specific version constraint to your project's manifest file to make the package available for import.
      depends: doctest ^ 2.4.10
      
    • Import doctest and add it to the dependencies of your application in your buildfile. Please note, it is still a single header-only library and nothing will be linked. Only the path of the header file will be made available to the executable.
      import doctest = doctest%lib{doctest}
      exe{my-exe}: {hxx cxx}{**} $doctest
      
    • For further information, please refer to build2 | Documentation, cppget.org/doctest, or GitHub: build2-packaging/doctest for bug reports.

Home

<p align="center"></p>