Skip to content

Commit

Permalink
Merge pull request #36 from edaa-org/C++-GoogleTest
Browse files Browse the repository at this point in the history
Added a C++/CMake/Ninja example project and unit tests for GoogleTest.
  • Loading branch information
skoehler authored May 16, 2024
2 parents fa66901 + 2aa79d4 commit 3dcc587
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/Pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,40 @@ 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: 🔧 Install dependencies
run: sudo apt-get install -y --no-install-recommends ninja-build
- name: 🛠 Run CMake
run: |
cd examples/Cpp/GoogleTest
cmake -B build -G Ninja
cmake --build build
- name: ✅ Run unit tests (directly)
run: |
cd examples/Cpp/GoogleTest
./build/unit_tests --gtest_output=xml:gtest.xml
- name: ✅ Run unit tests (by ctest)
run: |
cd examples/Cpp/GoogleTest
ctest --test-dir build/ --output-junit ../ctest.xml
- name: List generated XML reports
run: |
ls -lAh examples/Cpp/GoogleTest/*.xml
- name: 📤 Upload JUnit XML files as artifact
uses: actions/upload-artifact@v4
with:
name: Cpp-GoogleTest
path: examples/Cpp/GoogleTest/*.xml

Python-pytest:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -106,6 +140,7 @@ jobs:
needs:
- Package
- Java-Ant-JUnit4
- Cpp-GoogleTest
- Python-pytest
runs-on: ubuntu-latest
steps:
Expand Down
35 changes: 35 additions & 0 deletions examples/Cpp/GoogleTest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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)

file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/src/*.[ch]pp)

add_library(app STATIC ${SRC_FILES})
target_include_directories(app PUBLIC ${PROJECT_SOURCE_DIR}/src)

# GoogleTest
add_subdirectory(./lib/googletest)

########################################
# Test files
########################################
file(GLOB TEST_SRC_FILES ${PROJECT_SOURCE_DIR}/test/*.[ch]pp)

########################################
# Unit Tests
########################################
add_executable(unit_tests ${TEST_SRC_FILES})
target_link_libraries(unit_tests app gtest_main)


enable_testing()
include(GoogleTest)
gtest_discover_tests(unit_tests)
13 changes: 13 additions & 0 deletions examples/Cpp/GoogleTest/src/Counter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "Counter.hpp"

int Counter::Value() {
return _value;
}

int Counter::Increment() {
return _value++;
}

int Counter::Decrement() {
return _value--;
}
16 changes: 16 additions & 0 deletions examples/Cpp/GoogleTest/src/Counter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef COUNTER_H_
#define COUNTER_H_

class Counter {
private:
int _value;

public:
Counter(int value = 0) : _value { value } {}

int Value();
int Increment();
int Decrement();
};

#endif // COUNTER_H_
26 changes: 26 additions & 0 deletions examples/Cpp/GoogleTest/test/Counter.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "Counter.hpp"

#include "gtest/gtest.h"

namespace {

TEST(Counter, Init) {
Counter c0 { 0 };
Counter c1 { 1 };
EXPECT_EQ(0, c0.Value());
EXPECT_EQ(1, c1.Value());
}

TEST(Counter, Increment) {
Counter c { 0 };
EXPECT_EQ(0, c.Increment());
EXPECT_EQ(1, c.Value());
}

TEST(Counter, Decrement) {
Counter c { 1 };
EXPECT_EQ(1, c.Decrement());
EXPECT_EQ(0, c.Value());
}

} // namespace

0 comments on commit 3dcc587

Please sign in to comment.