Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doctest_discover_tests and FetchContent_Declare #351

Closed
ChristofferGreen opened this issue Apr 1, 2020 · 6 comments
Closed

doctest_discover_tests and FetchContent_Declare #351

ChristofferGreen opened this issue Apr 1, 2020 · 6 comments

Comments

@ChristofferGreen
Copy link

Hi

Using FetchContent_Declare/FetchContent_MakeAvailable it's possible to download doctest and use it from cmake without having it pre-installed or packaged with your project.

It's not clear to me however how to use the doctest_discover_tests functionality https://github.com/onqtam/doctest/blob/master/scripts/cmake/doctest.cmake when using FetchContent_Declare/FetchContent_MakeAvailable. Does anyone know?

The closest I have gotten is to do:
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
doctest_discover_tests(ProjectName)

But -P "${_DOCTEST_DISCOVER_TESTS_SCRIPT}" does not get set correctly.

Thank you

@onqtam
Copy link
Member

onqtam commented May 11, 2020

Perhaps its similar to what one would do when using Catch2. However, I'm not familiar so I'd appreciate it if someone else helps out with this.

@ChristofferGreen
Copy link
Author

ChristofferGreen commented May 19, 2020

This eventually started working. Posting current solution here incase anyone else has the same issue. Not sure what the solution ended up being. I think I had some random issue that solved itself, sorry about that.

------------------------------------ First fetch/config doctest:
FetchContent_Declare(doctest GIT_REPOSITORY https://github.com/onqtam/doctest GIT_TAG 2.3.7)
FetchContent_MakeAvailable(doctest)
------------------------------------  Add libs/includes etc, just how I do it in my project, will be different for others
list(APPEND PMDepIncludesPublic ${doctest_SOURCE_DIR}/doctest)
list(APPEND PMDepLibraries      doctest)
set(PMDepLibraries      ${PMDepLibraries}      PARENT_SCOPE)
set(PMDepIncludesPublic ${PMDepIncludesPublic} PARENT_SCOPE)
------------------------------------  Main things
enable_testing()
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
doctest_discover_tests(Dygd)

@stephenlevitt
Copy link

@ChristofferGreen I have followed your approach and have managed to produce a valid CMake file which contains doctest_discover_tests - thanks! Unfortunately, now I am getting an error in that the text executable cannot be run:

[build] [ 58%] Linking CXX executable bin\Tests.exe
[build] CMake Error at _deps/doctest-src/scripts/cmake/doctestAddTests.cmake:45 (message):
[build]   Error running test executable
[build]   'C:/Data/...Project/build/bin/Tests.exe':
[build] 
[build] 
[build]     Result: Exit code 0xc0000139

If I remove doctest_discover_tests and just build the test exe target, the tests run without a problem. Do you have any idea how to fix this?

@ChristofferGreen
Copy link
Author

It sounds like there is some error in the c++ code, the error message says that main is returning 0xc0000139, anything above 0 is an error.

@stephenlevitt
Copy link

@ChristofferGreen Thanks for your comment. I'm confused because no error is produced when I run my test executable. The test file cannot be run only when I attempt to use doctest_discover_tests.

Here is minimal example of a CMakeLists.txt file illustrating the problem (see the last line). I have used the factorial function from the doctest homepage. I am running on Windows 10, using VS Code:

message("Processing CMakeLists.txt...")

cmake_minimum_required(VERSION 3.27.1)
project(example-project)

# ====================== Setup Targets ======================

# Test executable
add_executable(factorial-tests ${CMAKE_SOURCE_DIR}/factorial.cpp)
target_link_libraries(factorial-tests PRIVATE doctest::doctest) # link publicly to make doctest headers available to the target

# ====================== Declare and Download Dependencies ======================

include(FetchContent)

# doctest
FetchContent_Declare(
    doctest
    GIT_REPOSITORY https://github.com/doctest/doctest.git
    # corresponds to doctest v2.4.11 - good practice to specify the commit hash as tags can be moved
    GIT_TAG "ae7a13539fb71f270b87eb2e874fbac80bc8dda2" )

# make the dependencies available to the build system
FetchContent_MakeAvailable(doctest)

# ====================== CTest ======================

enable_testing()
include(${doctest_SOURCE_DIR}/scripts/cmake/doctest.cmake)
# doctest_discover_tests(factorial-tests) # uncommenting this line causes the error

@stephenlevitt
Copy link

Here is factorial.cpp (in the same directory at CMakeLists.txt):

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"

int factorial(int number) { return number <= 1 ? number : factorial(number - 1) * number; }

TEST_CASE("testing the factorial function")
{
    CHECK(factorial(0) == 1);
    CHECK(factorial(1) == 1);
    CHECK(factorial(2) == 2);
    CHECK(factorial(3) == 6);
    CHECK(factorial(10) == 3628800);
}

Output with the last line commented:

$ "C:/Data/.../factorial-project/build/factorial-tests.exe"
[doctest] doctest version is "2.4.11"
[doctest] run with "--help" for options
===============================================================================
C:\Data\...\factorial-project\factorial.cpp:6:
TEST CASE:  testing the factorial function

C:\Data\...\factorial-project\factorial.cpp:8: ERROR: CHECK( factorial(0) == 1 ) is NOT correct!     
  values: CHECK( 0 == 1 )

===============================================================================
[doctest] test cases: 1 | 0 passed | 1 failed | 0 skipped
[doctest] assertions: 5 | 4 passed | 1 failed |
[doctest] Status: FAILURE!

Output with the last line uncommented:

[main] Building folder: factorial-project all
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Data/.../factorial-project/build --config Debug --target all -j 14 --
[build] [ 50%] Linking CXX executable factorial-tests.exe
[build] CMake Error at _deps/doctest-src/scripts/cmake/doctestAddTests.cmake:45 (message):
[build]   Error running test executable
[build]   'C:/Data/.../factorial-project/build/factorial-tests.exe':
[build] 
[build] 
[build]     Result: Exit code 0xc0000139
[build] 
[build]   
[build] 
[build]     Output: 
[build] 
[build] 
[build] 
[build] mingw32-make[2]: *** [CMakeFiles\factorial-tests.dir\build.make:100: factorial-tests.exe] Error 1
[build] mingw32-make[2]: *** Deleting file 'factorial-tests.exe'
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:99: CMakeFiles/factorial-tests.dir/all] Error 2
[build] mingw32-make: *** [Makefile:145: all] Error 2
[proc] The command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Data/.../factorial-project/build --config Debug --target all -j 14 -- exited with code: 2
[driver] Build completed: 00:00:01.281
[build] Build finished with exit code 2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants