public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Michael Kubacki" <mikuback@linux.microsoft.com>
To: Michael D Kinney <michael.d.kinney@intel.com>, devel@edk2.groups.io
Cc: Sean Brogan <sean.brogan@microsoft.com>
Subject: Re: [edk2-devel] [edk2-stable202402][Patch V3 6/7] UnitTestFrameworkPkg/SampleGoogleTest: Use EXPECT_ANY_THROW()
Date: Mon, 12 Feb 2024 11:25:42 -0500	[thread overview]
Message-ID: <df66284e-e431-4f04-83b9-11c1f20924f3@linux.microsoft.com> (raw)
In-Reply-To: <20240209203253.488-7-michael.d.kinney@intel.com>

Reviewed-by: Michael Kubacki <michael.kubacki@microsoft.com>

On 2/9/2024 3:32 PM, Michael D Kinney wrote:
> Update GoogleTest samples to use EXPECT_ANY_THROW() instead
> of ASSERT_DEATH(). ASSERT_DEATH() is a very slow method to
> detect an expected ASSERT() condition. Throwing an exception
> from ASSERT() and using EXPECT_ANY_THROW() is several orders
> of magnitude faster.
> 
> Update GoogleTest sample with example of using EXPECT_THROW()
> and EXPECT_THAT() to check for more specific ASSERT() conditions
> that allow unit test cases to test functions that contain
> more than one ASSERT() statement and verify that the expected
> ASSERT() is the one that was actually triggered.
> 
> Update library mappings so target-based unit tests use
> UnitTestDebugAssertLib.inf and host-based unit tests use
> UnitTestDebugAssertLibHost.inf
> 
> Cc: Michael Kubacki <mikuback@linux.microsoft.com>
> Cc: Sean Brogan <sean.brogan@microsoft.com>
> Signed-off-by: Michael D Kinney <michael.d.kinney@intel.com>
> ---
>   UnitTestFrameworkPkg/ReadMe.md                |  2 +-
>   .../SampleGoogleTest/SampleGoogleTest.cpp     | 36 ++++++++++++++++---
>   2 files changed, 32 insertions(+), 6 deletions(-)
> 
> diff --git a/UnitTestFrameworkPkg/ReadMe.md b/UnitTestFrameworkPkg/ReadMe.md
> index d6a3e0c15a2b..d28cb5cb0a5d 100644
> --- a/UnitTestFrameworkPkg/ReadMe.md
> +++ b/UnitTestFrameworkPkg/ReadMe.md
> @@ -59,7 +59,7 @@ reviewed. The paths to the SecureBootVariableLib unit tests are:
>   | Unit Test Source Language   |     C     |    C++     |
>   | Register Test Suite         |    YES    |    Auto    |
>   | Register Test Case          |    YES    |    Auto    |
> -| Death/Expected Assert Tests |    YES    |    YES     |
> +| Expected Assert Tests       |    YES    |    YES     |
>   | Setup/Teardown Hooks        |    YES    |    YES     |
>   | Value-Parameterized Tests   |    NO     |    YES     |
>   | Typed Tests                 |    NO     |    YES     |
> diff --git a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp
> index 94cbf2cf0b3c..2c2765e1e5ab 100644
> --- a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp
> +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/SampleGoogleTest.cpp
> @@ -7,7 +7,7 @@
>   
>   **/
>   
> -#include <gtest/gtest.h>
> +#include <Library/GoogleTestLib.h>
>   extern "C" {
>     #include <Uefi.h>
>     #include <Library/BaseLib.h>
> @@ -229,7 +229,7 @@ TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectNoAssertFailure) {
>   }
>   
>   /**
> -  Sample unit test using the ASSERT_DEATH() macro to test expected ASSERT()s.
> +  Sample unit test using the EXPECT_ANY_THROW() macro to test expected ASSERT()s.
>   **/
>   TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectAssertFailure) {
>     //
> @@ -242,14 +242,35 @@ TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectAssertFailure) {
>     //
>     // This test passes because it directly triggers an ASSERT().
>     //
> -  ASSERT_DEATH (ASSERT (FALSE), "");
> +  EXPECT_ANY_THROW (ASSERT (FALSE));
>   
>     //
>     // This test passes because DecimalToBcd() generates an ASSERT() if the
>     // value passed in is >= 100.  The expected ASSERT() is caught by the unit
> -  // test framework and ASSERT_DEATH() returns without an error.
> +  // test framework and EXPECT_ANY_THROW() returns without an error.
>     //
> -  ASSERT_DEATH (DecimalToBcd8 (101), "");
> +  EXPECT_ANY_THROW (DecimalToBcd8 (101));
> +
> +  //
> +  // This test passes because DecimalToBcd() generates an ASSERT() if the
> +  // value passed in is >= 100.  The expected ASSERT() is caught by the unit
> +  // test framework and throws the C++ exception of type std::runtime_error.
> +  // EXPECT_THROW() returns without an error.
> +  //
> +  EXPECT_THROW (DecimalToBcd8 (101), std::runtime_error);
> +
> +  //
> +  // This test passes because DecimalToBcd() generates an ASSERT() if the
> +  // value passed in is >= 100.  The expected ASSERT() is caught by the unit
> +  // test framework and throws the C++ exception of type std::runtime_error with
> +  // a message that includes the filename, linenumber, and the expression that
> +  // triggered the ASSERT().
> +  //
> +  // EXPECT_THROW_MESSAGE() calls DecimalToBcd() expecting DecimalToBds() to
> +  // throw a C++ exception of type std::runtime_error with a message that
> +  // includes the expression of "Value < 100" that triggered the ASSERT().
> +  //
> +  EXPECT_THROW_MESSAGE (DecimalToBcd8 (101), "Value < 100");
>   }
>   
>   INSTANTIATE_TEST_SUITE_P (
> @@ -266,6 +287,11 @@ TEST (MacroTestsMessages, MacroTraceMessage) {
>     // Example of logging.
>     //
>     SCOPED_TRACE ("SCOPED_TRACE message\n");
> +
> +  //
> +  // Always pass
> +  //
> +  ASSERT_TRUE (TRUE);
>   }
>   
>   int


-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#115358): https://edk2.groups.io/g/devel/message/115358
Mute This Topic: https://groups.io/mt/104267261/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



  reply	other threads:[~2024-02-12 16:25 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-09 20:32 [edk2-devel] [edk2-stable202402][Patch V3 0/7] EDK II CI misses UnitTestFrameworkPkg failures Michael D Kinney
2024-02-09 20:32 ` [edk2-devel] [edk2-stable202402][Patch V3 1/7] MdePkg/Include: Rename _DEBUG() to address name collision Michael D Kinney
2024-02-13 11:57   ` Leif Lindholm
2024-02-09 20:32 ` [edk2-devel] [edk2-stable202402][Patch V3 2/7] UnitTestFrameworkPkg: MSFT CC_FLAGS add /MT to for host builds Michael D Kinney
2024-02-10  2:26   ` Michael Kubacki
2024-02-10  4:03     ` Michael D Kinney
2024-02-12 16:25   ` Michael Kubacki
2024-02-09 20:32 ` [edk2-devel] [edk2-stable202402][Patch V3 3/7] UnitTestFrameworkPkg: Expand host-based exception handling and gcov Michael D Kinney
2024-02-12 16:25   ` Michael Kubacki
2024-02-09 20:32 ` [edk2-devel] [edk2-stable202402][Patch V3 4/7] UnitTestFrameworkPkg/UnitTestLib: GetActiveFrameworkHandle() no ASSERT() Michael D Kinney
2024-02-12 16:25   ` Michael Kubacki
2024-02-09 20:32 ` [edk2-devel] [edk2-stable202402][Patch V3 5/7] UnitTestFrameworkPkg/UnitTestDebugAssertLib: Add GoogleTest support Michael D Kinney
2024-02-12 16:25   ` Michael Kubacki
2024-02-09 20:32 ` [edk2-devel] [edk2-stable202402][Patch V3 6/7] UnitTestFrameworkPkg/SampleGoogleTest: Use EXPECT_ANY_THROW() Michael D Kinney
2024-02-12 16:25   ` Michael Kubacki [this message]
2024-02-09 20:32 ` [edk2-devel] [edk2-stable202402][Patch V3 7/7] UnitTestFrameworkPkg: Add DSC and host tests that always fail Michael D Kinney
2024-02-12 16:26   ` Michael Kubacki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=df66284e-e431-4f04-83b9-11c1f20924f3@linux.microsoft.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox