Files
ndk-samples/unit-test
Dan Albert bf1e27c275 Enable riscv64 for most samples.
This isn't a supported ABI yet, but I want to enable it here to get
ahead of some of that work. I just disabled that config for any of the
samples which don't already work with rv64. There are a few reasons why
some samples don't work with rv64 yet, noted in the comments next to the
line that disables the ABI. Most of them are because some upstream
dependency doesn't yet include rv64 libraries, and there's nothing we
can do about that here.

I haven't actually tested that any of these run because I don't have the
hardware to do so.
2025-08-18 14:43:51 -07:00
..
2025-08-18 14:43:51 -07:00
2022-12-08 15:08:46 -06:00
2022-12-08 15:08:46 -06:00

Unit Test

This Android sample shows how to write unit tests for native code with googletest, and run them on-device.

Writing unit tests

This app has a very simple library containing a function to add two numbers.

adder.h:

#pragma once

int add(int a, int b);

adder.cpp:

#include "adder.h"

int add(int a, int b) { return a + b; }

adder_test.cpp contains a unit test for this function:

#include "adder.h"

#include "gtest/gtest.h"

TEST(adder, adder) { EXPECT_EQ(3, add(1, 2)); }

Building and running the tests

We need to add a library for the test to CMakeLists.txt, which depends on googletest and junit-gtest:

find_package(googletest REQUIRED CONFIG)
find_package(junit-gtest REQUIRED CONFIG)
add_library(app_tests SHARED adder_test.cpp)
target_link_libraries(app_tests
  PRIVATE
    $<TARGET_OBJECTS:adder>
    googletest::gtest
    junit-gtest::junit-gtest
)

We need to add googletest and junit-gtest as dependencies in app/build.gradle. Googletest uses prefab, so we enable that.

    build_features {
    	prefab true
    }
dependencies {
    implementation 'androidx.test.ext:junit-gtest:1.0.0-alpha01'
    implementation 'com.android.ndk.thirdparty:googletest:1.11.0-beta-1'
}

Finally, we need a wrapper in androidTest, NativeTests.kt:

package com.example.unittest

import androidx.test.ext.junitgtest.GtestRunner
import androidx.test.ext.junitgtest.TargetLibrary
import org.junit.runner.RunWith

@RunWith(GtestRunner::class)
@TargetLibrary(libraryName = "app_tests")
class NativeTests

You can run the test on a phone or emulator as described in the Android developer documentation.

Try deliberately breaking the test, like this:

EXPECT_EQ(4, add(1, 2));

You should see a failure message like this:

java.lang.AssertionError:
/path/to/ndk-samples/unit-test/app/src/main/cpp/adder_test.cpp:6
Expected equality of these values:
4
add(1,2)
Which is: 3

Screenshot

screenshot

Support

If you've found an error in these samples, please file an issue.

Patches are encouraged, and may be submitted by forking this project and submitting a pull request through GitHub. Please see CONTRIBUTING.md for more details.

License

Copyright 2022 Google, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.