TianoCore EDK2 master
Loading...
Searching...
No Matches
UnitTestLib.h File Reference

Go to the source code of this file.

Macros

#define UNIT_TEST_PASSED   (0)
 
#define UNIT_TEST_ERROR_PREREQUISITE_NOT_MET   (1)
 
#define UNIT_TEST_ERROR_TEST_FAILED   (2)
 
#define UNIT_TEST_ERROR_CLEANUP_FAILED   (3)
 
#define UNIT_TEST_SKIPPED   (0xFFFFFFFD)
 
#define UNIT_TEST_RUNNING   (0xFFFFFFFE)
 
#define UNIT_TEST_PENDING   (0xFFFFFFFF)
 
#define UNIT_TEST_LOG_LEVEL_ERROR   BIT0
 
#define UNIT_TEST_LOG_LEVEL_WARN   BIT1
 
#define UNIT_TEST_LOG_LEVEL_INFO   BIT2
 
#define UNIT_TEST_LOG_LEVEL_VERBOSE   BIT3
 
#define UT_ASSERT_TRUE(Expression)
 
#define UT_ASSERT_FALSE(Expression)
 
#define UT_ASSERT_EQUAL(ValueA, ValueB)
 
#define UT_ASSERT_MEM_EQUAL(BufferA, BufferB, Length)
 
#define UT_ASSERT_NOT_EQUAL(ValueA, ValueB)
 
#define UT_ASSERT_NOT_EFI_ERROR(Status)
 
#define UT_ASSERT_STATUS_EQUAL(Status, Expected)
 
#define UT_ASSERT_NOT_NULL(Pointer)
 
#define UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status)   FunctionCall;
 
#define UT_LOG_ERROR(Format, ...)    UnitTestLog (UNIT_TEST_LOG_LEVEL_ERROR, Format, ##__VA_ARGS__)
 
#define UT_LOG_WARNING(Format, ...)    UnitTestLog (UNIT_TEST_LOG_LEVEL_WARN, Format, ##__VA_ARGS__)
 
#define UT_LOG_INFO(Format, ...)    UnitTestLog (UNIT_TEST_LOG_LEVEL_INFO, Format, ##__VA_ARGS__)
 
#define UT_LOG_VERBOSE(Format, ...)    UnitTestLog (UNIT_TEST_LOG_LEVEL_VERBOSE, Format, ##__VA_ARGS__)
 

Typedefs

typedef UINT32 UNIT_TEST_STATUS
 
typedef struct UNIT_TEST_FRAMEWORK_OBJECT * UNIT_TEST_FRAMEWORK_HANDLE
 
typedef struct UNIT_TEST_SUITE_OBJECT * UNIT_TEST_SUITE_HANDLE
 
typedef struct UNIT_TEST_OBJECT * UNIT_TEST_HANDLE
 
typedef VOID * UNIT_TEST_CONTEXT
 
typedef UNIT_TEST_STATUS(EFIAPI * UNIT_TEST_FUNCTION) (IN UNIT_TEST_CONTEXT Context)
 
typedef UNIT_TEST_STATUS(EFIAPI * UNIT_TEST_PREREQUISITE) (IN UNIT_TEST_CONTEXT Context)
 
typedef VOID(EFIAPI * UNIT_TEST_CLEANUP) (IN UNIT_TEST_CONTEXT Context)
 
typedef VOID(EFIAPI * UNIT_TEST_SUITE_SETUP) (VOID)
 
typedef VOID(EFIAPI * UNIT_TEST_SUITE_TEARDOWN) (VOID)
 

Functions

EFI_STATUS EFIAPI InitUnitTestFramework (OUT UNIT_TEST_FRAMEWORK_HANDLE *FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *ShortTitle, IN CHAR8 *VersionString)
 
EFI_STATUS EFIAPI CreateUnitTestSuite (OUT UNIT_TEST_SUITE_HANDLE *SuiteHandle, IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *Name, IN UNIT_TEST_SUITE_SETUP Setup OPTIONAL, IN UNIT_TEST_SUITE_TEARDOWN Teardown OPTIONAL)
 
EFI_STATUS EFIAPI AddTestCase (IN UNIT_TEST_SUITE_HANDLE SuiteHandle, IN CHAR8 *Description, IN CHAR8 *Name, IN UNIT_TEST_FUNCTION Function, IN UNIT_TEST_PREREQUISITE Prerequisite OPTIONAL, IN UNIT_TEST_CLEANUP CleanUp OPTIONAL, IN UNIT_TEST_CONTEXT Context OPTIONAL)
 
