1.. _module-pw_unit_test: 2 3============ 4pw_unit_test 5============ 6.. pigweed-module:: 7 :name: pw_unit_test 8 9.. tab-set:: 10 11 .. tab-item:: mylib_test.cpp 12 13 .. code-block:: c++ 14 15 #include "mylib.h" 16 17 #include "pw_unit_test/framework.h" 18 19 namespace { 20 21 TEST(MyTestSuite, MyTestCase) { 22 pw::InlineString<32> expected = "(╯°□°)╯︵ ┻━┻"; 23 pw::InlineString<32> actual = mylib::flip_table(); 24 EXPECT_STREQ(expected.c_str(), actual.c_str()); 25 } 26 27 } 28 29 .. tab-item:: BUILD.bazel 30 31 .. code-block:: python 32 33 load("@pigweed//pw_build:pigweed.bzl", "pw_cc_test") 34 35 cc_library( 36 name = "mylib", 37 srcs = ["mylib.cc"], 38 hdrs = ["mylib.h"], 39 includes = ["."], 40 deps = ["@pigweed//pw_string"], 41 ) 42 43 pw_cc_test( 44 name = "mylib_test", 45 srcs = ["mylib_test.cc"], 46 deps = [ 47 "@pigweed//pw_unit_test", 48 ":mylib", 49 ], 50 ) 51 52 .. tab-item:: mylib.cc 53 54 .. code-block:: c++ 55 56 #include "mylib.h" 57 58 #include "pw_string/string.h" 59 60 namespace mylib { 61 62 pw::InlineString<32> flip_table() { 63 pw::InlineString<32> textmoji = "(╯°□°)╯︵ ┻━┻"; 64 return textmoji; 65 } 66 67 } 68 69 .. tab-item:: mylib.h 70 71 .. code-block:: c++ 72 73 #include "pw_string/string.h" 74 75 namespace mylib { 76 77 pw::InlineString<32> flip_table(); 78 79 } 80 81.. _GoogleTest: https://google.github.io/googletest/ 82 83``pw_unit_test`` provides a `GoogleTest`_-compatible unit testing framework for 84Pigweed-based projects. The default backend is a lightweight subset of 85GoogleTest that uses embedded-friendly primitives. 86 87.. grid:: 1 88 89 .. grid-item-card:: :octicon:`rocket` Quickstart 90 :link: module-pw_unit_test-quickstart 91 :link-type: ref 92 :class-item: sales-pitch-cta-primary 93 94 Set up your project for testing and learn testing basics. 95 96.. grid:: 2 97 98 .. grid-item-card:: :octicon:`list-unordered` Guides 99 :link: module-pw_unit_test-guides 100 :link-type: ref 101 :class-item: sales-pitch-cta-secondary 102 103 Learn how to do common tasks. 104 105 .. grid-item-card:: :octicon:`code-square` C++ API reference 106 :link: module-pw_unit_test-cpp 107 :link-type: ref 108 :class-item: sales-pitch-cta-secondary 109 110 Get detailed C++ API reference information. 111 112.. grid:: 2 113 114 .. grid-item-card:: :octicon:`code-square` Bazel API reference 115 :link: module-pw_unit_test-bazel 116 :link-type: ref 117 :class-item: sales-pitch-cta-secondary 118 119 Get detailed Bazel API reference information. 120 121 .. grid-item-card:: :octicon:`code-square` GN API reference 122 :link: module-pw_unit_test-gn 123 :link-type: ref 124 :class-item: sales-pitch-cta-secondary 125 126 Get detailed GN API reference information. 127 128.. grid:: 2 129 130 .. grid-item-card:: :octicon:`code-square` CMake API reference 131 :link: module-pw_unit_test-cmake 132 :link-type: ref 133 :class-item: sales-pitch-cta-secondary 134 135 Get detailed CMake API reference information. 136 137 .. grid-item-card:: :octicon:`code-square` Python API reference 138 :link: module-pw_unit_test-py 139 :link-type: ref 140 :class-item: sales-pitch-cta-secondary 141 142 Get detailed Python API reference information. 143 144.. _module-pw_unit_test-quickstart: 145 146---------- 147Quickstart 148---------- 149 150Set up your build system 151======================== 152.. tab-set:: 153 154 .. tab-item:: Bazel 155 156 Load the :ref:`module-pw_unit_test-pw_cc_test` rule and create a target 157 that depends on ``@pigweed//pw_unit_test`` as well as the code you want 158 to test: 159 160 .. code-block:: python 161 162 load("@pigweed//pw_build:pigweed.bzl", "pw_cc_test") 163 164 cc_library( 165 name = "mylib", 166 srcs = ["mylib.cc"], 167 hdrs = ["mylib.h"], 168 includes = ["."], 169 deps = ["..."], 170 ) 171 172 pw_cc_test( 173 name = "mylib_test", 174 srcs = ["mylib_test.cc"], 175 deps = [ 176 "@pigweed//pw_unit_test", 177 ":mylib", 178 ], 179 ) 180 181 This assumes that your Bazel ``WORKSPACE`` has a `repository 182 <https://bazel.build/concepts/build-ref#repositories>`_ named ``@pigweed`` 183 that points to the upstream Pigweed repository. 184 185 See also :ref:`module-pw_unit_test-bazel`. 186 187 .. tab-item:: GN 188 189 Import ``$dir_pw_unit_test/test.gni`` and create a ``pw_test`` rule that 190 depends on the code you want to test: 191 192 .. code-block:: python 193 194 import("$dir_pw_unit_test/test.gni") 195 196 pw_source_set("mylib") { 197 sources = [ "mylib.cc" ] 198 } 199 200 pw_test("mylib_test") { 201 sources = [ "mylib_test.cc" ] 202 deps = [ ":mylib" ] 203 } 204 205 See :ref:`module-pw_unit_test-gn` for more information. 206 207``pw_unit_test`` generates a simple ``main`` function for running tests on 208:ref:`target-host`. See :ref:`module-pw_unit_test-main` to learn how to 209create your own ``main`` function for running on-device tests. 210 211Write tests 212=========== 213Create test suites and test cases: 214 215.. code-block:: c++ 216 217 #include "mylib.h" 218 219 #include "pw_unit_test/framework.h" 220 221 namespace { 222 223 TEST(MyTestSuite, MyTestCase) { 224 pw::InlineString<32> expected = "(╯°□°)╯︵ ┻━┻"; 225 pw::InlineString<32> actual = app::flip_table(); 226 EXPECT_STREQ(expected.c_str(), actual.c_str()); 227 } 228 229 } 230 231``pw_unit_test`` provides a standard set of ``TEST``, ``EXPECT``, ``ASSERT`` 232and ``FAIL`` macros. The default backend, ``pw_unit_test:light``, offers an 233embedded-friendly implementation which prioritizes small code size. 234 235Alternativley, users can opt into a larger set of assertion macros, matchers, 236and more detailed error messages by using the ``pw_unit_test:googletest`` 237backend. See :ref:`module-pw_unit_test-backends`. 238 239See `GoogleTest Primer <https://google.github.io/googletest/primer.html>`_ for 240the basics of using GoogleTest. 241 242Run tests 243========= 244.. tab-set:: 245 246 .. tab-item:: Bazel 247 248 .. code-block:: console 249 250 $ bazel test //src:mylib_test 251 252 .. tab-item:: GN 253 254 Run the generated test binary: 255 256 .. code-block:: console 257 258 $ ./out/.../mylib_test 259 260.. _module-pw_unit_test-guides: 261 262------ 263Guides 264------ 265 266.. _module-pw_unit_test-backends: 267 268Choose a backend 269================ 270The default backend, ``pw_unit_test:light``, is a lightweight subset of 271GoogleTest that uses embedded-friendly primitives. It's also highly portable 272because it offloads the responsibility of test reporting and output to the 273underlying system, communicating its results through a common interface. This 274lets you write unit tests once and run them under many different environments. 275 276If the :ref:`subset <module-pw_unit_test-compatibility>` of GoogleTest that 277``pw_unit_test:light`` supports doesn't meet your needs, you can access the 278full upstream GoogleTest API through ``pw_unit_test:googletest``. See 279:ref:`module-pw_unit_test-upstream`. 280 281.. _module-pw_unit_test-main: 282 283Create a custom ``main`` function 284================================= 285For simple unit tests that run on :ref:`target-host` the workflow outlined in 286:ref:`module-pw_unit_test-quickstart` is all you need. Pigweed's build templates 287generate a simple ``main`` function to run the tests with. 288 289To do more complex testing, such as on-device testing: 290 2911. Create your own ``main`` function: 292 293 .. code-block:: c++ 294 295 #include "pw_unit_test/framework.h" 296 // pw_unit_test:light requires an event handler to be configured. 297 #include "pw_unit_test/simple_printing_event_handler.h" 298 299 void WriteString(std::string_view string, bool newline) { 300 printf("%s", string.data()); 301 if (newline) { 302 printf("\n"); 303 } 304 } 305 306 int main() { 307 // Make the binary compatible with pw_unit_test:googletest. Has no effect 308 // when using pw_unit_test:light. 309 testing::InitGoogleTest(); 310 // Set up the event handler for pw_unit_test:light. 311 pw::unit_test::SimplePrintingEventHandler handler(WriteString); 312 pw::unit_test::RegisterEventHandler(&handler); 313 return RUN_ALL_TESTS(); 314 } 315 316 See :ref:`module-pw_unit_test-event-handlers` for more information about 317 handling events. 318 3192. Set the build argument that instructs your build system to use your custom 320 ``main`` function: 321 322 * Bazel: :option:`pw_unit_test_main` 323 * GN: :option:`pw_unit_test_MAIN` 324 325.. _module-pw_unit_test-event-handlers: 326 327Create event handlers 328===================== 329.. _//pw_unit_test/public/pw_unit_test/event_handler.h: https://cs.opensource.google/pigweed/pigweed/+/main:pw_unit_test/public/pw_unit_test/event_handler.h 330 331The ``pw::unit_test::EventHandler`` class defines the interface through which 332``pw_unit_test:light`` communicates the results of its test runs. If you're 333using a :ref:`custom main function <module-pw_unit_test-main>` you need to 334register an event handler to receive test output from the framework. 335 336.. _module-pw_unit_test-predefined-event-handlers: 337 338Predefined event handlers 339------------------------- 340Pigweed provides some standard event handlers to simplify the process of 341getting started with ``pw_unit_test:light``. All event handlers provide for 342GoogleTest-style output using the shared 343:cpp:class:`pw::unit_test::GoogleTestStyleEventHandler` base. Example 344output: 345 346.. code-block:: 347 348 [==========] Running all tests. 349 [ RUN ] Status.Default 350 [ OK ] Status.Default 351 [ RUN ] Status.ConstructWithStatusCode 352 [ OK ] Status.ConstructWithStatusCode 353 [ RUN ] Status.AssignFromStatusCode 354 [ OK ] Status.AssignFromStatusCode 355 [ RUN ] Status.CompareToStatusCode 356 [ OK ] Status.CompareToStatusCode 357 [ RUN ] Status.Ok_OkIsTrue 358 [ OK ] Status.Ok_OkIsTrue 359 [ RUN ] Status.NotOk_OkIsFalse 360 [ OK ] Status.NotOk_OkIsFalse 361 [ RUN ] Status.KnownString 362 [ OK ] Status.KnownString 363 [ RUN ] Status.UnknownString 364 [ OK ] Status.UnknownString 365 [==========] Done running all tests. 366 [ PASSED ] 8 test(s). 367 368.. _module-pw_unit_test-subset: 369 370Run a subset of test suites 371=========================== 372.. note:: This feature is only supported in C++17. 373 374.. _//pw_unit_test/light_public_overrides/pw_unit_test/framework_backend.h: https://cs.opensource.google/pigweed/pigweed/+/main:pw_unit_test/light_public_overrides/pw_unit_test/framework_backend.h 375 376To run only a subset of registered test suites, use the 377``pw::unit_test::SetTestSuitesToRun`` function. See 378`//pw_unit_test/light_public_overrides/pw_unit_test/framework_backend.h`_. 379 380This is useful when you've got a lot of test suites bundled up into a 381:ref:`single test binary <module-pw_unit_test-main>` and you only need 382to run some of them. 383 384.. _module-pw_unit_test-skip: 385 386Skip tests in Bazel 387=================== 388Use ``target_compatible_with`` in Bazel to skip tests. The following test is 389skipped when :ref:`using upstream GoogleTest <module-pw_unit_test-upstream>`: 390 391.. code-block:: 392 393 load("//pw_build:pigweed.bzl", "pw_cc_test") 394 395 pw_cc_test( 396 name = "no_upstream_test", 397 srcs = ["no_upstream_test.cc"], 398 target_compatible_with = select({ 399 "//pw_unit_test:light_setting": [], 400 "//conditions:default": ["@platforms//:incompatible"], 401 }), 402 } 403 404.. _module-pw_unit_test-static: 405 406Run tests in static libraries 407============================= 408To run tests in a static library, use the 409:c:macro:`PW_UNIT_TEST_LINK_FILE_CONTAINING_TEST` macro. 410 411Linkers usually ignore tests through static libraries (i.e. ``.a`` files) 412because test registration relies on the test instance's static constructor 413adding itself to a global list of tests. When linking against a static library, 414static constructors in an object file are ignored unless at least one entity 415in that object file is linked. 416 417.. _module-pw_unit_test-upstream: 418 419Use upstream GoogleTest 420======================= 421To use the upstream GoogleTest backend (``pw_unit_test:googletest``) instead 422of the default backend: 423 424.. _GoogleTestHandlerAdapter: https://cs.opensource.google/pigweed/pigweed/+/main:pw_unit_test/public/pw_unit_test/googletest_handler_adapter.h 425 4261. Clone the GoogleTest repository into your project. See 427 :ref:`module-pw_third_party_googletest`. 428 4292. :ref:`Create a custom main function <module-pw_unit_test-main>`. 430 4313. Combine `GoogleTestHandlerAdapter`_ with a :ref:`predefined event 432 handler <module-pw_unit_test-predefined-event-handlers>` to enable your 433 ``main`` function to work with upstream GoogleTest without modification. 434 435 .. code-block:: c++ 436 437 #include "pw_unit_test/framework.h" 438 #include "pw_unit_test/logging_event_handler.h" 439 440 int main() { 441 testing::InitGoogleTest(); 442 pw::unit_test::LoggingEventHandler logger; 443 pw::unit_test::RegisterEventHandler(&logger); 444 return RUN_ALL_TESTS(); 445 } 446 4474. If your tests needs GoogleTest functionality not included in the default 448 ``pw_unit_test:light`` backend, include the upstream GoogleTest headers 449 (e.g. ``gtest/gtest.h``) directly and guard your target definition to avoid 450 compiling with ``pw_unit_test:light`` (the default backend). 451 452.. _module-pw_unit_test-serial-runner: 453 454Run tests over serial 455===================== 456To accelerate automated unit test bringup for devices with plain-text logging, 457``pw_unit_test`` provides a serial-based test runner in Python that triggers a 458device flash and evaluates whether the test passed or failed based on the 459produced output. 460 461To set up a serial test runner in Python: 462 463.. _//pw_unit_test/py/pw_unit_test/serial_test_runner.py: https://cs.opensource.google/pigweed/pigweed/+/main:pw_unit_test/py/pw_unit_test/serial_test_runner.py 464 4651. Implement a ``SerialTestingDevice`` class for your device. See 466 `//pw_unit_test/py/pw_unit_test/serial_test_runner.py`_. 4672. Configure your device code to wait to run unit tests until 468 ``DEFAULT_TEST_START_CHARACTER`` is sent over the serial connection. 469 470.. _module-pw_unit_test-rpc: 471 472Run tests over RPC 473================== 474.. _//pw_unit_test/pw_unit_test_proto/unit_test.proto: https://cs.opensource.google/pigweed/pigweed/+/main:pw_unit_test/pw_unit_test_proto/unit_test.proto 475 476``pw_unit_test`` provides an RPC service which runs unit tests on demand and 477streams the results back to the client. The service is defined in 478`//pw_unit_test/pw_unit_test_proto/unit_test.proto`_. 479 480The RPC service is primarily intended for use with the default 481``pw_unit_test:light`` backend. It has some support for the upstream GoogleTest 482backend (``pw_unit_test:googletest``), however some features (such as test suite 483filtering) are missing. 484 485To set up RPC-based unit tests in your application: 486 4871. Depend on the relevant target for your build system: 488 489 * Bazel: ``@pigweed//pw_unit_test:rpc_service`` 490 * GN: ``$dir_pw_unit_test:rpc_service`` 491 4922. Create a ``pw::unit_test::UnitTestService`` instance. 493 4943. Register the instance with your RPC server. 495 496 .. code-block:: c++ 497 498 #include "pw_rpc/server.h" 499 #include "pw_unit_test/unit_test_service.h" 500 501 pw::rpc::Channel channels[] = { 502 pw::rpc::Channel::Create<1>(&my_output), 503 }; 504 pw::rpc::Server server(channels); 505 506 pw::unit_test::UnitTestService unit_test_service; 507 508 void RegisterServices() { 509 server.RegisterService(unit_test_services); 510 } 511 512 See :ref:`module-pw_rpc` for more guidance around setting up RPC. 513 5144. Run tests that have been flashed to a device by calling 515 ``pw_unit_test.rpc.run_tests()`` in Python. The argument should be an RPC 516 client services object that has the unit testing RPC service enabled. By 517 default, the results output via logging. The return value is a 518 ``TestRecord`` dataclass instance containing the results of the test run. 519 520 .. code-block:: python 521 522 import serial 523 524 from pw_hdlc import rpc 525 from pw_unit_test.rpc import run_tests 526 527 PROTO = Path( 528 os.environ['PW_ROOT'], 529 'pw_unit_test/pw_unit_test_proto/unit_test.proto' 530 ) 531 serial_device = serial.Serial(device, baud) 532 with rpc.SerialReader(serial_device) as reader: 533 with rpc.HdlcRpcClient( 534 reader, PROTO, rpc.default_channels(serial_device.write) 535 ) as client: 536 run_tests(client.rpcs()) 537 538.. _module-pw_unit_test-cpp: 539 540----------------- 541C++ API reference 542----------------- 543 544.. _module-pw_unit_test-compatibility: 545 546``pw_unit_test:light`` API compatibility 547======================================== 548``pw_unit_test:light`` offers a number of primitives for test declaration, 549assertion, event handlers, and configuration. 550 551.. note:: 552 553 The ``googletest_test_matchers`` target which provides Pigweed-specific 554 ``StatusIs``, ``IsOkAndHolds``, ``ASSERT_OK``, and ``ASSERT_OK_AND_ASSIGN`` 555 isn't part of the ``pw_unit_test:light`` backend. These matchers are only 556 usable when including the full upstream GoogleTest backend. 557 558Missing features include: 559 560* GoogleMock and matchers (e.g. :c:macro:`EXPECT_THAT`). 561* Death tests (e.g. :c:macro:`EXPECT_DEATH`). ``EXPECT_DEATH_IF_SUPPORTED`` 562 does nothing but silently passes. 563* Value-parameterized tests. 564* Stream messages (e.g. ``EXPECT_TRUE(...) << "My message"``) will compile, but 565 no message will be logged. 566 567See :ref:`module-pw_unit_test-upstream` for guidance on using the 568upstream GoogleTest backend (``pw_unit_test:googletest``) instead of 569``pw_unit_test:light``. 570 571.. _module-pw_unit_test-declare: 572 573Test declaration 574================ 575Note that ``TEST_F`` may allocate fixtures separately from the stack. 576Large variables should be stored in test fixture fields, 577rather than stack variables. This allows the test framework to statically ensure 578that enough space is available to store these variables. 579 580.. doxygendefine:: TEST 581.. doxygendefine:: GTEST_TEST 582.. doxygendefine:: TEST_F 583.. doxygendefine:: FRIEND_TEST 584 585.. _module-pw_unit_test-control: 586 587Test control 588============ 589 590.. doxygendefine:: FAIL 591.. doxygendefine:: GTEST_FAIL 592.. doxygendefine:: SUCCEED 593.. doxygendefine:: GTEST_SUCCEED 594.. doxygendefine:: GTEST_SKIP 595.. doxygendefine:: ADD_FAILURE 596.. doxygendefine:: RUN_ALL_TESTS 597.. doxygendefine:: GTEST_HAS_DEATH_TEST 598.. doxygendefine:: EXPECT_DEATH_IF_SUPPORTED 599.. doxygendefine:: ASSERT_DEATH_IF_SUPPORTED 600 601.. _module-pw_unit_test-api-expect: 602 603Expectations 604============ 605When a test fails an expectation, the framework marks the test as a failure 606and then continues executing the test. They're useful when you want to 607verify multiple dimensions of the same feature and see all the errors at the 608same time. 609 610.. doxygendefine:: EXPECT_TRUE 611.. doxygendefine:: EXPECT_FALSE 612.. doxygendefine:: EXPECT_EQ 613.. doxygendefine:: EXPECT_NE 614.. doxygendefine:: EXPECT_GT 615.. doxygendefine:: EXPECT_GE 616.. doxygendefine:: EXPECT_LT 617.. doxygendefine:: EXPECT_LE 618.. doxygendefine:: EXPECT_NEAR 619.. doxygendefine:: EXPECT_FLOAT_EQ 620.. doxygendefine:: EXPECT_DOUBLE_EQ 621.. doxygendefine:: EXPECT_STREQ 622.. doxygendefine:: EXPECT_STRNE 623 624.. _module-pw_unit_test-api-assert: 625 626Assertions 627========== 628Assertions work the same as expectations except they stop the execution of the 629test as soon as a failed condition is met. 630 631.. doxygendefine:: ASSERT_TRUE 632.. doxygendefine:: ASSERT_FALSE 633.. doxygendefine:: ASSERT_EQ 634.. doxygendefine:: ASSERT_NE 635.. doxygendefine:: ASSERT_GT 636.. doxygendefine:: ASSERT_GE 637.. doxygendefine:: ASSERT_LT 638.. doxygendefine:: ASSERT_LE 639.. doxygendefine:: ASSERT_NEAR 640.. doxygendefine:: ASSERT_FLOAT_EQ 641.. doxygendefine:: ASSERT_DOUBLE_EQ 642.. doxygendefine:: ASSERT_STREQ 643.. doxygendefine:: ASSERT_STRNE 644 645.. _module-pw_unit_test-api-event-handlers: 646 647Event handlers 648============== 649.. doxygenfunction:: pw::unit_test::RegisterEventHandler(EventHandler* event_handler) 650.. doxygenclass:: pw::unit_test::EventHandler 651 :members: 652.. doxygenclass:: pw::unit_test::GoogleTestHandlerAdapter 653.. doxygenclass:: pw::unit_test::GoogleTestStyleEventHandler 654.. doxygenclass:: pw::unit_test::SimplePrintingEventHandler 655.. doxygenclass:: pw::unit_test::LoggingEventHandler 656.. doxygenclass:: pw::unit_test::PrintfEventHandler 657.. doxygenclass:: pw::unit_test::MultiEventHandler 658.. doxygenclass:: pw::unit_test::TestRecordEventHandler 659 660.. _module-pw_unit_test-cpp-config: 661 662Configuration 663============= 664.. doxygendefine:: PW_UNIT_TEST_CONFIG_EVENT_BUFFER_SIZE 665.. doxygendefine:: PW_UNIT_TEST_CONFIG_MEMORY_POOL_SIZE 666 667.. _module-pw_unit_test-cpp-helpers: 668 669Helpers 670======= 671.. doxygendefine:: PW_UNIT_TEST_LINK_FILE_CONTAINING_TEST 672 673.. _module-pw_unit_test-py: 674 675-------------------- 676Python API reference 677-------------------- 678 679.. _module-pw_unit_test-py-serial_test_runner: 680 681``pw_unit_test.serial_test_runner`` 682=================================== 683.. automodule:: pw_unit_test.serial_test_runner 684 :members: 685 DEFAULT_TEST_START_CHARACTER, 686 SerialTestingDevice, 687 run_device_test, 688 689.. _module-pw_unit_test-py-rpc: 690 691``pw_unit_test.rpc`` 692==================== 693.. automodule:: pw_unit_test.rpc 694 :members: EventHandler, run_tests, TestRecord 695 696.. _module-pw_unit_test-helpers: 697 698---------------------- 699Build helper libraries 700---------------------- 701The following helper libraries can simplify setup and are supported in all 702build systems. 703 704.. object:: simple_printing_event_handler 705 706 When running tests, output test results as plain text over ``pw_sys_io``. 707 708.. object:: simple_printing_main 709 710 Implements a ``main()`` function that simply runs tests using the 711 ``simple_printing_event_handler``. 712 713.. object:: logging_event_handler 714 715 When running tests, log test results as plain text using 716 :ref:`module-pw_log`. Make sure your target has set a ``pw_log`` backend. 717 718.. object:: logging_main 719 720 Implements a ``main()`` function that simply runs tests using the 721 ``logging_event_handler``. 722 723.. _module-pw_unit_test-bazel: 724 725------------------- 726Bazel API reference 727------------------- 728 729See also :ref:`module-pw_unit_test-helpers`. 730 731.. _module-pw_unit_test-pw_cc_test: 732 733``pw_cc_test`` 734============== 735.. _cc_test: https://bazel.build/reference/be/c-cpp#cc_test 736 737``pw_cc_test`` is a wrapper for `cc_test`_ that provides some defaults, such as 738a dependency on ``@pigweed//pw_unit_test:main``. It supports and passes through 739all the arguments recognized by ``cc_test``. 740 741.. _module-pw_unit_test-bazel-args: 742 743Bazel build arguments 744===================== 745.. option:: pw_unit_test_backend <target> 746 747 The GoogleTest implementation to use for Pigweed unit tests. This library 748 provides ``gtest/gtest.h`` and related headers. Defaults to 749 ``@pigweed//pw_unit_test:light``, which implements a subset of GoogleTest. 750 751 Type: string (Bazel target label) 752 753 Usage: toolchain-controlled only 754 755.. option:: pw_unit_test_main <target> 756 757 Implementation of a main function for ``pw_cc_test`` unit test binaries. 758 759 Type: string (Bazel target label) 760 761 Usage: toolchain-controlled only 762 763.. _module-pw_unit_test-gn: 764 765------------ 766GN reference 767------------ 768See also :ref:`module-pw_unit_test-helpers`. 769 770.. _module-pw_unit_test-pw_test: 771 772``pw_test`` 773=========== 774``pw_test`` defines a single unit test suite. 775 776Targets 777------- 778 779.. object:: <target_name> 780 781 The test suite within a single binary. The test code is linked against 782 the target set in the build arg ``pw_unit_test_MAIN``. 783 784.. object:: <target_name>.run 785 786 If ``pw_unit_test_AUTOMATIC_RUNNER`` is set, this target runs the test as 787 part of the build. 788 789.. object:: <target_name>.lib 790 791 The test sources without ``pw_unit_test_MAIN``. 792 793Arguments 794--------- 795All GN executable arguments are accepted and forwarded to the underlying 796``pw_executable``. 797 798.. option:: enable_if 799 800 Boolean indicating whether the test should be built. If false, replaces the 801 test with an empty target. Default true. 802 803.. _get_target_outputs: https://gn.googlesource.com/gn/+/main/docs/reference.md#func_get_target_outputs 804 805.. option:: source_gen_deps 806 807 List of target labels that generate source files used by this test. The 808 labels must meet the constraints of GN's `get_target_outputs`_, namely they must have been previously defined in the 809 current file. This argument is required if a test uses generated source files 810 and ``enable_if`` can evaluate to false. 811 812.. option:: test_main 813 814 Target label to add to the tests's dependencies to provide the ``main()`` 815 function. Defaults to ``pw_unit_test_MAIN``. Set to ``""`` if ``main()`` 816 is implemented in the test's ``sources``. 817 818.. option:: test_automatic_runner_args 819 820 Array of args to pass to :ref:`automatic test 821 runner <module-pw_unit_test-serial-runner>`. Defaults to 822 ``pw_unit_test_AUTOMATIC_RUNNER_ARGS``. 823 824.. option:: envvars 825 826 Array of ``key=value`` strings representing environment variables to set 827 when invoking the automatic test runner. 828 829Example 830------- 831 832.. code-block:: 833 834 import("$dir_pw_unit_test/test.gni") 835 836 pw_test("large_test") { 837 sources = [ "large_test.cc" ] 838 enable_if = device_has_1m_flash 839 } 840 841.. _module-pw_unit_test-pw_test_group: 842 843``pw_test_group`` 844================= 845``pw_test_group`` defines a collection of tests or other test groups. 846 847Targets 848------- 849.. object:: <target_name> 850 851 The test group itself. 852 853.. object:: <target_name>.run 854 855 If ``pw_unit_test_AUTOMATIC_RUNNER`` is set, this target runs all of the 856 tests in the group and all of its group dependencies individually. See 857 :ref:`module-pw_unit_test-serial-runner`. 858 859.. object:: <target_name>.lib 860 861 The sources of all of the tests in this group and their dependencies. 862 863.. object:: <target_name>.bundle 864 865 All of the tests in the group and its dependencies bundled into a single binary. 866 867.. object:: <target_name>.bundle.run 868 869 Automatic runner for the test bundle. 870 871Arguments 872--------- 873.. option:: tests 874 875 List of the ``pw_test`` targets in the group. 876 877.. option:: group_deps 878 879 List of other ``pw_test_group`` targets on which this one depends. 880 881.. option:: enable_if 882 883 Boolean indicating whether the group target should be created. If false, an 884 empty GN group is created instead. Default true. 885 886Example 887------- 888.. code-block:: 889 890 import("$dir_pw_unit_test/test.gni") 891 892 pw_test_group("tests") { 893 tests = [ 894 ":bar_test", 895 ":foo_test", 896 ] 897 } 898 899 pw_test("foo_test") { 900 # ... 901 } 902 903 pw_test("bar_test") { 904 # ... 905 } 906 907.. _module-pw_unit_test-pw_facade_test: 908 909``pw_facade_test`` 910================== 911Pigweed facade test templates allow individual unit tests to build under the 912current device target configuration while overriding specific build arguments. 913This allows these tests to replace a facade's backend for the purpose of testing 914the facade layer. 915 916Facade tests are disabled by default. To build and run facade tests, set the GN 917arg :option:`pw_unit_test_FACADE_TESTS_ENABLED` to ``true``. 918 919.. warning:: 920 Facade tests are costly because each facade test will trigger a re-build of 921 every dependency of the test. While this sounds excessive, it's the only 922 technically correct way to handle this type of test. 923 924.. warning:: 925 Some facade test configurations may not be compatible with your target. Be 926 careful when running a facade test on a system that heavily depends on the 927 facade being tested. 928 929.. _module-pw_unit_test-gn-args: 930 931GN build arguments 932================== 933.. option:: pw_unit_test_BACKEND <source_set> 934 935 The GoogleTest implementation to use for Pigweed unit tests. This library 936 provides ``gtest/gtest.h`` and related headers. Defaults to 937 ``pw_unit_test:light``, which implements a subset of GoogleTest. 938 939 Type: string (GN path to a source set) 940 941 Usage: toolchain-controlled only 942 943.. option:: pw_unit_test_MAIN <source_set> 944 945 Implementation of a main function for ``pw_test`` unit test binaries. 946 See :ref:`module-pw_unit_test-main`. 947 948 Type: string (GN path to a source set) 949 950 Usage: toolchain-controlled only 951 952.. option:: pw_unit_test_AUTOMATIC_RUNNER <executable> 953 954 Path to a test runner to automatically run unit tests after they are built. 955 See :ref:`module-pw_unit_test-serial-runner`. 956 957 If set, a ``pw_test`` target's ``<target_name>.run`` action invokes the 958 test runner specified by this argument, passing the path to the unit test to 959 run. If this is unset, the ``pw_test`` target's ``<target_name>.run`` step 960 will do nothing. 961 962 Targets that don't support parallelized execution of tests (e.g. an 963 on-device test runner that must flash a device and run the test in serial) 964 should set ``pw_unit_test_POOL_DEPTH`` to ``1``. 965 966 Type: string (name of an executable on ``PATH``, or a path to an executable) 967 968 Usage: toolchain-controlled only 969 970.. option:: pw_unit_test_AUTOMATIC_RUNNER_ARGS <args> 971 972 An optional list of strings to pass as args to the test runner specified by 973 ``pw_unit_test_AUTOMATIC_RUNNER``. 974 975 Type: list of strings (args to pass to ``pw_unit_test_AUTOMATIC_RUNNER``) 976 977 Usage: toolchain-controlled only 978 979.. option:: pw_unit_test_AUTOMATIC_RUNNER_TIMEOUT <timeout_seconds> 980 981 An optional timeout to apply when running the automatic runner. Timeout is 982 in seconds. Defaults to empty which means no timeout. 983 984 Type: string (number of seconds to wait before killing test runner) 985 986 Usage: toolchain-controlled only 987 988.. option:: pw_unit_test_POOL_DEPTH <pool_depth> 989 990 The maximum number of unit tests that may be run concurrently for the 991 current toolchain. Setting this to 0 disables usage of a pool, allowing 992 unlimited parallelization. 993 994 Note: A single target with two toolchain configurations (e.g. ``release`` 995 and ``debug``) uses two separate test runner pools by default. Set 996 ``pw_unit_test_POOL_TOOLCHAIN`` to the same toolchain for both targets to 997 merge the pools and force serialization. 998 999 Type: integer 1000 1001 Usage: toolchain-controlled only 1002 1003.. option:: pw_unit_test_POOL_TOOLCHAIN <toolchain> 1004 1005 The toolchain to use when referring to the ``pw_unit_test`` runner pool. 1006 When this is disabled, the current toolchain is used. This means that every 1007 toolchain will use its own pool definition. If two toolchains should share 1008 the same pool, this argument should be by one of the toolchains to the GN 1009 path of the other toolchain. 1010 1011 Type: string (GN path to a toolchain) 1012 1013 Usage: toolchain-controlled only 1014 1015.. option:: pw_unit_test_EXECUTABLE_TARGET_TYPE <template name> 1016 1017 The name of the GN target type used to build ``pw_unit_test`` executables. 1018 1019 Type: string (name of a GN template) 1020 1021 Usage: toolchain-controlled only 1022 1023.. option:: pw_unit_test_EXECUTABLE_TARGET_TYPE_FILE <gni file path> 1024 1025 The path to the ``.gni`` file that defines 1026 ``pw_unit_test_EXECUTABLE_TARGET_TYPE``. 1027 1028 If ``pw_unit_test_EXECUTABLE_TARGET_TYPE`` is not the default of 1029 ``pw_executable``, this ``.gni`` file is imported to provide the template 1030 definition. 1031 1032 Type: string (path to a .gni file) 1033 1034 Usage: toolchain-controlled only 1035 1036.. option:: pw_unit_test_FACADE_TESTS_ENABLED <boolean> 1037 1038 Controls whether to build and run facade tests. Facade tests add considerably 1039 to build time, so they are disabled by default. 1040 1041.. option:: pw_unit_test_TESTONLY <boolean> 1042 1043 Controls the ``testonly`` variable in ``pw_test``, ``pw_test_group``, and 1044 miscellaneous testing targets. This is useful if your test libraries (e.g. 1045 GoogleTest) used by pw_unit_test have the ``testonly`` flag set. False by 1046 default for backwards compatibility. 1047 1048.. _module-pw_unit_test-cmake: 1049 1050--------------- 1051CMake reference 1052--------------- 1053See also :ref:`module-pw_unit_test-helpers`. 1054 1055.. _module-pw_unit_test-pw_add_test: 1056 1057``pw_add_test`` 1058=============== 1059``pw_add_test`` declares a single unit test suite. 1060 1061.. tip:: 1062 Upstream Pigweed tests can be disabled in downstream projects by setting 1063 ``pw_unit_test_ENABLE_PW_ADD_TEST`` to ``OFF`` before adding the ``pigweed`` 1064 directory to an existing cmake build. 1065 1066 .. code-block:: cmake 1067 1068 set(pw_unit_test_ENABLE_PW_ADD_TEST OFF) 1069 add_subdirectory(path/to/pigweed pigweed) 1070 set(pw_unit_test_ENABLE_PW_ADD_TEST ON) 1071 1072 See also: :ref:`module-pw_build-existing-cmake-project`. 1073 1074Targets 1075------- 1076.. object:: {NAME} 1077 1078 Depends on ``${NAME}.run`` if ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else 1079 it depends on ``${NAME}.bin``. 1080 1081.. object:: {NAME}.lib 1082 1083 Contains the provided test sources as a library target, which can then be 1084 linked into a test executable. 1085 1086.. object:: {NAME}.bin 1087 1088 A standalone executable which contains only the test sources specified in the 1089 ``pw_unit_test`` template. 1090 1091.. object:: {NAME}.run 1092 1093 Runs the unit test executable after building it if 1094 ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else it fails to build. 1095 1096Required arguments 1097------------------ 1098.. option:: NAME 1099 1100 Name to use for the produced test targets specified above. 1101 1102Optional arguments 1103------------------ 1104.. option:: SOURCES 1105 1106 Source files for this library. 1107 1108.. option:: HEADERS 1109 1110 Header files for this library. 1111 1112.. option:: PRIVATE_DEPS 1113 1114 Private ``pw_target_link_targets`` arguments. 1115.. option:: PRIVATE_INCLUDES 1116 1117 Public ``target_include_directories`` argument. 1118 1119.. option:: PRIVATE_DEFINES 1120 1121 Private ``target_compile_definitions`` arguments. 1122 1123.. option:: PRIVATE_COMPILE_OPTIONS 1124 1125 Private ``target_compile_options`` arguments. 1126 1127.. option:: PRIVATE_LINK_OPTIONS 1128 1129 Private ``target_link_options`` arguments. 1130 1131Example 1132------- 1133 1134.. code-block:: 1135 1136 include($ENV{PW_ROOT}/pw_unit_test/test.cmake) 1137 1138 pw_add_test(my_module.foo_test 1139 SOURCES 1140 foo_test.cc 1141 PRIVATE_DEPS 1142 my_module.foo 1143 ) 1144 1145.. _module-pw_unit_test-pw_add_test_group: 1146 1147``pw_add_test_group`` 1148===================== 1149``pw_add_test_group`` defines a collection of tests or other test groups. 1150 1151Targets 1152------- 1153.. object:: {NAME} 1154 1155 Depends on ``${NAME}.run`` if ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else 1156 it depends on ``${NAME}.bin``. 1157 1158.. object:: {NAME}.bundle 1159 1160 Depends on ``${NAME}.bundle.run`` if ``pw_unit_test_AUTOMATIC_RUNNER`` is 1161 set, else it depends on ``${NAME}.bundle.bin``. 1162 1163.. object:: {NAME}.lib 1164 1165 Depends on ``${NAME}.bundle.lib``. 1166 1167.. object:: {NAME}.bin 1168 1169 Depends on the provided ``TESTS``'s ``<test_dep>.bin`` targets. 1170 1171.. object:: {NAME}.run 1172 1173 Depends on the provided ``TESTS``'s ``<test_dep>.run`` targets if 1174 ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else it fails to build. 1175 1176.. object:: {NAME}.bundle.lib 1177 1178 Contains the provided tests bundled as a library target, which can then be 1179 linked into a test executable. 1180 1181.. object:: {NAME}.bundle.bin 1182 1183 Standalone executable which contains the bundled tests. 1184 1185.. object:: {NAME}.bundle.run 1186 1187 Runs the ``{NAME}.bundle.bin`` test bundle executable after building it if 1188 ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else it fails to build. 1189 1190Required arguments 1191------------------ 1192.. option:: NAME 1193 1194 The name of the executable target to be created. 1195 1196.. option:: TESTS 1197 1198 ``pw_add_test`` targets and ``pw_add_test_group`` bundles to be 1199 included in this test bundle. 1200 1201Example 1202------- 1203.. code-block:: 1204 1205 include($ENV{PW_ROOT}/pw_unit_test/test.cmake) 1206 1207 pw_add_test_group(tests 1208 TESTS 1209 bar_test 1210 foo_test 1211 ) 1212 1213 pw_add_test(foo_test 1214 # ... 1215 ) 1216 1217 pw_add_test(bar_test 1218 # ... 1219 ) 1220 1221.. _module-pw_unit_test-cmake-args: 1222 1223CMake build arguments 1224===================== 1225.. option:: pw_unit_test_BACKEND <target> 1226 1227 The GoogleTest implementation to use for Pigweed unit tests. This library 1228 provides ``gtest/gtest.h`` and related headers. Defaults to 1229 ``pw_unit_test.light``, which implements a subset of GoogleTest. 1230 1231 Type: string (CMake target name) 1232 1233 Usage: toolchain-controlled only 1234 1235.. option:: pw_unit_test_AUTOMATIC_RUNNER <executable> 1236 1237 Path to a test runner to automatically run unit tests after they're built. 1238 1239 If set, a ``pw_test`` target's ``${NAME}`` and ``${NAME}.run`` targets will 1240 invoke the test runner specified by this argument, passing the path to the 1241 unit test to run. If this is unset, the ``pw_test`` target's ``${NAME}`` will 1242 only build the unit test(s) and ``${NAME}.run`` will fail to build. 1243 1244 Type: string (name of an executable on the PATH, or path to an executable) 1245 1246 Usage: toolchain-controlled only 1247 1248.. option:: pw_unit_test_AUTOMATIC_RUNNER_ARGS <args> 1249 1250 An optional list of strings to pass as args to the test runner specified 1251 by ``pw_unit_test_AUTOMATIC_RUNNER``. 1252 1253 Type: list of strings (args to pass to pw_unit_test_AUTOMATIC_RUNNER) 1254 1255 Usage: toolchain-controlled only 1256 1257.. option:: pw_unit_test_AUTOMATIC_RUNNER_TIMEOUT_SECONDS <timeout_seconds> 1258 1259 An optional timeout to apply when running the automatic runner. Timeout is 1260 in seconds. Defaults to empty which means no timeout. 1261 1262 Type: string (number of seconds to wait before killing test runner) 1263 1264 Usage: toolchain-controlled only 1265 1266.. option:: pw_unit_test_ADD_EXECUTABLE_FUNCTION <function name> 1267 1268 The name of the CMake function used to build ``pw_unit_test`` executables. 1269 The provided function must take a ``NAME`` and a ``TEST_LIB`` argument which 1270 are the expected name of the executable target and the target which provides 1271 the unit test(s). 1272 1273 Type: string (name of a CMake function) 1274 1275 Usage: toolchain-controlled only 1276 1277.. option:: pw_unit_test_ADD_EXECUTABLE_FUNCTION_FILE <cmake file path> 1278 1279 The path to the ``.cmake`` file that defines 1280 ``pw_unit_test_ADD_EXECUTABLE_FUNCTION``. 1281 1282 Type: string (path to a ``.cmake`` file) 1283 1284 Usage: toolchain-controlled only 1285