1

First, the compilation environment is:

  • windows 64
  • Visual Studio Community 2022 Release - AMD64
  • CMake
  • vcpkg

Well, let's take a look at the project directory structure first.

.
│  .gitignore
│  CMakeLists.txt
│  LICENSE
│  README.md
│
├─.vscode
│      launch.json
│      settings.json
│
├─include
│      BDictionary.h
│      bencoding.h
│      BInteger.h
│      BItem.h
│      BItemVisitor.h
│      BList.h
│      BString.h
│      CMakeLists.txt
│      Decoder.h
│      Encoder.h
│      PrettyPrinter.h
│      Utils.h
│
├─src
│      BDictionary.cpp
│      BInteger.cpp
│      BItem.cpp
│      BItemVisitor.cpp
│      BList.cpp
│      BString.cpp
│      CMakeLists.txt
│      Decoder.cpp
│      Encoder.cpp
│      PrettyPrinter.cpp
│      Utils.cpp
│
└─tests
        BDictionaryTests.cpp
        BIntegerTests.cpp
        BListTests.cpp
        BStringTests.cpp
        CMakeLists.txt
        DecoderTests.cpp
        EncoderTests.cpp
        main.cpp
        PrettyPrinterTests.cpp
        TestUtils.cpp
        TestUtils.h
        UtilsTests.cpp

This project is a parsing library about Bencode on Github, the address is here: https://github.com/s3rvac/cpp-bencoding.git

As you can see, this project is controlled by CMake, and there are four CMakeLists under the directory. Among them, the contents of ./src/CMakeLists.txt and ./include/CMakeLists.txt are not important.

Let's mainly look at the contents of ./CMakeLists.txt and ./tests/CMakeLists.txt.

# ./CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
SET(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
project(cpp-bencoding CXX)
include_directories(include)
add_subdirectory(include)
add_subdirectory(src)
add_subdirectory(tests)
enable_testing()
# ./tests/CMakeLists.txt
find_package(GTest CONFIG REQUIRED)

set(TESTER_SOURCES
    BDictionaryTests.cpp
    BIntegerTests.cpp
    BListTests.cpp
    BStringTests.cpp
    DecoderTests.cpp
    EncoderTests.cpp
    PrettyPrinterTests.cpp
    TestUtils.cpp
    UtilsTests.cpp
    main.cpp
        )

add_executable(tester ${TESTER_SOURCES})

target_link_libraries(tester PRIVATE bencoding GTest::gmock GTest::gtest GTest::gmock_main GTest::gtest_main)

include(CTest)
include(GoogleTest)

gtest_discover_tests(tester)

Well, the background is basically explained clearly. As it is now, the compilation passes and the test program runs successfully. The problem is that my CTest doesn't discover the tests, it tells me that no tests were found, Is gtest_discover_tests() causing the problem? Also, can I run a single test function by mouse click in visual studio code like in visual studio?

endingly
  • 41
  • 1
  • And you build it with `-DWITH_TESTS=1`? Anyway, you provided too much info with really little relevant info. Like how you build it, what cmake version etc. – ixSci Feb 06 '22 at 14:38
  • How do you invoke `ctest`? From which directory and with which arguments? – JohnFilleau Feb 06 '22 at 14:40
  • I'm assuming there's a common build directory that `ctest` is invoke from. If so, then you need to perform `include(CTest)` at your top-level project. Otherwise you need to tell ctest which directory structure to search for your CTest files. – JohnFilleau Feb 06 '22 at 14:43
  • cmake version is 3.22 @ixSci – endingly Feb 06 '22 at 16:28
  • I also don't know how I invoke ctest. Because as given in the CMakeLists above, I just include ctest and click the button of Run CTest @JohnFilleau – endingly Feb 06 '22 at 16:28
  • *Otherwise you need to tell ctest which directory structure to search for your CTest files.* So how can I specify the path to this CTest file? By `set()`? @JohnFilleau – endingly Feb 06 '22 at 16:30
  • You need to move the call `enable_testing()` for it be **before** `add_subdirectory(tests)`, where you add tests. – Tsyvarev Feb 06 '22 at 16:40
  • @endingly you need to tell it what directory is your top-level test directory when you invoke. Can you give more information about how you invoke ctest? I've never run it through VS – JohnFilleau Feb 06 '22 at 16:52
  • Of course, again -- the canonical approach is to `include(CTest)` at the top of your project structure. – JohnFilleau Feb 06 '22 at 16:53

0 Answers0