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_unit_test:pw_cc_test.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_unit_test:pw_cc_test.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_unit_test:pw_cc_test.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``pw_status`` Helpers 545===================== 546Both the light and GoogleTest backends of ``pw_unit_test`` expose some matchers 547for dealing with Pigweed ``pw::Status`` and ``pw::Result`` values. See 548:ref:`module-pw_unit_test-api-expect` and :ref:`module-pw_unit_test-api-assert` 549for details. 550 551.. _module-pw_unit_test-compatibility: 552 553``pw_unit_test:light`` API compatibility 554======================================== 555``pw_unit_test:light`` offers a number of primitives for test declaration, 556assertion, event handlers, and configuration. 557 558.. note:: 559 560 The ``googletest_test_matchers`` target which provides Pigweed-specific 561 ``StatusIs``, ``IsOkAndHolds`` isn't part of the ``pw_unit_test:light`` 562 backend. These matchers are only usable when including the full upstream 563 GoogleTest backend. 564 565Missing features include: 566 567* GoogleMock and matchers (e.g. :c:macro:`EXPECT_THAT`). 568* Death tests (e.g. :c:macro:`EXPECT_DEATH`). ``EXPECT_DEATH_IF_SUPPORTED`` 569 does nothing but silently passes. 570* Value-parameterized tests. 571* Stream messages (e.g. ``EXPECT_TRUE(...) << "My message"``) will compile, but 572 no message will be logged. 573 574See :ref:`module-pw_unit_test-upstream` for guidance on using the 575upstream GoogleTest backend (``pw_unit_test:googletest``) instead of 576``pw_unit_test:light``. 577 578.. _module-pw_unit_test-declare: 579 580Test declaration 581================ 582Note that ``TEST_F`` may allocate fixtures separately from the stack. 583Large variables should be stored in test fixture fields, 584rather than stack variables. This allows the test framework to statically ensure 585that enough space is available to store these variables. 586 587.. doxygendefine:: TEST 588.. doxygendefine:: GTEST_TEST 589.. doxygendefine:: TEST_F 590.. doxygendefine:: FRIEND_TEST 591 592.. _module-pw_unit_test-control: 593 594Test control 595============ 596 597.. doxygenfunction:: RUN_ALL_TESTS 598.. doxygendefine:: FAIL 599.. doxygendefine:: GTEST_FAIL 600.. doxygendefine:: SUCCEED 601.. doxygendefine:: GTEST_SUCCEED 602.. doxygendefine:: GTEST_SKIP 603.. doxygendefine:: ADD_FAILURE 604.. doxygendefine:: GTEST_HAS_DEATH_TEST 605.. doxygendefine:: EXPECT_DEATH_IF_SUPPORTED 606.. doxygendefine:: ASSERT_DEATH_IF_SUPPORTED 607 608.. _module-pw_unit_test-api-expect: 609 610Expectations 611============ 612When a test fails an expectation, the framework marks the test as a failure 613and then continues executing the test. They're useful when you want to 614verify multiple dimensions of the same feature and see all the errors at the 615same time. 616 617.. doxygendefine:: EXPECT_TRUE 618.. doxygendefine:: EXPECT_FALSE 619.. doxygendefine:: EXPECT_EQ 620.. doxygendefine:: EXPECT_NE 621.. doxygendefine:: EXPECT_GT 622.. doxygendefine:: EXPECT_GE 623.. doxygendefine:: EXPECT_LT 624.. doxygendefine:: EXPECT_LE 625.. doxygendefine:: EXPECT_NEAR 626.. doxygendefine:: EXPECT_FLOAT_EQ 627.. doxygendefine:: EXPECT_DOUBLE_EQ 628.. doxygendefine:: EXPECT_STREQ 629.. doxygendefine:: EXPECT_STRNE 630.. doxygendefine:: PW_TEST_EXPECT_OK 631 632.. _module-pw_unit_test-api-assert: 633 634Assertions 635========== 636Assertions work the same as expectations except they stop the execution of the 637test as soon as a failed condition is met. 638 639.. doxygendefine:: ASSERT_TRUE 640.. doxygendefine:: ASSERT_FALSE 641.. doxygendefine:: ASSERT_EQ 642.. doxygendefine:: ASSERT_NE 643.. doxygendefine:: ASSERT_GT 644.. doxygendefine:: ASSERT_GE 645.. doxygendefine:: ASSERT_LT 646.. doxygendefine:: ASSERT_LE 647.. doxygendefine:: ASSERT_NEAR 648.. doxygendefine:: ASSERT_FLOAT_EQ 649.. doxygendefine:: ASSERT_DOUBLE_EQ 650.. doxygendefine:: ASSERT_STREQ 651.. doxygendefine:: ASSERT_STRNE 652.. doxygendefine:: PW_TEST_ASSERT_OK 653.. doxygendefine:: PW_TEST_ASSERT_OK_AND_ASSIGN 654 655.. _module-pw_unit_test-api-event-handlers: 656 657Event handlers 658============== 659.. doxygenfunction:: pw::unit_test::RegisterEventHandler(EventHandler* event_handler) 660.. doxygenclass:: pw::unit_test::EventHandler 661 :members: 662.. doxygenclass:: pw::unit_test::GoogleTestHandlerAdapter 663.. doxygenclass:: pw::unit_test::GoogleTestStyleEventHandler 664.. doxygenclass:: pw::unit_test::SimplePrintingEventHandler 665.. doxygenclass:: pw::unit_test::LoggingEventHandler 666.. doxygenclass:: pw::unit_test::PrintfEventHandler 667.. doxygenclass:: pw::unit_test::MultiEventHandler 668.. doxygenclass:: pw::unit_test::TestRecordEventHandler 669 670.. _module-pw_unit_test-cpp-config: 671 672Configuration 673============= 674.. doxygendefine:: PW_UNIT_TEST_CONFIG_EVENT_BUFFER_SIZE 675.. doxygendefine:: PW_UNIT_TEST_CONFIG_MEMORY_POOL_SIZE 676 677.. _module-pw_unit_test-cpp-helpers: 678 679Helpers 680======= 681.. doxygendefine:: PW_UNIT_TEST_LINK_FILE_CONTAINING_TEST 682 683.. _module-pw_unit_test-py: 684 685-------------------- 686Python API reference 687-------------------- 688 689.. _module-pw_unit_test-py-serial_test_runner: 690 691``pw_unit_test.serial_test_runner`` 692=================================== 693.. automodule:: pw_unit_test.serial_test_runner 694 :members: 695 DEFAULT_TEST_START_CHARACTER, 696 SerialTestingDevice, 697 run_device_test, 698 699.. _module-pw_unit_test-py-rpc: 700 701``pw_unit_test.rpc`` 702==================== 703.. automodule:: pw_unit_test.rpc 704 :members: EventHandler, run_tests, TestRecord 705 706.. _module-pw_unit_test-helpers: 707 708---------------------- 709Build helper libraries 710---------------------- 711The following helper libraries can simplify setup and are supported in all 712build systems. 713 714.. object:: simple_printing_event_handler 715 716 When running tests, output test results as plain text over ``pw_sys_io``. 717 718.. object:: simple_printing_main 719 720 Implements a ``main()`` function that simply runs tests using the 721 ``simple_printing_event_handler``. 722 723.. object:: logging_event_handler 724 725 When running tests, log test results as plain text using 726 :ref:`module-pw_log`. Make sure your target has set a ``pw_log`` backend. 727 728.. object:: logging_main 729 730 Implements a ``main()`` function that simply runs tests using the 731 ``logging_event_handler``. 732 733.. _module-pw_unit_test-bazel: 734 735------------------- 736Bazel API reference 737------------------- 738 739See also :ref:`module-pw_unit_test-helpers`. 740 741.. _module-pw_unit_test-pw_cc_test: 742 743``pw_cc_test`` 744============== 745.. _cc_test: https://bazel.build/reference/be/c-cpp#cc_test 746 747``pw_cc_test`` is a wrapper for `cc_test`_ that provides some defaults, such as 748a dependency on ``@pigweed//pw_unit_test:main``. It supports and passes through 749all the arguments recognized by ``cc_test``. 750 751.. _module-pw_unit_test-bazel-args: 752 753Bazel build arguments 754===================== 755.. option:: pw_unit_test_backend <target> 756 757 The GoogleTest implementation to use for Pigweed unit tests. This library 758 provides ``gtest/gtest.h`` and related headers. Defaults to 759 ``@pigweed//pw_unit_test:light``, which implements a subset of GoogleTest. 760 761 Type: string (Bazel target label) 762 763 Usage: toolchain-controlled only 764 765.. option:: pw_unit_test_main <target> 766 767 Implementation of a main function for ``pw_cc_test`` unit test binaries. 768 769 Type: string (Bazel target label) 770 771 Usage: toolchain-controlled only 772 773.. _module-pw_unit_test-gn: 774 775------------ 776GN reference 777------------ 778See also :ref:`module-pw_unit_test-helpers`. 779 780.. _module-pw_unit_test-pw_test: 781 782``pw_test`` 783=========== 784``pw_test`` defines a single unit test suite. 785 786Targets 787------- 788 789.. object:: <target_name> 790 791 The test suite within a single binary. The test code is linked against 792 the target set in the build arg ``pw_unit_test_MAIN``. 793 794.. object:: <target_name>.run 795 796 If ``pw_unit_test_AUTOMATIC_RUNNER`` is set, this target runs the test as 797 part of the build. 798 799.. object:: <target_name>.lib 800 801 The test sources without ``pw_unit_test_MAIN``. 802 803Arguments 804--------- 805All GN executable arguments are accepted and forwarded to the underlying 806``pw_executable``. 807 808.. option:: enable_if 809 810 Boolean indicating whether the test should be built. If false, replaces the 811 test with an empty target. Default true. 812 813.. _get_target_outputs: https://gn.googlesource.com/gn/+/main/docs/reference.md#func_get_target_outputs 814 815.. option:: source_gen_deps 816 817 List of target labels that generate source files used by this test. The 818 labels must meet the constraints of GN's `get_target_outputs`_, namely they must have been previously defined in the 819 current file. This argument is required if a test uses generated source files 820 and ``enable_if`` can evaluate to false. 821 822.. option:: test_main 823 824 Target label to add to the tests's dependencies to provide the ``main()`` 825 function. Defaults to ``pw_unit_test_MAIN``. Set to ``""`` if ``main()`` 826 is implemented in the test's ``sources``. 827 828.. option:: test_automatic_runner_args 829 830 Array of args to pass to :ref:`automatic test 831 runner <module-pw_unit_test-serial-runner>`. Defaults to 832 ``pw_unit_test_AUTOMATIC_RUNNER_ARGS``. 833 834.. option:: envvars 835 836 Array of ``key=value`` strings representing environment variables to set 837 when invoking the automatic test runner. 838 839Example 840------- 841 842.. code-block:: 843 844 import("$dir_pw_unit_test/test.gni") 845 846 pw_test("large_test") { 847 sources = [ "large_test.cc" ] 848 enable_if = device_has_1m_flash 849 } 850 851.. _module-pw_unit_test-pw_test_group: 852 853``pw_test_group`` 854================= 855``pw_test_group`` defines a collection of tests or other test groups. 856 857Targets 858------- 859.. object:: <target_name> 860 861 The test group itself. 862 863.. object:: <target_name>.run 864 865 If ``pw_unit_test_AUTOMATIC_RUNNER`` is set, this target runs all of the 866 tests in the group and all of its group dependencies individually. See 867 :ref:`module-pw_unit_test-serial-runner`. 868 869.. object:: <target_name>.lib 870 871 The sources of all of the tests in this group and their dependencies. 872 873.. object:: <target_name>.bundle 874 875 All of the tests in the group and its dependencies bundled into a single binary. 876 877.. object:: <target_name>.bundle.run 878 879 Automatic runner for the test bundle. 880 881Arguments 882--------- 883.. option:: tests 884 885 List of the ``pw_test`` targets in the group. 886 887.. option:: group_deps 888 889 List of other ``pw_test_group`` targets on which this one depends. 890 891.. option:: enable_if 892 893 Boolean indicating whether the group target should be created. If false, an 894 empty GN group is created instead. Default true. 895 896Example 897------- 898.. code-block:: 899 900 import("$dir_pw_unit_test/test.gni") 901 902 pw_test_group("tests") { 903 tests = [ 904 ":bar_test", 905 ":foo_test", 906 ] 907 } 908 909 pw_test("foo_test") { 910 # ... 911 } 912 913 pw_test("bar_test") { 914 # ... 915 } 916 917.. _module-pw_unit_test-pw_facade_test: 918 919``pw_facade_test`` 920================== 921Pigweed facade test templates allow individual unit tests to build under the 922current device target configuration while overriding specific build arguments. 923This allows these tests to replace a facade's backend for the purpose of testing 924the facade layer. 925 926Facade tests are disabled by default. To build and run facade tests, set the GN 927arg :option:`pw_unit_test_FACADE_TESTS_ENABLED` to ``true``. 928 929.. warning:: 930 Facade tests are costly because each facade test will trigger a re-build of 931 every dependency of the test. While this sounds excessive, it's the only 932 technically correct way to handle this type of test. 933 934.. warning:: 935 Some facade test configurations may not be compatible with your target. Be 936 careful when running a facade test on a system that heavily depends on the 937 facade being tested. 938 939.. _module-pw_unit_test-gn-args: 940 941GN build arguments 942================== 943.. option:: pw_unit_test_BACKEND <source_set> 944 945 The GoogleTest implementation to use for Pigweed unit tests. This library 946 provides ``gtest/gtest.h`` and related headers. Defaults to 947 ``pw_unit_test:light``, which implements a subset of GoogleTest. 948 949 Type: string (GN path to a source set) 950 951 Usage: toolchain-controlled only 952 953.. option:: pw_unit_test_MAIN <source_set> 954 955 Implementation of a main function for ``pw_test`` unit test binaries. 956 See :ref:`module-pw_unit_test-main`. 957 958 Type: string (GN path to a source set) 959 960 Usage: toolchain-controlled only 961 962.. option:: pw_unit_test_AUTOMATIC_RUNNER <executable> 963 964 Path to a test runner to automatically run unit tests after they are built. 965 See :ref:`module-pw_unit_test-serial-runner`. 966 967 If set, a ``pw_test`` target's ``<target_name>.run`` action invokes the 968 test runner specified by this argument, passing the path to the unit test to 969 run. If this is unset, the ``pw_test`` target's ``<target_name>.run`` step 970 will do nothing. 971 972 Targets that don't support parallelized execution of tests (e.g. an 973 on-device test runner that must flash a device and run the test in serial) 974 should set ``pw_unit_test_POOL_DEPTH`` to ``1``. 975 976 Type: string (name of an executable on ``PATH``, or a path to an executable) 977 978 Usage: toolchain-controlled only 979 980.. option:: pw_unit_test_AUTOMATIC_RUNNER_ARGS <args> 981 982 An optional list of strings to pass as args to the test runner specified by 983 ``pw_unit_test_AUTOMATIC_RUNNER``. 984 985 Type: list of strings (args to pass to ``pw_unit_test_AUTOMATIC_RUNNER``) 986 987 Usage: toolchain-controlled only 988 989.. option:: pw_unit_test_AUTOMATIC_RUNNER_TIMEOUT <timeout_seconds> 990 991 An optional timeout to apply when running the automatic runner. Timeout is 992 in seconds. Defaults to empty which means no timeout. 993 994 Type: string (number of seconds to wait before killing test runner) 995 996 Usage: toolchain-controlled only 997 998.. option:: pw_unit_test_POOL_DEPTH <pool_depth> 999 1000 The maximum number of unit tests that may be run concurrently for the 1001 current toolchain. Setting this to 0 disables usage of a pool, allowing 1002 unlimited parallelization. 1003 1004 Note: A single target with two toolchain configurations (e.g. ``release`` 1005 and ``debug``) uses two separate test runner pools by default. Set 1006 ``pw_unit_test_POOL_TOOLCHAIN`` to the same toolchain for both targets to 1007 merge the pools and force serialization. 1008 1009 Type: integer 1010 1011 Usage: toolchain-controlled only 1012 1013.. option:: pw_unit_test_POOL_TOOLCHAIN <toolchain> 1014 1015 The toolchain to use when referring to the ``pw_unit_test`` runner pool. 1016 When this is disabled, the current toolchain is used. This means that every 1017 toolchain will use its own pool definition. If two toolchains should share 1018 the same pool, this argument should be by one of the toolchains to the GN 1019 path of the other toolchain. 1020 1021 Type: string (GN path to a toolchain) 1022 1023 Usage: toolchain-controlled only 1024 1025.. option:: pw_unit_test_EXECUTABLE_TARGET_TYPE <template name> 1026 1027 The name of the GN target type used to build ``pw_unit_test`` executables. 1028 1029 Type: string (name of a GN template) 1030 1031 Usage: toolchain-controlled only 1032 1033.. option:: pw_unit_test_EXECUTABLE_TARGET_TYPE_FILE <gni file path> 1034 1035 The path to the ``.gni`` file that defines 1036 ``pw_unit_test_EXECUTABLE_TARGET_TYPE``. 1037 1038 If ``pw_unit_test_EXECUTABLE_TARGET_TYPE`` is not the default of 1039 ``pw_executable``, this ``.gni`` file is imported to provide the template 1040 definition. 1041 1042 Type: string (path to a .gni file) 1043 1044 Usage: toolchain-controlled only 1045 1046.. option:: pw_unit_test_FACADE_TESTS_ENABLED <boolean> 1047 1048 Controls whether to build and run facade tests. Facade tests add considerably 1049 to build time, so they are disabled by default. 1050 1051.. option:: pw_unit_test_TESTONLY <boolean> 1052 1053 Controls the ``testonly`` variable in ``pw_test``, ``pw_test_group``, and 1054 miscellaneous testing targets. This is useful if your test libraries (e.g. 1055 GoogleTest) used by pw_unit_test have the ``testonly`` flag set. False by 1056 default for backwards compatibility. 1057 1058.. _module-pw_unit_test-cmake: 1059 1060--------------- 1061CMake reference 1062--------------- 1063See also :ref:`module-pw_unit_test-helpers`. 1064 1065.. _module-pw_unit_test-pw_add_test: 1066 1067``pw_add_test`` 1068=============== 1069``pw_add_test`` declares a single unit test suite. 1070 1071.. tip:: 1072 Upstream Pigweed tests can be disabled in downstream projects by setting 1073 ``pw_unit_test_ENABLE_PW_ADD_TEST`` to ``OFF`` before adding the ``pigweed`` 1074 directory to an existing cmake build. 1075 1076 .. code-block:: cmake 1077 1078 set(pw_unit_test_ENABLE_PW_ADD_TEST OFF) 1079 add_subdirectory(path/to/pigweed pigweed) 1080 set(pw_unit_test_ENABLE_PW_ADD_TEST ON) 1081 1082 See also: :ref:`module-pw_build-existing-cmake-project`. 1083 1084Targets 1085------- 1086.. object:: {NAME} 1087 1088 Depends on ``${NAME}.run`` if ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else 1089 it depends on ``${NAME}.bin``. 1090 1091.. object:: {NAME}.lib 1092 1093 Contains the provided test sources as a library target, which can then be 1094 linked into a test executable. 1095 1096.. object:: {NAME}.bin 1097 1098 A standalone executable which contains only the test sources specified in the 1099 ``pw_unit_test`` template. 1100 1101.. object:: {NAME}.run 1102 1103 Runs the unit test executable after building it if 1104 ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else it fails to build. 1105 1106Required arguments 1107------------------ 1108.. option:: NAME 1109 1110 Name to use for the produced test targets specified above. 1111 1112Optional arguments 1113------------------ 1114.. option:: SOURCES 1115 1116 Source files for this library. 1117 1118.. option:: HEADERS 1119 1120 Header files for this library. 1121 1122.. option:: PRIVATE_DEPS 1123 1124 Private ``pw_target_link_targets`` arguments. 1125.. option:: PRIVATE_INCLUDES 1126 1127 Public ``target_include_directories`` argument. 1128 1129.. option:: PRIVATE_DEFINES 1130 1131 Private ``target_compile_definitions`` arguments. 1132 1133.. option:: PRIVATE_COMPILE_OPTIONS 1134 1135 Private ``target_compile_options`` arguments. 1136 1137.. option:: PRIVATE_LINK_OPTIONS 1138 1139 Private ``target_link_options`` arguments. 1140 1141Example 1142------- 1143 1144.. code-block:: 1145 1146 include($ENV{PW_ROOT}/pw_unit_test/test.cmake) 1147 1148 pw_add_test(my_module.foo_test 1149 SOURCES 1150 foo_test.cc 1151 PRIVATE_DEPS 1152 my_module.foo 1153 ) 1154 1155.. _module-pw_unit_test-pw_add_test_group: 1156 1157``pw_add_test_group`` 1158===================== 1159``pw_add_test_group`` defines a collection of tests or other test groups. 1160 1161Targets 1162------- 1163.. object:: {NAME} 1164 1165 Depends on ``${NAME}.run`` if ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else 1166 it depends on ``${NAME}.bin``. 1167 1168.. object:: {NAME}.bundle 1169 1170 Depends on ``${NAME}.bundle.run`` if ``pw_unit_test_AUTOMATIC_RUNNER`` is 1171 set, else it depends on ``${NAME}.bundle.bin``. 1172 1173.. object:: {NAME}.lib 1174 1175 Depends on ``${NAME}.bundle.lib``. 1176 1177.. object:: {NAME}.bin 1178 1179 Depends on the provided ``TESTS``'s ``<test_dep>.bin`` targets. 1180 1181.. object:: {NAME}.run 1182 1183 Depends on the provided ``TESTS``'s ``<test_dep>.run`` targets if 1184 ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else it fails to build. 1185 1186.. object:: {NAME}.bundle.lib 1187 1188 Contains the provided tests bundled as a library target, which can then be 1189 linked into a test executable. 1190 1191.. object:: {NAME}.bundle.bin 1192 1193 Standalone executable which contains the bundled tests. 1194 1195.. object:: {NAME}.bundle.run 1196 1197 Runs the ``{NAME}.bundle.bin`` test bundle executable after building it if 1198 ``pw_unit_test_AUTOMATIC_RUNNER`` is set, else it fails to build. 1199 1200Required arguments 1201------------------ 1202.. option:: NAME 1203 1204 The name of the executable target to be created. 1205 1206.. option:: TESTS 1207 1208 ``pw_add_test`` targets and ``pw_add_test_group`` bundles to be 1209 included in this test bundle. 1210 1211Example 1212------- 1213.. code-block:: 1214 1215 include($ENV{PW_ROOT}/pw_unit_test/test.cmake) 1216 1217 pw_add_test_group(tests 1218 TESTS 1219 bar_test 1220 foo_test 1221 ) 1222 1223 pw_add_test(foo_test 1224 # ... 1225 ) 1226 1227 pw_add_test(bar_test 1228 # ... 1229 ) 1230 1231.. _module-pw_unit_test-cmake-args: 1232 1233CMake build arguments 1234===================== 1235.. option:: pw_unit_test_BACKEND <target> 1236 1237 The GoogleTest implementation to use for Pigweed unit tests. This library 1238 provides ``gtest/gtest.h`` and related headers. Defaults to 1239 ``pw_unit_test.light``, which implements a subset of GoogleTest. 1240 1241 Type: string (CMake target name) 1242 1243 Usage: toolchain-controlled only 1244 1245.. option:: pw_unit_test_AUTOMATIC_RUNNER <executable> 1246 1247 Path to a test runner to automatically run unit tests after they're built. 1248 1249 If set, a ``pw_test`` target's ``${NAME}`` and ``${NAME}.run`` targets will 1250 invoke the test runner specified by this argument, passing the path to the 1251 unit test to run. If this is unset, the ``pw_test`` target's ``${NAME}`` will 1252 only build the unit test(s) and ``${NAME}.run`` will fail to build. 1253 1254 Type: string (name of an executable on the PATH, or path to an executable) 1255 1256 Usage: toolchain-controlled only 1257 1258.. option:: pw_unit_test_AUTOMATIC_RUNNER_ARGS <args> 1259 1260 An optional list of strings to pass as args to the test runner specified 1261 by ``pw_unit_test_AUTOMATIC_RUNNER``. 1262 1263 Type: list of strings (args to pass to pw_unit_test_AUTOMATIC_RUNNER) 1264 1265 Usage: toolchain-controlled only 1266 1267.. option:: pw_unit_test_AUTOMATIC_RUNNER_TIMEOUT_SECONDS <timeout_seconds> 1268 1269 An optional timeout to apply when running the automatic runner. Timeout is 1270 in seconds. Defaults to empty which means no timeout. 1271 1272 Type: string (number of seconds to wait before killing test runner) 1273 1274 Usage: toolchain-controlled only 1275 1276.. option:: pw_unit_test_ADD_EXECUTABLE_FUNCTION <function name> 1277 1278 The name of the CMake function used to build ``pw_unit_test`` executables. 1279 The provided function must take a ``NAME`` and a ``TEST_LIB`` argument which 1280 are the expected name of the executable target and the target which provides 1281 the unit test(s). 1282 1283 Type: string (name of a CMake function) 1284 1285 Usage: toolchain-controlled only 1286 1287.. option:: pw_unit_test_ADD_EXECUTABLE_FUNCTION_FILE <cmake file path> 1288 1289 The path to the ``.cmake`` file that defines 1290 ``pw_unit_test_ADD_EXECUTABLE_FUNCTION``. 1291 1292 Type: string (path to a ``.cmake`` file) 1293 1294 Usage: toolchain-controlled only 1295