googletest icon indicating copy to clipboard operation
googletest copied to clipboard

There are no test cases in the executable compiled by this test framework.

Open endingly opened this issue 3 years ago • 0 comments

Describe the bug

There are no test cases in the executable compiled by this test framework. Even if the main function that runs all the tests is included, it still does not work. Like as image And after my own troubleshooting, I found that the problem was in a class : simplelogger. If I don't use this class in the test source file, everything will work properly. Once I call it, the above problem will occur. Moreover, after my own test, if simpleLogger is called in the normal executable program, it will run normally.

Steps to reproduce the bug

The detailed code is as follows:

/// include/simpleLogger.h
#pragma once
#include <fmt/format.h>
#include <fstream>
#include <iostream>
#include <string>

using std::string;

enum LEVEL
{
    DEBUG,
    INFO,
    ERROR
};

class SimpleLogger
{
  private:
    std::fstream file;

    void writeLog(LEVEL level, string msg)
    {
        string levelStr;
        switch (level)
        {
        case DEBUG:
            levelStr = "Debug";
            break;
        case ERROR:
            levelStr = "Error";
            break;
        case INFO:
        default:
            levelStr = "Info";
        }

        auto s = fmt::format("[{}] {}\n", levelStr, msg);

        // string s = "[" + levelStr + "]" + "\r" + msg;

        std::cout << s << "\n";

        file.write(s.c_str(), s.length());
        file.flush();
    }

  public:
    SimpleLogger(string filename) : file(filename, std::ios::out) { }

    ~SimpleLogger() 
    {
        if (file.is_open())
        {
        file.close();
        }
    }

    void Debug(string msg)
    {
        writeLog(LEVEL::DEBUG, msg);
    }

    void Info(string msg)
    {
        writeLog(LEVEL::INFO, msg);
    }

    void Error(string msg)
    {
        writeLog(LEVEL::ERROR, msg);
    }
};
// simpleLogger_test.cpp
#include "SimpleLogger.h"
#include <fstream>
#include <gtest/gtest.h>
#include <string>

 TEST(Logger, writeByLogger)
 {
     SimpleLogger s("./test.txt");
     s.Debug("Hello world!");
 }

And CMakeLists file is

# ./CMakeLists.txt
cmake_minimum_required(VERSION 3.20.0)
set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
set(VCPKG_TARGET_TRIPLET "x64-mingw-dynamic")
project(plasma)

set(CMAKE_CXX_STANDARD 20)
############################################################################ 3rd package settings
include_directories(include)
aux_source_directory(src DIR_LIB_SRCS)
# find package
find_package(Eigen3 CONFIG REQUIRED)
find_package(GSL REQUIRED)
find_package(FFTW3 CONFIG REQUIRED)
find_package(fmt CONFIG REQUIRED)


############################################################################ mkl settings
find_package(MKL CONFIG REQUIRED)

# Eigen only support lp64
set(MKL_INTERFACE "lp64" CACHE STRING "data interface mode" FORCE)
########################################################################### LIB OUTPUT
add_library(${PROJECT_NAME} SHARED ${DIR_LIB_SRCS})
#add_executable(${PROJECT_NAME}_executable ./src/main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC GSL::gsl GSL::gslcblas Eigen3::Eigen FFTW3::fftw3 MKL::MKL fmt::fmt)
#target_link_libraries(${PROJECT_NAME}_executable PUBLIC ${PROJECT_NAME})
############################################################################ test_exe

add_subdirectory(tests)
# ./tests/CMakeLists.txt
cmake_minimum_required(VERSION 3.20.0)

project(plasma_test)

aux_source_directory(./ DIR_TEST_SRCS)

find_package(GTest CONFIG REQUIRED)
add_executable(plasma_test ${DIR_TEST_SRCS})
target_link_libraries(plasma_test GTest::gtest_main GTest::gtest plasma Eigen3::Eigen)

Does the bug persist in the most recent commit?

The version of GTest I use is 1.12.1

What operating system and version are you using?

Windows 11 Professional Edition 21H2 [22000.918]

What compiler and version are you using?

Clang version is

(built by Brecht Sanders) clang version 14.0.3
Target: x86_64-w64-windows-gnu
Thread model: posix
InstalledDir: C:/backup/mingw64/bin

What build system are you using?

Cmake version is 3.24.1

Additional context

Add any other context about the problem here.

endingly avatar Sep 01 '22 08:09 endingly