From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 75DBB740035 for ; Mon, 12 Feb 2024 16:25:44 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=9peM+GALt9GMwDiyMow0Nqp6pyvcE49TqbMGpMsoIng=; c=relaxed/simple; d=groups.io; h=DKIM-Filter:Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References:From:In-Reply-To:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Language:Content-Type:Content-Transfer-Encoding; s=20140610; t=1707755143; v=1; b=BBL/d+QQz7i3WdjPlZZHv1HsIlgk+Z1/6wrUFGkz1Jm4F4zHmSRZvuyDAeyMg6LmEznnx7Ck s1EacekVX4S5V+7/j4xBVzGW/SX4LfV+Vpza6Jtl34fAqKcO6uo2UIwo+NqAdupp4kFIEmt9wo+ qe2ZdnzlasA7z8s2pum9JTjE= X-Received: by 127.0.0.2 with SMTP id iX8gYY7687511xYaWUzBaUTM; Mon, 12 Feb 2024 08:25:43 -0800 X-Received: from linux.microsoft.com (linux.microsoft.com [13.77.154.182]) by mx.groups.io with SMTP id smtpd.web10.10591.1707755142692953723 for ; Mon, 12 Feb 2024 08:25:42 -0800 X-Received: from [10.0.0.154] (unknown [20.39.63.12]) by linux.microsoft.com (Postfix) with ESMTPSA id 2015420B2000; Mon, 12 Feb 2024 08:25:42 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 2015420B2000 Message-ID: Date: Mon, 12 Feb 2024 11:25:42 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [edk2-devel] [edk2-stable202402][Patch V3 6/7] UnitTestFrameworkPkg/SampleGoogleTest: Use EXPECT_ANY_THROW() To: Michael D Kinney , devel@edk2.groups.io Cc: Sean Brogan References: <20240209203253.488-1-michael.d.kinney@intel.com> <20240209203253.488-7-michael.d.kinney@intel.com> From: "Michael Kubacki" In-Reply-To: <20240209203253.488-7-michael.d.kinney@intel.com> Precedence: Bulk List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,mikuback@linux.microsoft.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: D8uWPYWeBfWzjcBhY8Si1yVbx7686176AA= Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b="BBL/d+QQ"; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=linux.microsoft.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io Reviewed-by: Michael Kubacki 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. >=20 > 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. >=20 > Update library mappings so target-based unit tests use > UnitTestDebugAssertLib.inf and host-based unit tests use > UnitTestDebugAssertLibHost.inf >=20 > Cc: Michael Kubacki > Cc: Sean Brogan > Signed-off-by: Michael D Kinney > --- > UnitTestFrameworkPkg/ReadMe.md | 2 +- > .../SampleGoogleTest/SampleGoogleTest.cpp | 36 ++++++++++++++++--- > 2 files changed, 32 insertions(+), 6 deletions(-) >=20 > 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 t= ests 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/SampleG= oogleTest/SampleGoogleTest.cpp > index 94cbf2cf0b3c..2c2765e1e5ab 100644 > --- a/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/Sample= GoogleTest.cpp > +++ b/UnitTestFrameworkPkg/Test/GoogleTest/Sample/SampleGoogleTest/Sample= GoogleTest.cpp > @@ -7,7 +7,7 @@ > =20 > **/ > =20 > -#include > +#include > extern "C" { > #include > #include > @@ -229,7 +229,7 @@ TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpect= NoAssertFailure) { > } > =20 > /** > - Sample unit test using the ASSERT_DEATH() macro to test expected ASSER= T()s. > + Sample unit test using the EXPECT_ANY_THROW() macro to test expected A= SSERT()s. > **/ > TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpectAssertFailure) { > // > @@ -242,14 +242,35 @@ TEST_P (MacroTestsAssertsEnabledDisabled, MacroExpe= ctAssertFailure) { > // > // This test passes because it directly triggers an ASSERT(). > // > - ASSERT_DEATH (ASSERT (FALSE), ""); > + EXPECT_ANY_THROW (ASSERT (FALSE)); > =20 > // > // This test passes because DecimalToBcd() generates an ASSERT() if t= he > // value passed in is >=3D 100. The expected ASSERT() is caught by t= he 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 th= e > + // value passed in is >=3D 100. The expected ASSERT() is caught by th= e unit > + // test framework and throws the C++ exception of type std::runtime_er= ror. > + // EXPECT_THROW() returns without an error. > + // > + EXPECT_THROW (DecimalToBcd8 (101), std::runtime_error); > + > + // > + // This test passes because DecimalToBcd() generates an ASSERT() if th= e > + // value passed in is >=3D 100. The expected ASSERT() is caught by th= e unit > + // test framework and throws the C++ exception of type std::runtime_er= ror with > + // a message that includes the filename, linenumber, and the expressio= n that > + // triggered the ASSERT(). > + // > + // EXPECT_THROW_MESSAGE() calls DecimalToBcd() expecting DecimalToBds(= ) to > + // throw a C++ exception of type std::runtime_error with a message tha= t > + // includes the expression of "Value < 100" that triggered the ASSERT(= ). > + // > + EXPECT_THROW_MESSAGE (DecimalToBcd8 (101), "Value < 100"); > } > =20 > 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); > } > =20 > int -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- 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] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-