]> git.proxmox.com Git - mirror_edk2.git/blobdiff - UnitTestFrameworkPkg/ReadMe.md
UnitTestFrameworkPkg: Add info to readme about working with UnitTests
[mirror_edk2.git] / UnitTestFrameworkPkg / ReadMe.md
index 7296f0a45c5ff94fcf009c429240796301384080..64386941cb4ba0e791cbe16cc734d71600249a88 100644 (file)
@@ -58,7 +58,7 @@ header for the `UnitTestLib` is located in `MdePkg`, so you shouldn't need to de
 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 'SampleUnitTestApp.inf'...\r
+See this example in 'SampleUnitTestUefiShell.inf'...\r
 \r
 ```\r
 [Packages]\r
@@ -72,6 +72,14 @@ See this example in 'SampleUnitTestApp.inf'...
   PrintLib\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      = SampleUnitTestUefiShell\r
+```\r
+\r
 ### 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
@@ -221,9 +229,11 @@ https://api.cmocka.org/
 \r
 ## Development\r
 \r
-When using the EDK2 Pytools for CI testing, the host-based unit tests will be built and run on any build that includes the `NOOPT` build target.\r
+When using the EDK2 Pytools for CI testing, the host-based unit tests will be built and run on any build that includes\r
+the `NOOPT` build target.\r
 \r
-If you are trying to iterate on a single test, a convenient pattern is to build only that test module. For example, the following command will build only the SafeIntLib host-based test from the MdePkg...\r
+If you are trying to iterate on a single test, a convenient pattern is to build only that test module. For example,\r
+the following command will build only the SafeIntLib host-based test from the MdePkg...\r
 \r
 ```bash\r
 stuart_ci_build -c .pytool/CISettings.py TOOL_CHAIN_TAG=VS2017 -p MdePkg -t NOOPT BUILDMODULE=MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLib.inf\r
@@ -250,8 +260,72 @@ reporting lib. This isn't currently possible with host-based. Only the assertion
 \r
 We will continue trying to make these as similar as possible.\r
 \r
+## Unit Test Location/Layout Rules\r
+\r
+Code/Test                                   | Location\r
+---------                                   | --------\r
+Host-Based Unit Tests for a Library/Protocol/PPI/GUID Interface   | If what's being tested is an interface (e.g. a library with a public header file, like DebugLib), the test should be scoped to the parent package.<br/>Example: `MdePkg/Test/UnitTest/[Library/Protocol/Ppi/Guid]/`<br/><br/>A real-world example of this is the BaseSafeIntLib test in MdePkg.<br/>`MdePkg/Test/UnitTest/Library/BaseSafeIntLib/TestBaseSafeIntLibHost.inf`\r
+Host-Based Unit Tests for a Library/Driver (PEI/DXE/SMM) implementation   | If what's being tested is a specific implementation (e.g. BaseDebugLibSerialPort for DebugLib), the test should be scoped to the implementation directory itself, in a UnitTest subdirectory.<br/><br/>Module Example: `MdeModulePkg/Universal/EsrtFmpDxe/UnitTest/`<br/>Library Example: `MdePkg/Library/BaseMemoryLib/UnitTest/`\r
+Host-Based Tests for a Functionality or Feature   | If you're writing a functional test that operates at the module level (i.e. if it's more than a single file or library), the test should be located in the package-level Tests directory under the HostFuncTest subdirectory.<br/>For example, if you were writing a test for the entire FMP Device Framework, you might put your test in:<br/>`FmpDevicePkg/Test/HostFuncTest/FmpDeviceFramework`<br/><br/>If the feature spans multiple packages, it's location should be determined by the package owners related to the feature.\r
+Non-Host-Based (PEI/DXE/SMM/Shell) Tests for a Functionality or Feature   | Similar to Host-Based, if the feature is in one package, should be located in the `*Pkg/Test/[Shell/Dxe/Smm/Pei]Test` directory.<br/><br/>If the feature spans multiple packages, it's location should be determined by the package owners related to the feature.<br/><br/>USAGE EXAMPLES<br/>PEI Example: MP_SERVICE_PPI. Or check MTRR configuration in a notification function.<br/> SMM Example: a test in a protocol callback function. (It is different with the solution that SmmAgent+ShellApp)<br/>DXE Example: a test in a UEFI event call back to check SPI/SMRAM status. <br/> Shell Example: the SMM handler audit test has a shell-based app that interacts with an SMM handler to get information. The SMM paging audit test gathers information about both DXE and SMM. And the SMM paging functional test actually forces errors into SMM via a DXE driver.\r
+\r
+### Example Directory Tree\r
+\r
+```text\r
+<PackageName>Pkg/\r
+  ComponentY/\r
+    ComponentY.inf\r
+    ComponentY.c\r
+    UnitTest/\r
+      ComponentYHostUnitTest.inf      # Host-Based Test for Driver Module\r
+      ComponentYUnitTest.c\r
+\r
+  Library/\r
+    GeneralPurposeLibBase/\r
+      ...\r
+\r
+    GeneralPurposeLibSerial/\r
+      ...\r
+\r
+    SpecificLibDxe/\r
+      SpecificLibDxe.c\r
+      SpecificLibDxe.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
+    UnitTest/\r
+      InterfaceX\r
+        InterfaceXHostUnitTest.inf    # Host-Based App (should be in Test/<Package>HostTest.dsc)\r
+        InterfaceXPeiUnitTest.inf     # PEIM Target-Based Test (if applicable)\r
+        InterfaceXDxeUnitTest.inf     # DXE Target-Based Test (if applicable)\r
+        InterfaceXSmmUnitTest.inf     # SMM Target-Based Test (if applicable)\r
+        InterfaceXShellUnitTest.inf   # Shell App Target-Based Test (if applicable)\r
+        InterfaceXUnitTest.c          # Test Logic\r
+\r
+      GeneralPurposeLib/              # Host-Based Test for any implementation of GeneralPurposeLib\r
+        GeneralPurposeLibTest.c\r
+        GeneralPurposeLibHostUnitTest.inf\r
+\r
+  <Package>Pkg.dsc          # Standard Modules and any Target-Based Test Apps (including in Test/)\r
+\r
+```\r
+\r
+### Future Locations in Consideration\r
+\r
+We don't know if these types will exist or be applicable yet, but if you write a support library or module that matches the following, please make sure they live in the correct place.\r
+\r
+Code/Test                                   | Location\r
+---------                                   | --------\r
+Host-Based Library Implementations                 | Host-Based Implementations of common libraries (eg. MemoryAllocationLibHost) should live in the same package that declares the library interface in its .DEC file in the `*Pkg/HostLibrary` directory. Should have 'Host' in the name.\r
+Host-Based Mocks and Stubs  | Mock and Stub libraries should live in the `UefiTestFrameworkPkg/StubLibrary` with either 'Mock' or 'Stub' in the library name.\r
+\r
+### If still in doubt...\r
+\r
+Hop on GitHub and ask @corthon, @mdkinney, or @spbrogan. ;)\r
+\r
 ## Copyright\r
 \r
 Copyright (c) Microsoft Corporation.\r
 SPDX-License-Identifier: BSD-2-Clause-Patent\r
-\r