EFI_STATUS EFIAPI RunAllTestSuites (IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
 
EFI_STATUS EFIAPI FreeUnitTestFramework (IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
 
EFI_STATUS EFIAPI SaveFrameworkState (IN UNIT_TEST_CONTEXT ContextToSave OPTIONAL, IN UINTN ContextToSaveSize)
 
BOOLEAN EFIAPI UnitTestAssertTrue (IN BOOLEAN Expression, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
 
BOOLEAN EFIAPI UnitTestAssertFalse (IN BOOLEAN Expression, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
 
BOOLEAN EFIAPI UnitTestAssertNotEfiError (IN EFI_STATUS Status, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
 
BOOLEAN EFIAPI UnitTestAssertEqual (IN UINT64 ValueA, IN UINT64 ValueB, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *DescriptionA, IN CONST CHAR8 *DescriptionB)
 
BOOLEAN EFIAPI UnitTestAssertMemEqual (IN VOID *BufferA, IN VOID *BufferB, IN UINTN Length, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *DescriptionA, IN CONST CHAR8 *DescriptionB)
 
BOOLEAN EFIAPI UnitTestAssertNotEqual (IN UINT64 ValueA, IN UINT64 ValueB, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *DescriptionA, IN CONST CHAR8 *DescriptionB)
 
BOOLEAN EFIAPI UnitTestAssertStatusEqual (IN EFI_STATUS Status, IN EFI_STATUS Expected, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
 
BOOLEAN EFIAPI UnitTestAssertNotNull (IN VOID *Pointer, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *PointerName)
 
BOOLEAN EFIAPI UnitTestExpectAssertFailure (IN UNIT_TEST_STATUS UnitTestStatus, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *FunctionCall, OUT UNIT_TEST_STATUS *ResultStatus OPTIONAL)
 
VOID EFIAPI UnitTestLog (IN UINTN ErrorLevel, IN CONST CHAR8 *Format,...)
 

Detailed Description

Provides a unit test framework. This allows tests to focus on testing logic and the framework to focus on runnings, reporting, statistics, etc.

Copyright (c) Microsoft Corporation.
Copyright (c) 2020, Intel Corporation. All rights reserved.
SPDX-License-Identifier: BSD-2-Clause-Patent

Definition in file UnitTestLib.h.

Macro Definition Documentation

◆ UNIT_TEST_ERROR_CLEANUP_FAILED

#define UNIT_TEST_ERROR_CLEANUP_FAILED   (3)

Definition at line 20 of file UnitTestLib.h.

◆ UNIT_TEST_ERROR_PREREQUISITE_NOT_MET

#define UNIT_TEST_ERROR_PREREQUISITE_NOT_MET   (1)

Definition at line 18 of file UnitTestLib.h.

◆ UNIT_TEST_ERROR_TEST_FAILED

#define UNIT_TEST_ERROR_TEST_FAILED   (2)

Definition at line 19 of file UnitTestLib.h.

◆ UNIT_TEST_LOG_LEVEL_ERROR

#define UNIT_TEST_LOG_LEVEL_ERROR   BIT0

Declare PcdUnitTestLogLevel bits and UnitTestLog() ErrorLevel parameter.

Definition at line 28 of file UnitTestLib.h.

◆ UNIT_TEST_LOG_LEVEL_INFO

#define UNIT_TEST_LOG_LEVEL_INFO   BIT2

Definition at line 30 of file UnitTestLib.h.

◆ UNIT_TEST_LOG_LEVEL_VERBOSE

#define UNIT_TEST_LOG_LEVEL_VERBOSE   BIT3

Definition at line 31 of file UnitTestLib.h.

◆ UNIT_TEST_LOG_LEVEL_WARN

#define UNIT_TEST_LOG_LEVEL_WARN   BIT1

Definition at line 29 of file UnitTestLib.h.

◆ UNIT_TEST_PASSED

#define UNIT_TEST_PASSED   (0)

Definition at line 17 of file UnitTestLib.h.

◆ UNIT_TEST_PENDING

#define UNIT_TEST_PENDING   (0xFFFFFFFF)

Definition at line 23 of file UnitTestLib.h.

◆ UNIT_TEST_RUNNING

#define UNIT_TEST_RUNNING   (0xFFFFFFFE)

Definition at line 22 of file UnitTestLib.h.

◆ UNIT_TEST_SKIPPED

#define UNIT_TEST_SKIPPED   (0xFFFFFFFD)

Definition at line 21 of file UnitTestLib.h.

◆ UT_ASSERT_EQUAL

#define UT_ASSERT_EQUAL (   ValueA,
  ValueB 
)
Value:
if(!UnitTestAssertEqual ((UINT64)(ValueA), (UINT64)(ValueB), __func__, DEBUG_LINE_NUMBER, __FILE__, #ValueA, #ValueB)) { \
return UNIT_TEST_ERROR_TEST_FAILED; \
}
BOOLEAN EFIAPI UnitTestAssertEqual(IN UINT64 ValueA, IN UINT64 ValueB, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *DescriptionA, IN CONST CHAR8 *DescriptionB)
Definition: Assert.c:250

This macro uses the framework assertion logic to check whether two simple values are equal. If the values are equal, execution continues. Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.

Parameters
[in]ValueAValue to be compared for equality (64-bit comparison).
[in]ValueBValue to be compared for equality (64-bit comparison).

Definition at line 375 of file UnitTestLib.h.

◆ UT_ASSERT_FALSE

#define UT_ASSERT_FALSE (   Expression)
Value:
if(!UnitTestAssertFalse ((Expression), __func__, DEBUG_LINE_NUMBER, __FILE__, #Expression)) { \
return UNIT_TEST_ERROR_TEST_FAILED; \
}
BOOLEAN EFIAPI UnitTestAssertFalse(IN BOOLEAN Expression, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: Assert.c:151

This macro uses the framework assertion logic to check an expression for "FALSE". If the expression evaluates to FALSE, execution continues. Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.

Parameters
[in]ExpressionExpression to be evaluated for FALSE.

Definition at line 362 of file UnitTestLib.h.

◆ UT_ASSERT_MEM_EQUAL

#define UT_ASSERT_MEM_EQUAL (   BufferA,
  BufferB,
  Length 
)
Value:
if(!UnitTestAssertMemEqual ((VOID *)(UINTN)(BufferA), (VOID *)(UINTN)(BufferB), (UINTN)Length, __func__, DEBUG_LINE_NUMBER, __FILE__, #BufferA, #BufferB)) { \
return UNIT_TEST_ERROR_TEST_FAILED; \
}
UINT64 UINTN
BOOLEAN EFIAPI UnitTestAssertMemEqual(IN VOID *BufferA, IN VOID *BufferB, IN UINTN Length, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *DescriptionA, IN CONST CHAR8 *DescriptionB)
Definition: Assert.c:312

This macro uses the framework assertion logic to check whether two memory buffers are equal. If the buffers are equal, execution continues. Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.

Parameters
[in]BufferAPointer to a buffer for comparison.
[in]BufferBPointer to a buffer for comparison.
[in]LengthNumber of bytes to compare in BufferA and BufferB.

Definition at line 389 of file UnitTestLib.h.

◆ UT_ASSERT_NOT_EFI_ERROR

#define UT_ASSERT_NOT_EFI_ERROR (   Status)
Value:
if(!UnitTestAssertNotEfiError ((Status), __func__, DEBUG_LINE_NUMBER, __FILE__, #Status)) { \
return UNIT_TEST_ERROR_TEST_FAILED; \
}
BOOLEAN EFIAPI UnitTestAssertNotEfiError(IN EFI_STATUS Status, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: Assert.c:198

This macro uses the framework assertion logic to check whether an EFI_STATUS value is !EFI_ERROR(). If the status is !EFI_ERROR(), execution continues. Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.

Parameters
[in]StatusEFI_STATUS value to check.

Definition at line 414 of file UnitTestLib.h.

◆ UT_ASSERT_NOT_EQUAL

#define UT_ASSERT_NOT_EQUAL (   ValueA,
  ValueB 
)
Value:
if(!UnitTestAssertNotEqual ((UINT64)(ValueA), (UINT64)(ValueB), __func__, DEBUG_LINE_NUMBER, __FILE__, #ValueA, #ValueB)) { \
return UNIT_TEST_ERROR_TEST_FAILED; \
}
BOOLEAN EFIAPI UnitTestAssertNotEqual(IN UINT64 ValueA, IN UINT64 ValueB, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *DescriptionA, IN CONST CHAR8 *DescriptionB)
Definition: Assert.c:370

This macro uses the framework assertion logic to check whether two simple values are non-equal. If the values are non-equal, execution continues. Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.

Parameters
[in]ValueAValue to be compared for inequality (64-bit comparison).
[in]ValueBValue to be compared for inequality (64-bit comparison).

Definition at line 402 of file UnitTestLib.h.

◆ UT_ASSERT_NOT_NULL

#define UT_ASSERT_NOT_NULL (   Pointer)
Value:
if(!UnitTestAssertNotNull ((Pointer), __func__, DEBUG_LINE_NUMBER, __FILE__, #Pointer)) { \
return UNIT_TEST_ERROR_TEST_FAILED; \
}
BOOLEAN EFIAPI UnitTestAssertNotNull(IN VOID *Pointer, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *PointerName)
Definition: Assert.c:481

This macro uses the framework assertion logic to check whether a pointer is not NULL. If the pointer is not NULL, execution continues. Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.

Parameters
[in]PointerPointer to be checked against NULL.

Definition at line 439 of file UnitTestLib.h.

◆ UT_ASSERT_STATUS_EQUAL

#define UT_ASSERT_STATUS_EQUAL (   Status,
  Expected 
)
Value:
if(!UnitTestAssertStatusEqual ((Status), (Expected), __func__, DEBUG_LINE_NUMBER, __FILE__, #Status)) { \
return UNIT_TEST_ERROR_TEST_FAILED; \
}
BOOLEAN EFIAPI UnitTestAssertStatusEqual(IN EFI_STATUS Status, IN EFI_STATUS Expected, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: Assert.c:427

This macro uses the framework assertion logic to check whether two EFI_STATUS values are equal. If the values are equal, execution continues. Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.

Parameters
[in]StatusEFI_STATUS values to compare for equality.
[in]ExpectedEFI_STATUS values to compare for equality.

Definition at line 427 of file UnitTestLib.h.

◆ UT_ASSERT_TRUE

#define UT_ASSERT_TRUE (   Expression)
Value:
if(!UnitTestAssertTrue ((Expression), __func__, DEBUG_LINE_NUMBER, __FILE__, #Expression)) { \
return UNIT_TEST_ERROR_TEST_FAILED; \
}
BOOLEAN EFIAPI UnitTestAssertTrue(IN BOOLEAN Expression, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: Assert.c:104

This macro uses the framework assertion logic to check an expression for "TRUE". If the expression evaluates to TRUE, execution continues. Otherwise, the test case immediately returns UNIT_TEST_ERROR_TEST_FAILED.

Parameters
[in]ExpressionExpression to be evaluated for TRUE.

Definition at line 350 of file UnitTestLib.h.

◆ UT_EXPECT_ASSERT_FAILURE

#define UT_EXPECT_ASSERT_FAILURE (   FunctionCall,
  Status 
)    FunctionCall;

This macro uses the framework assertion logic to check whether a function call triggers an ASSERT() condition. The BaseLib SetJump()/LongJump() services are used to establish a safe return point when an ASSERT() is triggered. If an ASSERT() is triggered, unit test execution continues and Status is set to UNIT_TEST_PASSED. Otherwise, a unit test case failure is raised and Status is set to UNIT_TEST_ERROR_TEST_FAILED.

If ASSERT() macros are disabled, then the test case is skipped and a warning message is added to the unit test log. Status is set to UNIT_TEST_SKIPPED.

Parameters
[in]FunctionCallFunction call that is expected to trigger ASSERT().
[out]StatusPointer to a UNIT_TEST_STATUS return value. This is an optional parameter that may be NULL.

Definition at line 491 of file UnitTestLib.h.

◆ UT_LOG_ERROR

#define UT_LOG_ERROR (   Format,
  ... 
)     UnitTestLog (UNIT_TEST_LOG_LEVEL_ERROR, Format, ##__VA_ARGS__)

Test logging macro that records an ERROR message in the test framework log. Record is associated with the currently executing test case.

Parameters
[in]FormatFormatting string following the format defined in MdePkg/Include/Library/PrintLib.h.
[in]...Print args.

Definition at line 791 of file UnitTestLib.h.

◆ UT_LOG_INFO

#define UT_LOG_INFO (   Format,
  ... 
)     UnitTestLog (UNIT_TEST_LOG_LEVEL_INFO, Format, ##__VA_ARGS__)

Test logging macro that records an INFO message in the test framework log. Record is associated with the currently executing test case.

Parameters
[in]FormatFormatting string following the format defined in MdePkg/Include/Library/PrintLib.h.
[in]...Print args.

Definition at line 813 of file UnitTestLib.h.

◆ UT_LOG_VERBOSE

#define UT_LOG_VERBOSE (   Format,
  ... 
)     UnitTestLog (UNIT_TEST_LOG_LEVEL_VERBOSE, Format, ##__VA_ARGS__)

Test logging macro that records a VERBOSE message in the test framework log. Record is associated with the currently executing test case.

Parameters
[in]FormatFormatting string following the format defined in MdePkg/Include/Library/PrintLib.h.
[in]...Print args.

Definition at line 824 of file UnitTestLib.h.

◆ UT_LOG_WARNING

#define UT_LOG_WARNING (   Format,
  ... 
)     UnitTestLog (UNIT_TEST_LOG_LEVEL_WARN, Format, ##__VA_ARGS__)

Test logging macro that records a WARNING message in the test framework log. Record is associated with the currently executing test case.

Parameters
[in]FormatFormatting string following the format defined in MdePkg/Include/Library/PrintLib.h.
[in]...Print args.

Definition at line 802 of file UnitTestLib.h.

Typedef Documentation

◆ UNIT_TEST_CLEANUP

typedef VOID(EFIAPI * UNIT_TEST_CLEANUP) (IN UNIT_TEST_CONTEXT Context)

Unit-Test Cleanup (after) function pointer type.

Functions with this prototype are registered to be dispatched by the unit test framework after a given test case. This will be called even if the test case returns an error, but not if the prerequisite fails and the test is skipped. The purpose of this function is to clean up any global state or test data.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDTest case cleanup succeeded.
UNIT_TEST_ERROR_CLEANUP_FAILEDTest case cleanup failed.

Definition at line 129 of file UnitTestLib.h.

◆ UNIT_TEST_CONTEXT

typedef VOID* UNIT_TEST_CONTEXT

Unit Test Context

Definition at line 54 of file UnitTestLib.h.

◆ UNIT_TEST_FRAMEWORK_HANDLE

typedef struct UNIT_TEST_FRAMEWORK_OBJECT* UNIT_TEST_FRAMEWORK_HANDLE

Definition at line 37 of file UnitTestLib.h.

◆ UNIT_TEST_FUNCTION

typedef UNIT_TEST_STATUS(EFIAPI * UNIT_TEST_FUNCTION) (IN UNIT_TEST_CONTEXT Context)

The prototype for a single UnitTest case function.

Functions with this prototype are registered to be dispatched by the UnitTest framework, and results are recorded as test Pass or Fail.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDThe Unit test has completed and the test case was successful.
UNIT_TEST_ERROR_TEST_FAILEDA test case assertion has failed.

Definition at line 76 of file UnitTestLib.h.

◆ UNIT_TEST_HANDLE

typedef struct UNIT_TEST_OBJECT* UNIT_TEST_HANDLE

Definition at line 49 of file UnitTestLib.h.

◆ UNIT_TEST_PREREQUISITE

typedef UNIT_TEST_STATUS(EFIAPI * UNIT_TEST_PREREQUISITE) (IN UNIT_TEST_CONTEXT Context)

Unit-Test Prerequisite Function pointer type.

Functions with this prototype are registered to be dispatched by the unit test framework prior to a given test case. If this prereq function returns UNIT_TEST_ERROR_PREREQUISITE_NOT_MET, the test case will be skipped.

Parameters
[in]Context[Optional] An optional parameter that enables: 1) test-case reuse with varied parameters and 2) test-case re-entry for Target tests that need a reboot. This parameter is a VOID* and it is the responsibility of the test author to ensure that the contents are well understood by all test cases that may consume it.
Return values
UNIT_TEST_PASSEDUnit test case prerequisites are met.
UNIT_TEST_ERROR_PREREQUISITE_NOT_METTest case should be skipped.

Definition at line 102 of file UnitTestLib.h.

◆ UNIT_TEST_STATUS

typedef UINT32 UNIT_TEST_STATUS

Unit Test Status

Definition at line 16 of file UnitTestLib.h.

◆ UNIT_TEST_SUITE_HANDLE

typedef struct UNIT_TEST_SUITE_OBJECT* UNIT_TEST_SUITE_HANDLE

Definition at line 43 of file UnitTestLib.h.

◆ UNIT_TEST_SUITE_SETUP

typedef VOID(EFIAPI * UNIT_TEST_SUITE_SETUP) (VOID)

Unit-Test Test Suite Setup (before) function pointer type. Functions with this prototype are registered to be dispatched by the UnitTest framework prior to running any of the test cases in a test suite. It will only be run once at the beginning of the suite (not prior to each case).

The purpose of this function is to set up any global state or test data.

Definition at line 143 of file UnitTestLib.h.

◆ UNIT_TEST_SUITE_TEARDOWN

typedef VOID(EFIAPI * UNIT_TEST_SUITE_TEARDOWN) (VOID)

Unit-Test Test Suite Teardown (after) function pointer type. Functions with this prototype are registered to be dispatched by the UnitTest framework after running all of the test cases in a test suite. It will only be run once at the end of the suite.

The purpose of this function is to clean up any global state or test data.

Definition at line 157 of file UnitTestLib.h.

Function Documentation

◆ AddTestCase()

EFI_STATUS EFIAPI AddTestCase ( IN UNIT_TEST_SUITE_HANDLE  SuiteHandle,
IN CHAR8 *  Description,
IN CHAR8 *  Name,
IN UNIT_TEST_FUNCTION  Function,
IN UNIT_TEST_PREREQUISITE Prerequisite  OPTIONAL,
IN UNIT_TEST_CLEANUP CleanUp  OPTIONAL,
IN UNIT_TEST_CONTEXT Context  OPTIONAL 
)

Adds test case to Suite

Parameters
[in]SuiteHandleUnit test suite to add test to.
[in]DescriptionNull-terminated ASCII string that is the user friendly description of a test. String is copied.
[in]NameNull-terminated ASCII string that is the short name of the test with no spaces. String is copied.
[in]FunctionUnit test function.
[in]PrerequisitePrerequisite function, runs before test. This is an optional parameter that may be NULL.
[in]CleanUpClean up function, runs after test. This is an optional parameter that may be NULL.
[in]ContextPointer to context. This is an optional parameter that may be NULL.
Return values
EFI_SUCCESSThe unit test case was added to Suite.
EFI_INVALID_PARAMETERSuiteHandle is NULL.
EFI_INVALID_PARAMETERDescription is NULL.
EFI_INVALID_PARAMETERName is NULL.
EFI_INVALID_PARAMETERFunction is NULL.
EFI_OUT_OF_RESOURCESThere are not enough resources available to add the unit test case to Suite.

Definition at line 426 of file UnitTestLib.c.

◆ CreateUnitTestSuite()

EFI_STATUS EFIAPI CreateUnitTestSuite ( OUT UNIT_TEST_SUITE_HANDLE *  SuiteHandle,
IN UNIT_TEST_FRAMEWORK_HANDLE  FrameworkHandle,
IN CHAR8 *  Title,
IN CHAR8 *  Name,
IN UNIT_TEST_SUITE_SETUP Setup  OPTIONAL,
IN UNIT_TEST_SUITE_TEARDOWN Teardown  OPTIONAL 
)

Registers a Unit Test Suite in the Unit Test Framework. At least one test suite must be registered, because all test cases must be within a unit test suite.

Parameters
[out]SuiteHandleUnit test suite to create
[in]FrameworkHandleUnit test framework to add unit test suite to
[in]TitleNull-terminated ASCII string that is the user friendly name of the test suite. String is copied.
[in]NameNull-terminated ASCII string that is the short name of the test suite with no spaces. String is copied.
[in]SetupSetup function, runs before suite. This is an optional parameter that may be NULL.
[in]TeardownTeardown function, runs after suite. This is an optional parameter that may be NULL.
Return values
EFI_SUCCESSThe unit test suite was created.
EFI_INVALID_PARAMETERSuiteHandle is NULL.
EFI_INVALID_PARAMETERFrameworkHandle is NULL.
EFI_INVALID_PARAMETERTitle is NULL.
EFI_INVALID_PARAMETERName is NULL.
EFI_OUT_OF_RESOURCESThere are not enough resources available to initialize the unit test suite.

Definition at line 326 of file UnitTestLib.c.

◆ FreeUnitTestFramework()

EFI_STATUS EFIAPI FreeUnitTestFramework ( IN UNIT_TEST_FRAMEWORK_HANDLE  FrameworkHandle)

Cleanup a test framework.

After tests are run, this will teardown the entire framework and free all allocated data within.

Parameters
[in]FrameworkHandleA handle to the current running framework that dispatched the test. Necessary for recording certain test events with the framework.
Return values
EFI_SUCCESSAll resources associated with framework were freed.
EFI_INVALID_PARAMETERFrameworkHandle is NULL.

Definition at line 150 of file UnitTestLib.c.

◆ InitUnitTestFramework()

EFI_STATUS EFIAPI InitUnitTestFramework ( OUT UNIT_TEST_FRAMEWORK_HANDLE *  FrameworkHandle,
IN CHAR8 *  Title,
IN CHAR8 *  ShortTitle,
IN CHAR8 *  VersionString 
)

Method to Initialize the Unit Test framework. This function registers the test name and also initializes the internal state of the test framework to receive any new suites and tests.

Parameters
[out]FrameworkHandleUnit test framework to be created.
[in]TitleNull-terminated ASCII string that is the user friendly name of the framework. String is copied.
[in]ShortTitleNull-terminated ASCII short string that is the short name of the framework with no spaces. String is copied.
[in]VersionStringNull-terminated ASCII version string for the framework. String is copied.
Return values
EFI_SUCCESSThe unit test framework was initialized.
EFI_INVALID_PARAMETERFrameworkHandle is NULL.
EFI_INVALID_PARAMETERTitle is NULL.
EFI_INVALID_PARAMETERShortTitle is NULL.
EFI_INVALID_PARAMETERVersionString is NULL.
EFI_INVALID_PARAMETERShortTitle is invalid.
EFI_OUT_OF_RESOURCESThere are not enough resources available to initialize the unit test framework.

Definition at line 204 of file UnitTestLib.c.

◆ RunAllTestSuites()

EFI_STATUS EFIAPI RunAllTestSuites ( IN UNIT_TEST_FRAMEWORK_HANDLE  FrameworkHandle)

Execute all unit test cases in all unit test suites added to a Framework.

Once a unit test framework is initialized and all unit test suites and unit test cases are registered, this function will cause the unit test framework to dispatch all unit test cases in sequence and record the results for reporting.

Parameters
[in]FrameworkHandleA handle to the current running framework that dispatched the test. Necessary for recording certain test events with the framework.
Return values
EFI_SUCCESSAll test cases were dispatched.
EFI_INVALID_PARAMETERFrameworkHandle is NULL.

Definition at line 145 of file RunTests.c.

◆ SaveFrameworkState()

EFI_STATUS EFIAPI SaveFrameworkState ( IN UNIT_TEST_CONTEXT ContextToSave  OPTIONAL,
IN UINTN  ContextToSaveSize 
)

Leverages a framework-specific mechanism (see UnitTestPersistenceLib if you're a framework author) to save the state of the executing framework along with any allocated data so that the test may be resumed upon reentry. A test case should pass any needed context (which, to prevent an infinite loop, should be at least the current execution count) which will be saved by the framework and passed to the test case upon resume.

This should be called while the current test framework is valid and active. It is generally called from within a test case prior to quitting or rebooting.

Parameters
[in]ContextToSaveA buffer of test case-specific data to be saved along with framework state. Will be passed as "Context" to the test case upon resume. This is an optional parameter that may be NULL.
[in]ContextToSaveSizeSize of the ContextToSave buffer.
Return values
EFI_SUCCESSThe framework state and context were saved.
EFI_NOT_FOUNDAn active framework handle was not found.
EFI_INVALID_PARAMETERContextToSave is not NULL and ContextToSaveSize is 0.
EFI_INVALID_PARAMETERContextToSave is >= 4GB.
EFI_OUT_OF_RESOURCESThere are not enough resources available to save the framework and context state.
EFI_DEVICE_ERRORThe framework and context state could not be saved to a persistent storage device due to a device error.

Definition at line 818 of file UnitTestLib.c.

◆ UnitTestAssertEqual()

BOOLEAN EFIAPI UnitTestAssertEqual ( IN UINT64  ValueA,
IN UINT64  ValueB,
IN CONST CHAR8 *  FunctionName,
IN UINTN  LineNumber,
IN CONST CHAR8 *  FileName,
IN CONST CHAR8 *  DescriptionA,
IN CONST CHAR8 *  DescriptionB 
)

If ValueA is equal ValueB, then TRUE is returned. If ValueA is not equal to ValueB, then an assert is triggered and the location of the assert provided by FunctionName, LineNumber, FileName, DescriptionA, and DescriptionB are recorded and FALSE is returned.

Parameters
[in]ValueA64-bit value.
[in]ValueB64-bit value.
[in]FunctionNameNull-terminated ASCII string of the function executing the assert macro.
[in]LineNumberThe source file line number of the assert macro.
[in]FileNameNull-terminated ASCII string of the filename executing the assert macro.
[in]DescriptionANull-terminated ASCII string that is a description of ValueA.
[in]DescriptionBNull-terminated ASCII string that is a description of ValueB.
Return values
TRUEValueA is equal to ValueB.
FALSEValueA is not equal to ValueB.

Definition at line 250 of file Assert.c.

◆ UnitTestAssertFalse()

BOOLEAN EFIAPI UnitTestAssertFalse ( IN BOOLEAN  Expression,
IN CONST CHAR8 *  FunctionName,
IN UINTN  LineNumber,
IN CONST CHAR8 *  FileName,
IN CONST CHAR8 *  Description 
)

If Expression is FALSE, then TRUE is returned. If Expression is TRUE, then an assert is triggered and the location of the assert provided by FunctionName, LineNumber, FileName, and Description are recorded and FALSE is returned.

Parameters
[in]ExpressionThe BOOLEAN result of the expression evaluation.
[in]FunctionNameNull-terminated ASCII string of the function executing the assert macro.
[in]LineNumberThe source file line number of the assert macro.
[in]FileNameNull-terminated ASCII string of the filename executing the assert macro.
[in]DescriptionNull-terminated ASCII string of the expression being evaluated.
Return values
TRUEExpression is FALSE.
FALSEExpression is TRUE.

Definition at line 151 of file Assert.c.

◆ UnitTestAssertMemEqual()

BOOLEAN EFIAPI UnitTestAssertMemEqual ( IN VOID *  BufferA,
IN VOID *  BufferB,
IN UINTN  Length,
IN CONST CHAR8 *  FunctionName,
IN UINTN  LineNumber,
IN CONST CHAR8 *  FileName,
IN CONST CHAR8 *  DescriptionA,
IN CONST CHAR8 *  DescriptionB 
)

If the contents of BufferA are identical to the contents of BufferB, then TRUE is returned. If the contents of BufferA are not identical to the contents of BufferB, then an assert is triggered and the location of the assert provided by FunctionName, LineNumber, FileName, DescriptionA, and DescriptionB are recorded and FALSE is returned.

Parameters
[in]BufferAPointer to a buffer for comparison.
[in]BufferBPointer to a buffer for comparison.
[in]LengthNumber of bytes to compare in BufferA and BufferB.
[in]FunctionNameNull-terminated ASCII string of the function executing the assert macro.
[in]LineNumberThe source file line number of the assert macro.
[in]FileNameNull-terminated ASCII string of the filename executing the assert macro.
[in]DescriptionANull-terminated ASCII string that is a description of BufferA.
[in]DescriptionBNull-terminated ASCII string that is a description of BufferB.
Return values
TRUEThe contents of BufferA are identical to the contents of BufferB.
FALSEThe contents of BufferA are not identical to the contents of BufferB.

Definition at line 312 of file Assert.c.

◆ UnitTestAssertNotEfiError()

BOOLEAN EFIAPI UnitTestAssertNotEfiError ( IN EFI_STATUS  Status,
IN CONST CHAR8 *  FunctionName,
IN UINTN  LineNumber,
IN CONST CHAR8 *  FileName,
IN CONST CHAR8 *  Description 
)

If Status is not an EFI_ERROR(), then TRUE is returned. If Status is an EFI_ERROR(), then an assert is triggered and the location of the assert provided by FunctionName, LineNumber, FileName, and Description are recorded and FALSE is returned.

Parameters
[in]StatusThe EFI_STATUS value to evaluate.
[in]FunctionNameNull-terminated ASCII string of the function executing the assert macro.
[in]LineNumberThe source file line number of the assert macro.
[in]FileNameNull-terminated ASCII string of the filename executing the assert macro.
[in]DescriptionNull-terminated ASCII string of the status expression being evaluated.
Return values
TRUEStatus is not an EFI_ERROR().
FALSEStatus is an EFI_ERROR().

Definition at line 198 of file Assert.c.

◆ UnitTestAssertNotEqual()

BOOLEAN EFIAPI UnitTestAssertNotEqual ( IN UINT64  ValueA,
IN UINT64  ValueB,
IN CONST CHAR8 *  FunctionName,
IN UINTN  LineNumber,
IN CONST CHAR8 *  FileName,
IN CONST CHAR8 *  DescriptionA,
IN CONST CHAR8 *  DescriptionB 
)

If ValueA is not equal ValueB, then TRUE is returned. If ValueA is equal to ValueB, then an assert is triggered and the location of the assert provided by FunctionName, LineNumber, FileName, DescriptionA and DescriptionB are recorded and FALSE is returned.

Parameters
[in]ValueA64-bit value.
[in]ValueB64-bit value.
[in]FunctionNameNull-terminated ASCII string of the function executing the assert macro.
[in]LineNumberThe source file line number of the assert macro.
[in]FileNameNull-terminated ASCII string of the filename executing the assert macro.
[in]DescriptionANull-terminated ASCII string that is a description of ValueA.
[in]DescriptionBNull-terminated ASCII string that is a description of ValueB.
Return values
TRUEValueA is not equal to ValueB.
FALSEValueA is equal to ValueB.

Definition at line 370 of file Assert.c.

◆ UnitTestAssertNotNull()

BOOLEAN EFIAPI UnitTestAssertNotNull ( IN VOID *  Pointer,
IN CONST CHAR8 *  FunctionName,
IN UINTN  LineNumber,
IN CONST CHAR8 *  FileName,
IN CONST CHAR8 *  PointerName 
)

If Pointer is not equal to NULL, then TRUE is returned. If Pointer is equal to NULL, then an assert is triggered and the location of the assert provided by FunctionName, LineNumber, FileName, and PointerName are recorded and FALSE is returned.

Parameters
[in]PointerPointer value to be checked against NULL.
[in]ExpectedThe expected EFI_STATUS return value from a function under test.
[in]FunctionNameNull-terminated ASCII string of the function executing the assert macro.
[in]LineNumberThe source file line number of the assert macro.
[in]FileNameNull-terminated ASCII string of the filename executing the assert macro.
[in]PointerNameNull-terminated ASCII string that is a description of Pointer.
Return values
TRUEPointer is not equal to NULL.
FALSEPointer is equal to NULL.

Definition at line 481 of file Assert.c.

◆ UnitTestAssertStatusEqual()

BOOLEAN EFIAPI UnitTestAssertStatusEqual ( IN EFI_STATUS  Status,
IN EFI_STATUS  Expected,
IN CONST CHAR8 *  FunctionName,
IN UINTN  LineNumber,
IN CONST CHAR8 *  FileName,
IN CONST CHAR8 *  Description 
)

If Status is equal to Expected, then TRUE is returned. If Status is not equal to Expected, then an assert is triggered and the location of the assert provided by FunctionName, LineNumber, FileName, and Description are recorded and FALSE is returned.

Parameters
[in]StatusEFI_STATUS value returned from an API under test.
[in]ExpectedThe expected EFI_STATUS return value from an API under test.
[in]FunctionNameNull-terminated ASCII string of the function executing the assert macro.
[in]LineNumberThe source file line number of the assert macro.
[in]FileNameNull-terminated ASCII string of the filename executing the assert macro.
[in]DescriptionNull-terminated ASCII string that is a description of Status.
Return values
TRUEStatus is equal to Expected.
FALSEStatus is not equal to Expected.

Definition at line 427 of file Assert.c.

◆ UnitTestAssertTrue()

BOOLEAN EFIAPI UnitTestAssertTrue ( IN BOOLEAN  Expression,
IN CONST CHAR8 *  FunctionName,
IN UINTN  LineNumber,
IN CONST CHAR8 *  FileName,
IN CONST CHAR8 *  Description 
)

If Expression is TRUE, then TRUE is returned. If Expression is FALSE, then an assert is triggered and the location of the assert provided by FunctionName, LineNumber, FileName, and Description are recorded and FALSE is returned.

Parameters
[in]ExpressionThe BOOLEAN result of the expression evaluation.
[in]FunctionNameNull-terminated ASCII string of the function executing the assert macro.
[in]LineNumberThe source file line number of the assert macro.
[in]FileNameNull-terminated ASCII string of the filename executing the assert macro.
[in]DescriptionNull-terminated ASCII string of the expression being evaluated.
Return values
TRUEExpression is TRUE.
FALSEExpression is FALSE.

Definition at line 104 of file Assert.c.

◆ UnitTestExpectAssertFailure()

BOOLEAN EFIAPI UnitTestExpectAssertFailure ( IN UNIT_TEST_STATUS  UnitTestStatus,
IN CONST CHAR8 *  FunctionName,
IN UINTN  LineNumber,
IN CONST CHAR8 *  FileName,
IN CONST CHAR8 *  FunctionCall,
OUT UNIT_TEST_STATUS *ResultStatus  OPTIONAL 
)

If UnitTestStatus is UNIT_TEST_PASSED, then log an info message and return TRUE because an ASSERT() was expected when FunctionCall was executed and an ASSERT() was triggered. If UnitTestStatus is UNIT_TEST_SKIPPED, then log a warning message and return TRUE because ASSERT() macros are disabled. If UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED, then log an error message and return FALSE because an ASSERT() was expected when FunctionCall was executed, but no ASSERT() conditions were triggered. The log messages contain FunctionName, LineNumber, and FileName strings to provide the location of the UT_EXPECT_ASSERT_FAILURE() macro.

Parameters
[in]UnitTestStatusThe status from UT_EXPECT_ASSERT_FAILURE() that is either pass, skipped, or failed.
[in]FunctionNameNull-terminated ASCII string of the function executing the UT_EXPECT_ASSERT_FAILURE() macro.
[in]LineNumberThe source file line number of the the function executing the UT_EXPECT_ASSERT_FAILURE() macro.
[in]FileNameNull-terminated ASCII string of the filename executing the UT_EXPECT_ASSERT_FAILURE() macro.
[in]FunctionCallNull-terminated ASCII string of the function call executed by the UT_EXPECT_ASSERT_FAILURE() macro.
[out]ResultStatusUsed to return the UnitTestStatus value to the caller of UT_EXPECT_ASSERT_FAILURE(). This is optional parameter that may be NULL.
Return values
TRUEUnitTestStatus is UNIT_TEST_PASSED.
TRUEUnitTestStatus is UNIT_TEST_SKIPPED.
FALSEUnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED.

Definition at line 539 of file Assert.c.

◆ UnitTestLog()

VOID EFIAPI UnitTestLog ( IN UINTN  ErrorLevel,
IN CONST CHAR8 *  Format,
  ... 
)

Test logging function that records a messages in the test framework log. Record is associated with the currently executing test case.

Parameters
[in]ErrorLevelThe error level of the unit test log message.
[in]FormatFormatting string following the format defined in the MdePkg/Include/Library/PrintLib.h.
[in]...Print args.

Definition at line 155 of file Log.c.