diff --git a/.github/workflows/Pipeline.yml b/.github/workflows/Pipeline.yml index e11f37cb..eda7bf34 100644 --- a/.github/workflows/Pipeline.yml +++ b/.github/workflows/Pipeline.yml @@ -31,6 +31,31 @@ jobs: name: Java-Ant-JUnit4 path: examples/Java/JUnit/build/*.xml + Cpp-GoogleTest: + runs-on: ubuntu-latest + steps: + - name: ⏬ Checkout repository + uses: actions/checkout@v4 + - name: ⏬ Checkout 'google/googletest' repository + uses: actions/checkout@v4 + with: + repository: google/googletest + path: examples/Cpp/GoogleTest/lib/googletest + - name: ✅ Run the CMake 'googletest' target + run: | + cd examples/Cpp/GoogleTest + cmake -B build + cmake --build build + ./build/project1 + ./build/runUnitTests + - name: List generated XML reports + run: ls -lAh examples/Cpp/GoogleTest/build/*.xml + - name: 📤 Upload JUnit XML files as artifact + uses: actions/upload-artifact@v4 + with: + name: Cpp-GoogleTest + path: examples/Cpp/GoogleTest/build/*.xml + Python-pytest: runs-on: ubuntu-latest steps: @@ -106,6 +131,7 @@ jobs: needs: - Package - Java-Ant-JUnit4 + - Cpp-GoogleTest - Python-pytest runs-on: ubuntu-latest steps: diff --git a/examples/Cpp/GoogleTest/CMakeLists.txt b/examples/Cpp/GoogleTest/CMakeLists.txt new file mode 100644 index 00000000..6c6ee10c --- /dev/null +++ b/examples/Cpp/GoogleTest/CMakeLists.txt @@ -0,0 +1,32 @@ +cmake_minimum_required(VERSION 3.20) + +# Project configuration +set(PROJECT_NAME Unittesting) +project(${PROJECT_NAME}) +# * PROJECT_SOURCE_DIR - Top level source directory for the project +# * PROJECT_BINARY_DIR - Full path to build directory for project + +# Compiler flags +set(CMAKE_CXX_FLAGS "-g -Wall") +set(CMAKE_CXX_STANDARD 20) + +include_directories(${PROJECT_SOURCE_DIR}/src) +file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/src/*.cpp) + +# GoogleTest +add_subdirectory(./lib/googletest) +enable_testing() +# +# Include the gtest library. gtest_SOURCE_DIR is available due to +# 'project(gtest)' above. +include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) + +######################################## +# Test files +######################################## +file(GLOB TEST_SRC_FILES ${PROJECT_SOURCE_DIR}/test/*.cpp) + +######################################## +# Unit Tests +######################################## +add_executable(runUnitTests ${TEST_SRC_FILES}) diff --git a/examples/Cpp/GoogleTest/src/Counter.cpp b/examples/Cpp/GoogleTest/src/Counter.cpp new file mode 100644 index 00000000..930189d8 --- /dev/null +++ b/examples/Cpp/GoogleTest/src/Counter.cpp @@ -0,0 +1,13 @@ +#include "Counter.hpp" + +int Counter::Value() { + return _value; +} + +int Counter::Increment() { + return _value++; +} + +int Counter::Decrement() { + return _value--; +} diff --git a/examples/Cpp/GoogleTest/src/Counter.hpp b/examples/Cpp/GoogleTest/src/Counter.hpp new file mode 100644 index 00000000..177fb9f6 --- /dev/null +++ b/examples/Cpp/GoogleTest/src/Counter.hpp @@ -0,0 +1,16 @@ +#ifndef COUNTER_H_ +#define COUNTER_H_ + +class Counter { + private: + int _value; + + public: + Counter() : counter_(0) {} + + int Value(); + int Increment(); + int Decrement(); +}; + +#endif // COUNTER_H_ diff --git a/examples/Cpp/GoogleTest/test/Counter.test.cpp b/examples/Cpp/GoogleTest/test/Counter.test.cpp new file mode 100644 index 00000000..72970d5d --- /dev/null +++ b/examples/Cpp/GoogleTest/test/Counter.test.cpp @@ -0,0 +1,17 @@ +#include "../src/Counter.hpp" + +#include "gtest/gtest.h" + +namespace { +TEST(Counter, Increment) { + Counter c; + + EXPECT_EQ(0, c.Value()); + EXPECT_EQ(0, c.Increment()); + EXPECT_EQ(1, c.Value()); + + EXPECT_EQ(1, c.Decrement()); + EXPECT_EQ(0, c.Value()); +} + +} // namespace