tree: d4577f6b7b2f5cdbe8f9d9b901e30bd061370bbc [path history] [tgz]
  1. common/
  2. README.md
  3. test.mk
test/README.md

CHRE Unit Test Framework

Background

The framework aims to provide an environment to test CHRE (and its users) code on-device, using Pigweed's Unit Test Framework. Test instantiations are syntactically identical to Googletest, so modifications to on-host unit tests to run on-device are easier.

CHRE recommends running the same host-side gtests on-device using this framework, to catch subtle bugs. For example, the target CPU may raise an exception on unaligned access, when the same code would run without any problems on a local x86-based machine.

Note

One key difference is to run the tests via a call to chre::runAllTests in chre/test/common/unit_test.h, which basically wraps the gtest RUN_ALL_TESTS macro, and implements CHRE specific event handlers for Pigweed's UT Framework.

Running Tests

Under the current incarnation of the CHRE Unit Test Framework, the following steps need to be taken to run the on-device tests:

  • Set to true and export an environment variable called CHRE_ON_DEVICE_TESTS_ENABLED from your Makefile invocation before CHRE is built.
  • Ensure that this flag is not always set to avoid codesize bloat.
  • Append your test source file to COMMON_SRCS either in test/test.mk or in your own Makefile.
  • Call chre::runAllTests from somewhere in your code.
Sample code

In math_test.cc

#include <gtest/gtest.h>

TEST(MyMath, Add) {
  int x = 1, y = 2;
  int result = myAdd(x, y);
  EXPECT_EQ(result, 3);
}

In some_source.cc

#include "chre/test/common/unit_test.h"

void utEntryFunc() {
  chre::runAllTests();
}

Caveats

Some advanced features of gtest (SCOPED_TRACE, etc.) are unsupported by Pigweed.

Compatibility

The framework has been tested with Pigweed's git revision ee460238b8a7ec0a6b4f61fe7e67a12231db6d3e