Skip to content

Commit

Permalink
Added CMake file.
Browse files Browse the repository at this point in the history
Added an C++ example project and unit tests for GoogleTest (gtest).
  • Loading branch information
Paebbels committed May 15, 2024
1 parent fa66901 commit d36cd07
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/Pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -106,6 +131,7 @@ jobs:
needs:
- Package
- Java-Ant-JUnit4
- Cpp-GoogleTest
- Python-pytest
runs-on: ubuntu-latest
steps:
Expand Down
32 changes: 32 additions & 0 deletions examples/Cpp/GoogleTest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
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() : counter_(0) {}

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

#endif // COUNTER_H_
17 changes: 17 additions & 0 deletions examples/Cpp/GoogleTest/test/Counter.test.cpp
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d36cd07

Please sign in to comment.