Skip to content

Commit

Permalink
Merge pull request #4 from DavidingPlus/feature-liuzx-unit-test
Browse files Browse the repository at this point in the history
Use Conan To Add GoogleTest For Unit Test
  • Loading branch information
DavidingPlus authored Dec 16, 2024
2 parents ddddd6e + 89025f7 commit 93232ef
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 90 deletions.
21 changes: 16 additions & 5 deletions .github/workflows/build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Install Conan
run: |
pip install conan==2.6.0
conan profile detect
- name: Configure CMake
working-directory: ${{github.workspace}}
run: |
mkdir -p build && cd build
conan install . --build=missing
cmake ..
cmake --preset cmake-project-release -DWITH_GTEST=ON
- name: Build
working-directory: ${{github.workspace}}/build
run: make
- name: Build Targets
working-directory: ${{github.workspace}}
run: cmake --build --preset cmake-project-release

- name: Run Unit Test
working-directory: ${{github.workspace}}
run: cmake --build --preset cmake-project-release -t tests
24 changes: 16 additions & 8 deletions .github/workflows/build-win32.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,32 @@ on:
branches:
- master

env:
BUILD_TYPE: Release

jobs:
build:
runs-on: windows-latest

steps:
- uses: actions/checkout@v4

- name: Install Conan
run: |
pip install conan==2.6.0
conan profile detect
- name: Configure CMake
working-directory: ${{github.workspace}}
run: |
mkdir -p build && cd build
conan install . --build=missing
cmake --preset cmake-project-default -DWITH_GTEST=ON
cmake -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} ..
- name: Build Targets
working-directory: ${{github.workspace}}
run: cmake --build --preset cmake-project-release

- name: Build
working-directory: ${{github.workspace}}/build
run: cmake --build ./ --config ${{env.BUILD_TYPE}}
- name: Run Unit Test
working-directory: ${{github.workspace}}
run: cmake --build --preset cmake-project-release -t tests
44 changes: 0 additions & 44 deletions .github/workflows/unit-test-linux.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/
build/
CMakeUserPresets.json
21 changes: 19 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required (VERSION 3.12)
cmake_minimum_required (VERSION 3.23)

set (CMAKE_CXX_STANDARD 11)
set (CMAKE_CXX_STANDARD_REQUIRED true)
Expand Down Expand Up @@ -32,7 +32,7 @@ set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDI

# configure targets
aux_source_directory (${DIR_SRC_ROOT} PROJECT_SOURCE_FILES)
add_library (cmakeproject ${PROJECT_SOURCE_FILES})
add_library (cmake-project ${PROJECT_SOURCE_FILES})

unset (CMAKE_ARCHIVE_OUTPUT_DIRECTORY)
unset (CMAKE_LIBRARY_OUTPUT_DIRECTORY)
Expand All @@ -41,3 +41,20 @@ unset (CMAKE_RUNTIME_OUTPUT_DIRECTORY)

# configure snippet
add_subdirectory ("snippet")


# configure unit test
option (WITH_GTEST "Enable unit tests by GoogleTest" OFF)

if (WITH_GTEST)
message ("-- Unit test by GoogleTest is enabled")

else ()
message ("-- Unit test by GoogleTest is disabled")

endif ()

if (WITH_GTEST)
add_subdirectory (test)

endif ()
24 changes: 24 additions & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from conan import ConanFile
from conan.tools.cmake import cmake_layout, CMakeToolchain


class ExampleRecipe(ConanFile):
name = "cmake-project"
description = "C/C++ 项目的 CMake 模板。"
languages = "C++"
author = "DavidingPlus"
homepage = "https://github.com/DavidingPlus/cmake-project-template"

settings = "os", "compiler", "build_type", "arch"
generators = "CMakeDeps"

def requirements(self):
self.requires("gtest/1.12.1")

def layout(self):
cmake_layout(self)

def generate(self):
tc = CMakeToolchain(self)
tc.presets_prefix = "cmake-project"
tc.generate()
17 changes: 0 additions & 17 deletions config-unix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,3 @@ message ("-- Using C++ Compiler: ${CMAKE_CXX_COMPILER} (${CMAKE_CXX_COMPILER_VER


option (BUILD_SHARED_LIBS "Build using shared libraries" ON)


# configure unit test
option (WITH_GTEST "Enable unit tests by GoogleTest" OFF)

if (WITH_GTEST)
message ("-- Unit test by GoogleTest is enabled")

else ()
message ("-- Unit test by GoogleTest is disabled")

endif ()

if (WITH_GTEST)
add_subdirectory (test)

endif ()
2 changes: 1 addition & 1 deletion snippet/TestClassTest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
add_executable (TestClassTest main.cpp)
target_link_libraries (TestClassTest cmakeproject)
target_link_libraries (TestClassTest cmake-project)
16 changes: 3 additions & 13 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
message ("-- Checking if GoogleTest is installed ...")

find_package (GTest REQUIRED)
if (NOT GTest_FOUND)
message (WARNING "-- GoogleTest not installed")
return ()

endif ()

include (GoogleTest)


aux_source_directory (${CMAKE_CURRENT_SOURCE_DIR} SRC_GTEST)
add_executable (gtest_testrun EXCLUDE_FROM_ALL ${SRC_GTEST})
target_link_libraries (gtest_testrun gtest gtest_main cmakeproject)
add_executable (gtest-testrun EXCLUDE_FROM_ALL ${SRC_GTEST})
target_link_libraries (gtest-testrun gtest::gtest cmake-project)

add_custom_target (tests)
add_dependencies (tests gtest_testrun)
add_dependencies (tests gtest-testrun)


message ("-- Done configuring gtest")

0 comments on commit 93232ef

Please sign in to comment.