From 9c913fa040b8f6f059affb1ddd71c9bf6133028d Mon Sep 17 00:00:00 2001
From: Bartosz Podrygajlo <bartosz.podrygajlo@openairinterface.org>
Date: Mon, 20 May 2024 13:30:49 +0200
Subject: [PATCH] Introduce googletest library

The library will be either found in the system or downloaded via FetchContent.
---
 CMakeLists.txt   | 15 +++++++++++++++
 doc/UnitTests.md |  5 +++--
 2 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 03cbc144c7f..5226e05c663 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -2507,6 +2507,21 @@ if(ENABLE_TESTS)
   # meta-target: each test is supposed to add_dependencies(tests ${NEWTEST})
   # then, it is possible to build and execute all tests using "ninja tests && ctest"
   add_custom_target(tests)
+  find_package(GTest)
+  if (NOT GTest_FOUND)
+    message(STATUS "GTest package not found, will download googletest automatically. To prevent that install google test on your system (libgtest-dev)")
+    include(FetchContent)
+    FetchContent_Declare(
+      googletest
+      GIT_REPOSITORY https://github.com/google/googletest.git
+      GIT_TAG        58d77fa8070e8cec2dc1ed015d66b454c8d78850 # 1.12.1
+    )
+    set(BUILD_GMOCK OFF)
+    set(INSTALL_GTEST OFF)
+    FetchContent_MakeAvailable(googletest)
+    add_library(GTest::gtest ALIAS gtest)
+    add_library(GTest::gtest_main ALIAS gtest_main)
+  endif()
 endif()
 
 add_subdirectory(common)
diff --git a/doc/UnitTests.md b/doc/UnitTests.md
index 6ea18e22802..d1bbbed0ae2 100644
--- a/doc/UnitTests.md
+++ b/doc/UnitTests.md
@@ -8,8 +8,9 @@ explaining how to test with cmake and ctest; it is a suggested read, and the
 following just lists the main points of how to compile the tests and how to add
 new ones.
 
-At the time of writing, only the NR RLC tests have been integrated. The author
-hopes that more tests follow suit.
+GoogleTest is a C++ unit testing framework that has been added as an external dependency. While using GoogleTest is not a requirement it can simplify writing unit tests.
+See [primer](http://google.github.io/googletest/primer.html) for a quick introduction. To add it to your test executable link against
+`GTest::gtest` or `GTest::gtest_main`.
 
 # How to compile tests
 
-- 
GitLab