]> git.proxmox.com Git - mirror_edk2.git/commitdiff
UnitTestFrameworkPkg: Add googletest submodule and GoogleTestLib
authorMichael D Kinney <michael.d.kinney@intel.com>
Tue, 21 Jun 2022 02:52:03 +0000 (19:52 -0700)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 11 Nov 2022 05:46:51 +0000 (05:46 +0000)
REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4134

Add submodule for googletest and add GoogleTestLib that is
required for GoogleTest based unit tests. Add GoogleTest
documentation to Readme.md along with a port of the sample
unit test to the GoogleTest style.

A few typos in Readme.md are also fixed.

Cc: Michael Kubacki <mikuback@linux.microsoft.com>
Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Andrew Fish <afish@apple.com>
Cc: Leif Lindholm <quic_llindhol@quicinc.com>
Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Michael Kubacki <mikuback@linux.microsoft.com>
Acked-by: Leif Lindholm <quic_llindhol@quicinc.com>
13 files changed:
.gitmodules
ReadMe.rst
UnitTestFrameworkPkg/Include/Library/GoogleTestLib.h [new file with mode: 0644]
UnitTestFrameworkPkg/Library/GoogleTestLib/GoogleTestLib.inf [new file with mode: 0644]
UnitTestFrameworkPkg/Library/GoogleTestLib/GoogleTestLib.uni [new file with mode: 0644]
UnitTestFrameworkPkg/Library/GoogleTestLib/googletest [new submodule]
UnitTestFrameworkPkg/ReadMe.md
UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp [new file with mode: 0644]
UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf [new file with mode: 0644]
UnitTestFrameworkPkg/Test/UnitTestFrameworkPkgHostTest.dsc
UnitTestFrameworkPkg/UnitTestFrameworkPkg.ci.yaml
UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec
UnitTestFrameworkPkg/UnitTestFrameworkPkgHost.dsc.inc

index b845c9ee3ff00e6b4a7c76820d80eccf30af24bc..8011a88d9d2551ba69a44008c686195fbfceffca 100644 (file)
@@ -20,3 +20,6 @@
 [submodule "RedfishPkg/Library/JsonLib/jansson"]
        path = RedfishPkg/Library/JsonLib/jansson
        url = https://github.com/akheron/jansson
+[submodule "UnitTestFrameworkPkg/Library/GoogleTestLib/googletest"]
+       path = UnitTestFrameworkPkg/Library/GoogleTestLib/googletest
+       url = https://github.com/google/googletest.git
index 8f5db11281bf87d5197b62fec669c421de35a4e9..497d96355908f7abeeefe27544a0c704e38af26d 100644 (file)
@@ -93,6 +93,7 @@ that are covered by additional licenses.
 -  `MdeModulePkg/Library/BrotliCustomDecompressLib/brotli <https://github.com/google/brotli/blob/666c3280cc11dc433c303d79a83d4ffbdd12cc8d/LICENSE>`__\r
 -  `MdeModulePkg/Universal/RegularExpressionDxe/oniguruma <https://github.com/kkos/oniguruma/blob/abfc8ff81df4067f309032467785e06975678f0d/COPYING>`__\r
 -  `UnitTestFrameworkPkg/Library/CmockaLib/cmocka <https://github.com/tianocore/edk2-cmocka/blob/f5e2cd77c88d9f792562888d2b70c5a396bfbf7a/COPYING>`__\r
+-  `UnitTestFrameworkPkg/Library/GoogleTestLib/googletest <https://github.com/google/googletest/blob/86add13493e5c881d7e4ba77fb91c1f57752b3a4/LICENSE>`__\r
 -  `RedfishPkg/Library/JsonLib/jansson <https://github.com/akheron/jansson/blob/2882ead5bb90cf12a01b07b2c2361e24960fae02/LICENSE>`__\r
 \r
 The EDK II Project is composed of packages. The maintainers for each package\r
diff --git a/UnitTestFrameworkPkg/Include/Library/GoogleTestLib.h b/UnitTestFrameworkPkg/Include/Library/GoogleTestLib.h
new file mode 100644 (file)
index 0000000..ebec766
--- /dev/null
@@ -0,0 +1,14 @@
+/** @file\r
+  GoogleTestLib class with APIs from the googletest project\r
+\r
+  Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#ifndef GOOGLE_TEST_LIB_H_\r
+#define GOOGLE_TEST_LIB_H_\r
+\r
+#include <gtest/gtest.h>\r
+\r
+#endif\r
diff --git a/UnitTestFrameworkPkg/Library/GoogleTestLib/GoogleTestLib.inf b/UnitTestFrameworkPkg/Library/GoogleTestLib/GoogleTestLib.inf
new file mode 100644 (file)
index 0000000..68db75d
--- /dev/null
@@ -0,0 +1,36 @@
+## @file\r
+#  This module provides GoogleTest Library implementation.\r
+#\r
+#  Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>\r
+#  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION     = 0x00010005\r
+  BASE_NAME       = GoogleTestLib\r
+  MODULE_UNI_FILE = GoogleTestLib.uni\r
+  FILE_GUID       = A90E4751-AD30-43CC-980B-01E356B49ADF\r
+  MODULE_TYPE     = BASE\r
+  VERSION_STRING  = 0.1\r
+  LIBRARY_CLASS   = GoogleTestLib|HOST_APPLICATION\r
+\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64 ARM AARCH64\r
+#\r
+\r
+[Sources]\r
+  googletest/googletest/src/gtest-all.cc\r
+\r
+[Packages]\r
+  UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec\r
+\r
+[BuildOptions]\r
+  MSFT:*_*_*_CC_FLAGS     == /c /EHsc /Zi\r
+  MSFT:NOOPT_*_*_CC_FLAGS =  /Od\r
+\r
+  GCC:*_*_*_CC_FLAGS     == -g -c\r
+\r
+  GCC:NOOPT_*_*_CC_FLAGS =  -O0\r
+  GCC:*_*_IA32_CC_FLAGS  =  -m32\r
+  GCC:*_*_X64_CC_FLAGS   =  -m64\r
diff --git a/UnitTestFrameworkPkg/Library/GoogleTestLib/GoogleTestLib.uni b/UnitTestFrameworkPkg/Library/GoogleTestLib/GoogleTestLib.uni
new file mode 100644 (file)
index 0000000..14c862a
--- /dev/null
@@ -0,0 +1,14 @@
+// /** @file\r
+// This module provides GoogleTest Library implementation.\r
+//\r
+// This module provides GoogleTest Library implementation.\r
+//\r
+// Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>\r
+//\r
+// SPDX-License-Identifier: BSD-2-Clause-Patent\r
+//\r
+// **/\r
+\r
+#string STR_MODULE_ABSTRACT             #language en-US "GoogleTest Library implementation"\r
+\r
+#string STR_MODULE_DESCRIPTION          #language en-US "This module provides GoogleTest Library implementation."\r
diff --git a/UnitTestFrameworkPkg/Library/GoogleTestLib/googletest b/UnitTestFrameworkPkg/Library/GoogleTestLib/googletest
new file mode 160000 (submodule)
index 0000000..86add13
--- /dev/null
@@ -0,0 +1 @@
+Subproject commit 86add13493e5c881d7e4ba77fb91c1f57752b3a4
index e696412cb3cf637dfbdc46a4ff635b0bca7a1e93..9ce04b7f3eb6f0b34065271bfc9a7057580a806d 100644 (file)
@@ -2,12 +2,67 @@
 \r
 ## About\r
 \r
