Let's assume you have already installed CMake and the Google Test framework in your system.
I hope the following simple example will help to find an appropriate solution.
The example's files structure:
├── CMakeLists.txt
├── converter
├── converterTests
└── Src
├── Converter
│ ├── Headers
│ │ └── converter.hpp
│ └── Sources
│ └── converter.cpp
├── main.cpp
└── Tests
├── CMakeLists.txt
├── gTestsMain.cpp
├── Headers
└── Sources
└── converterTests.cpp
CMakeLists.txt
cmake_minimum_required( VERSION 3.11 )
project( converter VERSION 0.1.0 )
set( CMAKE_CXX_STANDARD 20 )
set( SOURCE_FILES
Src/main.cpp
Src/Converter/Sources/converter.cpp
)
include_directories(
Src/Converter/Headers/
)
add_executable( ${PROJECT_NAME} ${SOURCE_FILES} )
##########
# GTests
##########
add_subdirectory(Src/Tests)
Src/main.cpp
#include "converter.hpp"
#include <iostream>
#include <iomanip>
int main()
{
std::string start_date{"23102014"};
std::cout
<< std::endl
<< "Converting DDmmYYYY to YYmmDD"
<< std::endl
<< std::endl
<< "Result of converting "
<< std::quoted(start_date)
<< " : "
<< std::quoted(*date::DdMmYyyyDateToYyMmDd(start_date))
<< std::endl
<< std::endl;
}
Src/Converter/Headers/converter.hpp
#pragma once
#include <memory>
#include <string>
namespace date
{
std::shared_ptr<std::string> DdMmYyyyDateToYyMmDd(const std::string& dd_mm_yyyy_date);
}
Src/Converter/Sources/converter.cpp
#include "converter.hpp"
namespace date
{
std::shared_ptr<std::string> DdMmYyyyDateToYyMmDd( const std::string& dd_mm_yyyy_date )
{
// e.g. 20102014 to 141020 or DDmmYYYY to YYmmDD
if ( dd_mm_yyyy_date.size() < std::string("20102014").size() )
{
return nullptr;
}
std::string yy_mm_dd_date = dd_mm_yyyy_date.substr(0, 2);
yy_mm_dd_date.insert(0, dd_mm_yyyy_date.substr(2, 2));
yy_mm_dd_date.insert(0, dd_mm_yyyy_date.substr(6));
return std::make_shared<std::string>(yy_mm_dd_date);
}
}
Src/Tests/CMakeLists.txt
cmake_minimum_required( VERSION 3.11 )
set( TEST_SOURCE_FILES
gTestsMain.cpp
Sources/converterTests.cpp
../Converter/Sources/converter.cpp
../Converter/Headers/converter.hpp
)
add_executable(converterTests ${TEST_SOURCE_FILES})
target_link_libraries(converterTests gtest gmock pthread)
Src/Tests/gTestsMain.cpp
#include <gtest/gtest.h>
#include <gmock/gmock.h>
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
Src/Tests/Sources/converterTests.cpp
#include "converter.hpp"
#include <gtest/gtest.h>
TEST(ConverterTestSuite, empty_date)
{
ASSERT_EQ( date::DdMmYyyyDateToYyMmDd( std::string( "" ) ), nullptr );
}
TEST(ConverterTestSuite, date_convert)
{
std::string start_date{"30122045"};
ASSERT_EQ( *date::DdMmYyyyDateToYyMmDd( start_date ), std::string("451230") );
}
Execution of converterTests
will print:
[==========] Running 2 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 2 tests from ConverterTestSuite
[ RUN ] ConverterTestSuite.empty_date
[ OK ] ConverterTestSuite.empty_date (0 ms)
[ RUN ] ConverterTestSuite.date_convert
[ OK ] ConverterTestSuite.date_convert (0 ms)
[----------] 2 tests from ConverterTestSuite (0 ms total)
[----------] Global test environment tear-down
[==========] 2 tests from 1 test suite ran. (1 ms total)
[ PASSED ] 2 tests.
main
, it will call class/function of the lib normally. Then your Test application can also link to your lib and test its components. (For a total of 3 projects: MyLib, MyApp, MyTest). – Fecundate