-This package adds a unit test framework capable of building tests for multiple contexts including\r
+This package provides unit test frameworks capable of building tests for multiple contexts including\r
 the UEFI shell environment and host-based environments. It allows for unit test development to focus\r
-on the tests and leave error logging, result formatting, context persistance, and test running to the framework.\r
+on the tests and leave error logging, result formatting, context persistence, and test running to the framework.\r
 The unit test framework works well for low level unit tests as well as system level tests and\r
 fits easily in automation frameworks.\r
 \r
+### Framework\r
+\r
+The first unit test framework is called **Framework** and is implemented as a set of EDK II libraries.\r
+The Framework supports both host-based unit tests and target-based unit tests that share the same\r
+source style, macros, and APIs. In some scenarios, the same unit test case sources can be built\r
+for both host-based unit test execution and target-based unit test execution. Host-based unit tests\r
+that require mocked interfaces can use the mocking infrastructure provided by\r
+[cmocka](https://api.cmocka.org/) that is included in the UnitTestFrameworkPkg as a submodule.\r
+\r
+### GoogleTest\r
+\r
+The second unit test framework supported by the UnitTestFrameworkPkg is\r
+[GoogleTest](http://google.github.io/googletest/) that can be used to implement host-based unit tests.\r
+Use of GoogleTest for target-based unit tests of EDK II components is not supported. If a\r
+host-based unit test requires mocked interfaces, then the Framework with cmocka support should be\r
+used instead. Enabling support for mocked interfaces with GoogleTest is being actively investigated.\r
+[GoogleTest on GitHub](https://github.com/google/googletest) is included in the UnitTestFrameworkPkg\r
+as a submodule.\r
+\r
+GoogleTest requires less overhead to register test suites and test cases compared to the Framework.\r
+There are also a number of tools that layer on top of GoogleTest that improve developer productivity.\r
+One example is the VS Code extension\r
+[C++ TestMate](https://marketplace.visualstudio.com/items?itemName=matepek.vscode-catch2-test-adapter)\r
+that may be used to implement, run, and debug unit tests implemented using GoogleTest.\r
+\r
+If a component can be tested with host-based unit tests without support for mocked interfaces,\r
+then GoogleTest is recommended. The MdePkg contains a port of the BaseSafeIntLib unit tests in\r
+the GoogleTest style so the differences between GoogleTest and Framework unit tests can be reviewed.\r
+The paths to the BaseSafeIntLib unit tests are:\r
+\r
+* MdePkg\Test\UnitTest\Library\BaseSafeIntLib\r
+* MdePkg\Test\GoogleTest\Library\BaseSafeIntLib\r
+\r
+## Framework and GoogleTest Feature Comparison\r
+\r
+| Feature                     | Framework | GoogleTest |\r
+|:----------------------------|:---------:|:----------:|\r
+| Host Based Unit Tests       |    YES    |    YES     |\r
+| Target Based Unit Tests     |    YES    |     NO     |\r
+| Unit Test Source Language   |     C     |    C++     |\r
+| Register Test Suite         |    YES    |    Auto    |\r
+| Register Test Case          |    YES    |    Auto    |\r
+| Death/Expected Assert Tests |    YES    |    YES     |\r
+| Setup/Teardown Hooks        |    YES    |    YES     |\r
+| Value-Parameterized Tests   |    NO     |    YES     |\r
+| Typed Tests                 |    NO     |    YES     |\r
+| Type-Parameterized Tests    |    NO     |    YES     |\r
+| Timeout Support             |    NO     |    YES     |\r
+| Mocking Support             |   Cmocka  |     NO     |\r
+| JUNIT XML Reports           |    YES    |    YES     |\r
+| Execute subset of tests     |    NO     |    YES     |\r
+| VS Code Extensions          |    NO     |    YES     |\r
+\r
+## Framework Libraries\r
+\r
 ### UnitTestLib\r
 \r
 The main "framework" library. The core of the framework is the Framework object, which can have any number\r
@@ -31,10 +86,10 @@ in supporting a system reboot in the middle of a test run.
 \r
 Library provides function to run at the end of a framework test run and handles formatting the report.\r
 This is a common customization point and allows the unit test framework to fit its output reports into\r
-other test infrastructure. In this package a simple library instances has been supplied to output test\r
+other test infrastructure. In this package simple library instances have been supplied to output test\r
 results to the console as plain text.\r
 \r
-## Samples\r
+## Framework Samples\r
 \r
 There is a sample unit test provided as both an example of how to write a unit test and leverage\r
 many of the features of the framework. This sample can be found in the `Test/UnitTest/Sample/SampleUnitTest`\r
@@ -43,7 +98,7 @@ directory.
 The sample is provided in PEI, SMM, DXE, and UEFI App flavors. It also has a flavor for the HOST_APPLICATION\r
 build type, which can be run on a host system without needing a target.\r
 \r
-## Usage\r
+## Framework Usage\r
 \r
 This section is built a lot like a "Getting Started". We'll go through some of the components that are needed\r
 when constructing a unit test and some of the decisions that are made by the test writer. We'll also describe\r
@@ -51,7 +106,7 @@ how to check for expected conditions in test cases and a bit of the logging char
 \r
 Most of these examples will refer to the SampleUnitTestUefiShell app found in this package.\r
 \r
-### Requirements - INF\r
+### Framework Requirements - INF\r
 \r
 In our INF file, we'll need to bring in the `UnitTestLib` library. Conveniently, the interface\r
 header for the `UnitTestLib` is located in `MdePkg`, so you shouldn't need to depend on any other\r
@@ -80,7 +135,7 @@ to make sure that the module `BASE_NAME` contains the word `Test`...
   BASE_NAME      = SampleUnitTestUefiShell\r
 ```\r
 \r
-### Requirements - Code\r
+### Framework Requirements - Code\r
 \r
 Not to state the obvious, but let's make sure we have the following include before getting too far along...\r
 \r
@@ -90,9 +145,9 @@ Not to state the obvious, but let's make sure we have the following include befo
 \r
 Now that we've got that squared away, let's look at our 'Main()'' routine (or DriverEntryPoint() or whatever).\r
 \r
-### Configuring the Framework\r
+### Framework Configuration\r
 \r
-Everything in the UnitTestPkg framework is built around an object called -- conveniently -- the Framework.\r
+Everything in the UnitTestFrameworkPkg framework is built around an object called -- conveniently -- the Framework.\r
 This Framework object will contain all the information about our test, the test suites and test cases associated\r
 with it, the current location within the test pass, and any results that have been recorded so far.\r
 \r
@@ -102,7 +157,7 @@ The long name and version strings are just for user presentation and relatively
 will be used to name any cache files and/or test results, so should be a name that makes sense in that context.\r
 These strings are copied internally to the Framework, so using stack-allocated or literal strings is fine.\r
 \r
-In the 'SampleUnitTestUefiShell' app, the module name is used as the short name, so the init looks like this.\r
+In the 'SampleUnitTestUefiShell' app, the module name is used as the short name, so the initialization looks like this.\r
 \r
 ```c\r
 DEBUG(( DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION ));\r
@@ -144,11 +199,11 @@ will be used when adding test cases.
 Great! Now we've finished some of the cruft, red tape, and busy work. We're ready to add some tests. Adding a test\r
 to a test suite is accomplished with the -- you guessed it -- `AddTestCase` function. It takes in the suite handle;\r
 a `CHAR8` string for the description and class name; a function pointer for the test case itself; additional, optional\r
-function pointers for prerequisite check and cleanup routines; and and optional pointer to a context structure.\r
+function pointers for prerequisite check and cleanup routines; and an optional pointer to a context structure.\r
 \r
 Okay, that's a lot. Let's take it one piece at a time. The description and class name strings are very similar in\r
 usage to the suite title and package name strings in the test suites. The former is for user presentation and the\r
-latter is for xUnit parsing. The test case function pointer is what is actually executed as the "test" and the\r
+latter is for xUnit parsing. The test case function pointer is what is executed as the "test" and the\r
 prototype should be `UNIT_TEST_FUNCTION`. The last three parameters require a little bit more explaining.\r
 \r
 The prerequisite check function has a prototype of `UNIT_TEST_PREREQUISITE` and -- if provided -- will be called\r
@@ -180,7 +235,7 @@ Once all the suites and cases are added, it's time to run the Framework.
 Status = RunAllTestSuites( Framework );\r
 ```\r
 \r
-### A Simple Test Case\r
+### Framework - A Simple Test Case\r
 \r
 We'll take a look at the below test case from 'SampleUnitTestApp'...\r
 \r
@@ -217,9 +272,9 @@ _Note_ that this early return can have implications for memory leakage.
 \r
 At the end, if all test criteria pass, you should return `UNIT_TEST_PASSED`.\r
 \r
-### More Complex Cases\r
+### Framework - More Complex Cases\r
 \r
-To write more advanced tests, first take a look at all the Assertion and Logging macros provided in the framework.\r
+To write more advanced tests, first look at all the Assertion and Logging macros provided in the framework.\r
 \r
 Beyond that, if you're writing host-based tests and want to take a dependency on the UnitTestFrameworkPkg, you can\r
 leverage the `cmocka.h` interface and write tests with all the features of the Cmocka framework.\r
@@ -227,6 +282,125 @@ leverage the `cmocka.h` interface and write tests with all the features of the C
 Documentation for Cmocka can be found here:\r
 https://api.cmocka.org/\r
 \r
+## GoogleTest Samples\r
+\r
+There is a sample unit test provided as both an example of how to write a unit test and leverage\r
+many of the GoogleTest features. This sample can be found in the `Test/GoogleTest/Sample/SampleGoogleTest`\r
+directory.\r
+\r
+The sample is provided for the HOST_APPLICATION build type, which can be run on a host system without\r
+needing a target.\r
+\r
+## GoogleTest Usage\r
+\r
+This section is built a lot like a "Getting Started". We'll go through some of the components that are needed\r
+when constructing a unit test and some of the decisions that are made by the test writer. We'll also describe\r
+how to check for expected conditions in test cases and a bit of the logging characteristics.\r
+\r
+Most of these examples will refer to the SampleGoogleTestHost app found in this package.\r
+\r
+### GoogleTest Requirements - INF\r
+\r
+In our INF file, we'll need to bring in the `GoogleTest` library. Conveniently, the interface\r
+header for the `GoogleTest` is in `UnitTestFrameworkPkg`, so you shouldn't need to depend on any other\r
+packages. As long as your DSC file knows where to find the lib implementation that you want to use,\r
+you should be good to go.\r
+\r
+See this example in 'SampleGoogleTestHost.inf'...\r
+\r
+```\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec\r
+\r
+[LibraryClasses]\r
+  GoogleTestLib\r
+  BaseLib\r
+  DebugLib\r
+```\r
+\r
+Also, if you want you test to automatically be picked up by the Test Runner plugin, you will need\r
+to make sure that the module `BASE_NAME` contains the word `Test`...\r
+\r
+```\r
+[Defines]\r
+  BASE_NAME      = SampleGoogleTestHost\r
+```\r
+\r
+### GoogleTest Requirements - Code\r
+\r
+Not to state the obvious, but let's make sure we have the following include before getting too far along...\r
+\r
+```\r
+#include <gtest/gtest.h>\r
+extern "C" {\r
+  #include <Uefi.h>\r
+  #include <Library/BaseLib.h>\r
+  #include <Library/DebugLib.h>\r
+}\r
+```\r
+\r
+GoogleTest applications are implemented in C++. The first include brings in the\r
+GoogleTest definitions. Other EDK II related include files must be wrapped in\r
+`extern "C" {}` because they are C include files. Link failures will occur if\r
+this is not done.\r
+\r
+Now that we've got that squared away, let's look at our 'Main()'' routine (or DriverEntryPoint() or whatever).\r
+\r
+### GoogleTest Configuration\r
+\r
+Unlike the Framework, GoogleTest does not require test suites or test cases to\r
+be registered. Instead, the test cases declare the test suite name and test\r
+case name as part of their implementation. The only requirement for GoogleTest\r
+is to have a `main()` function that initialize the GoogleTest infrastructure and\r
+call the service `RUN_ALL_TESTS()` to run all the unit tests.\r
+\r
+```c\r
+int main(int argc, char* argv[]) {\r
+  testing::InitGoogleTest(&argc, argv);\r
+  return RUN_ALL_TESTS();\r
+}\r
+```\r
+\r
+### GoogleTest - A Simple Test Case\r
+\r
+We'll look at the below test case from 'SampleGoogleTestHost'...\r
+\r
+```c\r
+TEST(SimpleMathTests, OnePlusOneShouldEqualTwo) {\r
+  UINTN  A;\r
+  UINTN  B;\r
+  UINTN  C;\r
+\r
+  A = 1;\r
+  B = 1;\r
+  C = A + B;\r
+\r
+  ASSERT_EQ (C, 2);\r
+}\r
+```\r
+\r
+This uses the simplest form of a GoogleTest unit test using `TEST()` that\r
+declares the test suite name and the unit test name within that test suite.\r
+The unit test performs actions and typically makes calls to the code under test\r
+and contains test assertions to verify that the code under test behaves as\r
+expected for the given inputs.\r
+\r
+In this test case, the `ASSERT_EQ` assertion is being used to establish that the business logic has functioned\r
+correctly. There are several assertion macros, and you are encouraged to use one that matches as closely to your\r
+intended test criterium as possible, because the logging is specific to the macro and more specific macros have more\r
+detailed logs. When in doubt, there are always `ASSERT_TRUE` and `ASSERT_FALSE`. Assertion macros that fail their\r
+test criterium will immediately return from the test case with a failed status and log an error string.\r
+_Note_ that this early return can have implications for memory leakage.\r
+\r
+There is no return status from a GooglTest unit test. If no assertions are\r
+triggered then the unit test has a passing status.\r
+\r
+### GoogleTest - More Complex Cases\r
+\r
+To write more advanced tests, take a look at the\r
+[GoogleTest User's Guide](http://google.github.io/googletest/).\r
+\r
 ## Development\r
 \r
 ### Iterating on a Single Test\r
@@ -243,11 +417,11 @@ stuart_ci_build -c .pytool/CISettings.py TOOL_CHAIN_TAG=VS2017 -p MdePkg -t NOOP
 \r
 ### Hooking BaseLib\r
 \r
-Most unit test mocking can be performed by the functions provided in the UnitTestFramework libraries, but since\r
+Most unit test mocking can be performed by the functions provided in the UnitTestFrameworkPkg libraries, but since\r
 BaseLib is consumed by the Framework itself, it requires different techniques to substitute parts of the\r
 functionality.\r
 \r
-To solve some of this, the UnitTestFramework consumes a special implementation of BaseLib for host-based tests.\r
+To solve some of this, the UnitTestFrameworkPkg consumes a special implementation of BaseLib for host-based tests.\r
 This implementation contains a [hook table](https://github.com/tianocore/edk2/blob/e188ecc8b4aed8fdd26b731d43883861f5e5e7b4/MdePkg/Test/UnitTest/Include/Library/UnitTestHostBaseLib.h#L507)\r
 that can be used to substitute test functionality for any of the BaseLib functions. By default, this implementation\r
 will use the underlying BaseLib implementation, so the unit test writer only has to supply minimal code to test a\r
@@ -255,7 +429,7 @@ particular case.
 \r
 ### Debugging the Framework Itself\r
 \r
-While most of the tests that are produced by the UnitTestFramework are easy to step through in a debugger, the Framework\r
+While most of the tests that are produced by the UnitTestFrameworkPkg are easy to step through in a debugger, the Framework\r
 itself consumes code (mostly Cmocka) that sets its own build flags. These flags cause parts of the Framework to not\r
 export symbols and captures exceptions, and as such are harder to debug. We have provided a Stuart parameter to force\r
 symbolic debugging to be enabled.\r
@@ -269,15 +443,17 @@ stuart_ci_build -c .pytool/CISettings.py TOOL_CHAIN_TAG=VS2019 -p MdePkg -t NOOP
 ## Building and Running Host-Based Tests\r
 \r
 The EDK2 CI infrastructure provides a convenient way to run all host-based tests -- in the the entire tree or just\r
-selected packages -- and aggregate all the the reports, including highlighting any failures. This functionality is\r
-provided through the Stuart build system (published by EDK2-PyTools) and the `NOOPT` build target.\r
+selected packages -- and aggregate all the reports, including highlighting any failures. This functionality is\r
+provided through the Stuart build system (published by EDK2-PyTools) and the `NOOPT` build target. The sections that\r
+follow use Framework examples. Unit tests based on GoogleTest are built and run the same way. The text output and\r
+JUNIT XML output format have small differences.\r
 \r
 ### Building Locally\r
 \r
 First, to make sure you're working with the latest PyTools, run the following command:\r
 \r
 ```bash\r
-# Would recommend to run this in a Python venv, but that's out of scope for this doc.\r
+# Would recommend running this in a Python venv, but that's out of scope for this doc.\r
 python -m pip install --upgrade -r ./pip-requirements.txt\r
 ```\r
 \r
@@ -361,7 +537,7 @@ RUNNING TEST SUITE: Int Safe Conversions Test Suite
 ```\r
 \r
 You can also, if you are so inclined, read the output from the exact instance of the test that was run during\r
-`stuart_ci_build`. The ouput file can be found on a path that looks like:\r
+`stuart_ci_build`. The output file can be found on a path that looks like:\r
 \r
 `Build/<Package>/HostTest/<Arch>/<TestName>.<TestSuiteName>.<Arch>.result.xml`\r
 \r
@@ -389,22 +565,30 @@ c:\_uefi\MdePkg\Test\UnitTest\Library\BaseSafeIntLib\TestBaseSafeIntLib.c:35: er
 \r
 ### XML Reporting Mode\r
 \r
-Since these applications are built using the CMocka framework, they can also use the following env variables to output\r
-in a structured XML rather than text:\r
+Unit test applications using Framework are built using Cmocka that requires the\r
+following environment variables to be set to generate structured XML output\r
+rather than text:\r
 \r
-```text\r
+```\r
 CMOCKA_MESSAGE_OUTPUT=xml\r
 CMOCKA_XML_FILE=<absolute or relative path to output file>\r
 ```\r
 \r
+Unit test applications using GoogleTest require the following environment\r
+variable to be set to generate structured XML output rather than text:\r
+\r
+```\r
+GTEST_OUTPUT=xml:<absolute or relative path to output file>\r
+```\r
+\r
 This mode is used by the test running plugin to aggregate the results for CI test status reporting in the web view.\r
 \r
 ### Important Note\r
 \r
-This works on both Windows and Linux, but is currently limited to x64 architectures. Working on getting others, but we\r
+This works on both Windows and Linux but is currently limited to x64 architectures. Working on getting others, but we\r
 also welcome contributions.\r
 \r
-## Known Limitations\r
+## Framework Known Limitations\r
 \r
 ### PEI, DXE, SMM\r
 \r
@@ -418,7 +602,7 @@ PEI, DXE, and SMM is forthcoming, but should be considered beta/staging for now.
 The host-based test framework is powered internally by the Cmocka framework. As such, it has abilities\r
 that the target-based tests don't (yet). It would be awesome if this meant that it was a super set of\r
 the target-based tests, and it worked just like the target-based tests but with more features. Unfortunately,\r
-this is not the case. While care has been taken to keep them as close a possible, there are a few known\r
+this is not the case. While care has been taken to keep them as close as possible, there are a few known\r
 inconsistencies that we're still ironing out. For example, the logging messages in the target-based tests\r
 are cached internally and associated with the running test case. They can be saved later as part of the\r
 reporting lib. This isn't currently possible with host-based. Only the assertion failures are logged.\r
@@ -441,6 +625,9 @@ Non-Host-Based (PEI/DXE/SMM/Shell) Tests for a Functionality or Feature   | Simi
   ComponentY/\r
     ComponentY.inf\r
     ComponentY.c\r
+    GoogleTest/\r
+      ComponentYHostGoogleTest.inf    # Host-Based Test for Driver Module\r
+      ComponentYGoogleTest.cpp\r
     UnitTest/\r
       ComponentYHostUnitTest.inf      # Host-Based Test for Driver Module\r
       ComponentYUnitTest.c\r
@@ -455,11 +642,23 @@ Non-Host-Based (PEI/DXE/SMM/Shell) Tests for a Functionality or Feature   | Simi
     SpecificLibDxe/\r
       SpecificLibDxe.c\r
       SpecificLibDxe.inf\r
+      GoogleTest/                    # Host-Based Test for Specific Library Implementation\r
+        SpecificLibDxeHostGoogleTest.cpp\r
+        SpecificLibDxeHostGoogleTest.inf\r
       UnitTest/                      # Host-Based Test for Specific Library Implementation\r
         SpecificLibDxeHostUnitTest.c\r
         SpecificLibDxeHostUnitTest.inf\r
   Test/\r
     <Package>HostTest.dsc             # Host-Based Test Apps\r
+    GoogleTest/\r
+      InterfaceX\r
+        InterfaceXHostGoogleTest.inf  # Host-Based App (should be in Test/<Package>HostTest.dsc)\r
+        InterfaceXUnitTest.cpp        # Test Logic\r
+\r
+      GeneralPurposeLib/              # Host-Based Test for any implementation of GeneralPurposeLib\r
+        GeneralPurposeLibTest.cpp\r
+        GeneralPurposeLibHostUnitTest.inf\r
+\r
     UnitTest/\r
       InterfaceX\r
         InterfaceXHostUnitTest.inf    # Host-Based App (should be in Test/<Package>HostTest.dsc)\r
diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp
new file mode 100644 (file)
index 0000000..c83e585
--- /dev/null
@@ -0,0 +1,263 @@
+/** @file\r
+  This is a sample to demonstrates the use of GoogleTest that supports host\r
+  execution environments.\r
+\r
+  Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <gtest/gtest.h>\r
+extern "C" {\r
+  #include <Uefi.h>\r
+  #include <Library/BaseLib.h>\r
+  #include <Library/DebugLib.h>\r
+}\r
+\r
+/**\r
+  Sample unit test that verifies the expected result of an unsigned integer\r
+  addition operation.\r
+**/\r
+TEST(SimpleMathTests, OnePlusOneShouldEqualTwo) {\r
+  UINTN  A;\r
+  UINTN  B;\r
+  UINTN  C;\r
+\r
+  A = 1;\r
+  B = 1;\r
+  C = A + B;\r
+\r
+  ASSERT_EQ (C, (UINTN)2);\r
+}\r
+\r
+/**\r
+  Sample unit test that verifies that a global BOOLEAN is updatable.\r
+**/\r
+class GlobalBooleanVarTests : public ::testing::Test {\r
+  public:\r
+    BOOLEAN  SampleGlobalTestBoolean  = FALSE;\r
+};\r
+\r
+TEST_F(GlobalBooleanVarTests, GlobalBooleanShouldBeChangeable) {\r
+  SampleGlobalTestBoolean = TRUE;\r
+  ASSERT_TRUE (SampleGlobalTestBoolean);\r
+\r
+  SampleGlobalTestBoolean = FALSE;\r
+  ASSERT_FALSE (SampleGlobalTestBoolean);\r
+}\r
+\r
+/**\r
+  Sample unit test that logs a warning message and verifies that a global\r
+  pointer is updatable.\r
+**/\r
+class GlobalVarTests : public ::testing::Test {\r
+  public:\r
+    VOID  *SampleGlobalTestPointer = NULL;\r
+\r
+  protected:\r
+  void SetUp() override {\r
+    ASSERT_EQ ((UINTN)SampleGlobalTestPointer, (UINTN)NULL);\r
+  }\r
+  void TearDown() {\r
+    SampleGlobalTestPointer = NULL;\r
+  }\r
+};\r
+\r
+TEST_F(GlobalVarTests, GlobalPointerShouldBeChangeable) {\r
+  SampleGlobalTestPointer = (VOID *)-1;\r
+  ASSERT_EQ ((UINTN)SampleGlobalTestPointer, (UINTN)((VOID *)-1));\r
+}\r
+\r
+\r
+/**\r
+  Set PcdDebugPropertyMask for each MacroTestsAssertsEnabledDisabled test\r
+**/\r
+class MacroTestsAssertsEnabledDisabled : public testing::TestWithParam<UINT8> {\r
+  void SetUp() {\r
+    PatchPcdSet8 (PcdDebugPropertyMask, GetParam());\r
+  }\r
+};\r
+\r
+/**\r
+  Sample unit test using the ASSERT_TRUE() macro.\r
+**/\r
+TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertTrue) {\r
+  UINT64  Result;\r
+\r
+  //\r
+  // This test passes because expression always evaluated to TRUE.\r
+  //\r
+  ASSERT_TRUE (TRUE);\r
+\r
+  //\r
+  // This test passes because expression always evaluates to TRUE.\r
+  //\r
+  Result = LShiftU64 (BIT0, 1);\r
+  ASSERT_TRUE (Result == BIT1);\r
+}\r
+\r
+/**\r
+  Sample unit test using the ASSERT_FALSE() macro.\r
+**/\r
+TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertFalse) {\r
+  UINT64  Result;\r
+\r
+  //\r
+  // This test passes because expression always evaluated to FALSE.\r
+  //\r
+  ASSERT_FALSE (FALSE);\r
+\r
+  //\r
+  // This test passes because expression always evaluates to FALSE.\r
+  //\r
+  Result = LShiftU64 (BIT0, 1);\r
+  ASSERT_FALSE (Result == BIT0);\r
+}\r
+\r
+/**\r
+  Sample unit test using the ASSERT_EQ() macro.\r
+**/\r
+TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertEqual) {\r
+  UINT64  Result;\r
+\r
+  //\r
+  // This test passes because both values are always equal.\r
+  //\r
+  ASSERT_EQ (1, 1);\r
+\r
+  //\r
+  // This test passes because both values are always equal.\r
+  //\r
+  Result = LShiftU64 (BIT0, 1);\r
+  ASSERT_EQ (Result, (UINT64)BIT1);\r
+}\r
+\r
+/**\r
+  Sample unit test using the ASSERT_STREQ() macro.\r
+**/\r
+TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertMemEqual) {\r
+  CHAR8  *String1;\r
+  CHAR8  *String2;\r
+\r
+  //\r
+  // This test passes because String1 and String2 are the same.\r
+  //\r
+  String1 = (CHAR8 *)"Hello";\r
+  String2 = (CHAR8 *)"Hello";\r
+  ASSERT_STREQ (String1, String2);\r
+}\r
+\r
+/**\r
+  Sample unit test using the ASSERT_NE() macro.\r
+**/\r
+TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertNotEqual) {\r
+  UINT64  Result;\r
+\r
+  //\r
+  // This test passes because both values are never equal.\r
+  //\r
+  ASSERT_NE (0, 1);\r
+\r
+  //\r
+  // This test passes because both values are never equal.\r
+  //\r
+  Result = LShiftU64 (BIT0, 1);\r
+  ASSERT_NE (Result, (UINT64)BIT0);\r
+}\r
+\r
+/**\r
+  Sample unit test using the ASSERT_TRUE() and ASSERT(FALSE)\r
+  and EFI_EFFOR() macros to check status\r
+**/\r
+TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertNotEfiError) {\r
+  //\r
+  // This test passes because the status is not an EFI error.\r
+  //\r
+  ASSERT_FALSE (EFI_ERROR (EFI_SUCCESS));\r
+\r
+  //\r
+  // This test passes because the status is not an EFI error.\r
+  //\r
+  ASSERT_FALSE (EFI_ERROR (EFI_WARN_BUFFER_TOO_SMALL));\r
+}\r
+\r
+/**\r
+  Sample unit test using the ASSERT_EQ() macro to compare EFI_STATUS values.\r
+**/\r
+TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertStatusEqual) {\r
+  //\r
+  // This test passes because the status value are always equal.\r
+  //\r
+  ASSERT_EQ (EFI_SUCCESS, EFI_SUCCESS);\r
+}\r
+\r
+/**\r
+  Sample unit test using ASSERT_NE() macro to make sure a pointer is not NULL.\r
+**/\r
+TEST_P(MacroTestsAssertsEnabledDisabled, MacroAssertNotNull) {\r
+  UINT64  Result;\r
+\r
+  //\r
+  // This test passes because the pointer is never NULL.\r
+  //\r
+  ASSERT_NE (&Result, (UINT64 *)NULL);\r
+}\r
+\r
+/**\r
+  Sample unit test using that should not generate any ASSERTs()\r
+**/\r
+TEST_P(MacroTestsAssertsEnabledDisabled, MacroExpectNoAssertFailure) {\r
+  //\r
+  // This test passes because it never triggers an ASSERT().\r
+  //\r
+  ASSERT (TRUE);\r
+\r
+  //\r
+  // This test passes because DecimalToBcd() does not ASSERT() if the\r
+  // value passed in is <= 99.\r
+  //\r
+  DecimalToBcd8 (99);\r
+}\r
+\r
+/**\r
+  Sample unit test using the ASSERT_DEATH() macro to test expected ASSERT()s.\r
+**/\r
+TEST_P(MacroTestsAssertsEnabledDisabled, MacroExpectAssertFailure) {\r
+  //\r
+  // Skip tests that verify an ASSERT() is triggered if ASSERT()s are disabled.\r
+  //\r
+  if ((PcdGet8 (PcdDebugPropertyMask) & BIT0) == 0x00) {\r
+    return;\r
+  }\r
+\r
+  //\r
+  // This test passes because it directly triggers an ASSERT().\r
+  //\r
+  ASSERT_DEATH (ASSERT (FALSE), "");\r
+\r
+  //\r
+  // This test passes because DecimalToBcd() generates an ASSERT() if the\r
+  // value passed in is >= 100.  The expected ASSERT() is caught by the unit\r
+  // test framework and ASSERT_DEATH() returns without an error.\r
+  //\r
+  ASSERT_DEATH (DecimalToBcd8 (101), "");\r
+}\r
+\r
+INSTANTIATE_TEST_SUITE_P(ValidInput,\r
+                         MacroTestsAssertsEnabledDisabled,\r
+                         ::testing::Values(PcdGet8 (PcdDebugPropertyMask) | BIT0, PcdGet8 (PcdDebugPropertyMask) & (~BIT0)));\r
+\r
+/**\r
+  Sample unit test using the SCOPED_TRACE() macro for trace messages.\r
+**/\r
+TEST(MacroTestsMessages, MacroTraceMessage) {\r
+  //\r
+  // Example of logging.\r
+  //\r
+  SCOPED_TRACE ("SCOPED_TRACE message\n");\r
+}\r
+\r
+int main(int argc, char* argv[]) {\r
+  testing::InitGoogleTest(&argc, argv);\r
+  return RUN_ALL_TESTS();\r
+}\r
diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf
new file mode 100644 (file)
index 0000000..37e7c86
--- /dev/null
@@ -0,0 +1,35 @@
+## @file\r
+# This is a sample to demonstrates the use of GoogleTest that supports host\r
+# execution environments.\r
+#\r
+# Copyright (c) 2022, Intel Corporation. All rights reserved.<BR>\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION    = 0x00010005\r
+  BASE_NAME      = SampleGoogleTestHost\r
+  FILE_GUID      = 7D8BBFBB-7977-4AEE-A59F-257BF5C2F87C\r
+  MODULE_TYPE    = HOST_APPLICATION\r
+  VERSION_STRING = 1.0\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  SampleGoogleTest.cpp\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec\r
+\r
+[LibraryClasses]\r
+  GoogleTestLib\r
+  BaseLib\r
+  DebugLib\r
+\r
+[Pcd]\r
+  gEfiMdePkgTokenSpaceGuid.PcdDebugPropertyMask\r
index 184fdec87acf038278b4b02745c742e4c8469f3c..708ef7f9ab3512a755bf1866cbc5492b42605131 100644 (file)
 \r
 [Components]\r
   #\r
-  # Build HOST_APPLICATION that tests the SampleUnitTest\r
+  # Build HOST_APPLICATIONs that test the SampleUnitTest\r
   #\r
   UnitTestFrameworkPkg/Test/UnitTest/Sample/SampleUnitTest/SampleUnitTestHost.inf\r
+  UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTestHost.inf\r
 \r
   #\r
   # Build HOST_APPLICATION Libraries\r
   #\r
   UnitTestFrameworkPkg/Library/CmockaLib/CmockaLib.inf\r
+  UnitTestFrameworkPkg/Library/GoogleTestLib/GoogleTestLib.inf\r
   UnitTestFrameworkPkg/Library/Posix/DebugLibPosix/DebugLibPosix.inf\r
   UnitTestFrameworkPkg/Library/Posix/MemoryAllocationLibPosix/MemoryAllocationLibPosix.inf\r
   UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLibCmocka.inf\r
index 77d51e13484c6df5d95ebcd6b86353d81ecdcab2..072df6208c926f6ef29a4ae35ce9c78e51ce5cdd 100644 (file)
@@ -78,7 +78,8 @@
     "SpellCheck": {\r
         "AuditOnly": False,           # Fails test but run in AuditOnly mode to collect log\r
         "IgnoreFiles": [             # use gitignore syntax to ignore errors in matching files\r
-            "Library/CmockaLib/cmocka/**/*.*"  # not going to spell check a submodule\r
+            "Library/CmockaLib/cmocka/**/*.*",  # not going to spell check a submodule\r
+            "Library/GoogleTestLib/googletest/**/*.*"  # not going to spell check a submodule\r
         ],\r
         "ExtendWords": [             # words to extend to the dictionary for this package\r
             "testcase",\r
@@ -91,6 +92,7 @@
             "NOFAILURE",\r
             "cmockery",\r
             "DHAVE", # build flag for cmocka in the INF\r
+            "gtest", # file name in GoogleTestLib.inf\r
             "corthon",      # Contact GitHub account in Readme\r
             "mdkinney",     # Contact GitHub account in Readme\r
             "spbrogan"      # Contact GitHub account in Readme\r
index 069289f00969ed2ba5dce1b167d1c3a0d7f29028..ed12f32009d850bb6d6eaa1a2ff68e43ef30cc84 100644 (file)
   PACKAGE_VERSION   = 1.00\r
 \r
 [Includes]\r
+  Include\r
   Library/CmockaLib/cmocka/include\r
+  Library/GoogleTestLib/googletest/googletest/include\r
+  Library/GoogleTestLib/googletest/googlemock/include\r
 \r
 [Includes.Common.Private]\r
   PrivateInclude\r
   Library/CmockaLib/cmocka/include/cmockery\r
+  Library/GoogleTestLib/googletest/googletest\r
 \r
 [LibraryClasses.Common.Private]\r
   ## @libraryclass Allows save and restore unit test internal state\r
   #\r
   UnitTestBootLib|PrivateInclude/Library/UnitTestBootLib.h\r
 \r
+  ## @libraryclass GoogleTest infrastructure\r
+  #\r
+  GoogleTestLib|Include/Library/GoogleTestLib.h\r
+\r
 [Guids]\r
   gUnitTestFrameworkPkgTokenSpaceGuid = { 0x833d3aba, 0x39b4, 0x43a2, { 0xb9, 0x30, 0x7a, 0x34, 0x53, 0x39, 0x31, 0xb3 } }\r
 \r
index 9beeaef1ba5e4fe6bcf8d8123ca832ca3c4aff72..8009337552cc4205e73b162c265a4997e263b9e8 100644 (file)
@@ -14,6 +14,7 @@
   CpuLib|MdePkg/Library/BaseCpuLibNull/BaseCpuLibNull.inf\r
   CacheMaintenanceLib|MdePkg/Library/BaseCacheMaintenanceLibNull/BaseCacheMaintenanceLibNull.inf\r
   CmockaLib|UnitTestFrameworkPkg/Library/CmockaLib/CmockaLib.inf\r
+  GoogleTestLib|UnitTestFrameworkPkg/Library/GoogleTestLib/GoogleTestLib.inf\r
   UnitTestLib|UnitTestFrameworkPkg/Library/UnitTestLib/UnitTestLibCmocka.inf\r
   DebugLib|UnitTestFrameworkPkg/Library/Posix/DebugLibPosix/DebugLibPosix.inf\r
   MemoryAllocationLib|UnitTestFrameworkPkg/Library/Posix/MemoryAllocationLibPosix/MemoryAllocationLibPosix.inf\r
@@ -31,6 +32,7 @@
   #\r
   # MSFT\r
   #\r
+  MSFT:*_*_*_CC_FLAGS               = /EHsc\r
   MSFT:*_*_*_DLINK_FLAGS            == /out:"$(BIN_DIR)\$(MODULE_NAME_GUID).exe" /pdb:"$(BIN_DIR)\$(MODULE_NAME_GUID).pdb" /IGNORE:4001 /NOLOGO /SUBSYSTEM:CONSOLE /DEBUG /STACK:0x40000,0x40000 /NODEFAULTLIB:libcmt.lib libcmtd.lib\r
   MSFT:*_*_IA32_DLINK_FLAGS         = /MACHINE:I386\r
   MSFT:*_*_X64_DLINK_FLAGS          = /MACHINE:AMD64\r
@@ -50,7 +52,7 @@
   #\r
   GCC:*_*_IA32_DLINK_FLAGS == -o $(BIN_DIR)/$(MODULE_NAME_GUID) -m32 -no-pie\r
   GCC:*_*_X64_DLINK_FLAGS  == -o $(BIN_DIR)/$(MODULE_NAME_GUID) -m64 -no-pie\r
-  GCC:*_*_*_DLINK2_FLAGS   == -lgcov\r
+  GCC:*_*_*_DLINK2_FLAGS   == -lgcov -lpthread -lstdc++ -lm\r
 \r
   #\r
   # Need to do this link via gcc and not ld as the pathing to libraries changes from OS version to OS version\r