Skip to content

Commit c45a0ad

Browse files
authored
Merge pull request #437 from fsladkey/add-junit-gtest-sample
Add junit-gtest sample
2 parents 9a9e354 + da5cb58 commit c45a0ad

File tree

19 files changed

+516
-0
lines changed

19 files changed

+516
-0
lines changed

projects.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ ui/espresso/RecyclerViewSample
1515
ui/espresso/ScreenshotSample
1616
ui/espresso/WebBasicSample
1717
ui/uiautomator/BasicSample
18+
unit/BasicNativeAndroidTest
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/*
5+
.DS_Store
6+
/build
7+
/captures
8+
.externalNativeBuild
9+
.cxx
10+
local.properties
11+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Basic sample for writing C/C++ GTest tests and running them on Android emulators / devices.
2+
3+
This project uses the Gradle build system. You don't need an IDE to build and execute it but Android Studio is recommended.
4+
5+
1. Download the project code, preferably using `git clone`.
6+
2. In Android Studio, select *File* | *Open...* and point to the `./build.gradle` file.
7+
3. Check out the relevant code:
8+
* The C++ sources and tests are in `src/main/cpp`
9+
* The device tests (which wrap the native tests) are in `src/androidTest/java`
10+
4. Create and run the Instrumented test configuration
11+
* Open the `AdderTest` file, and click the run icon in the gutter, or
12+
* Manually create a configuration.
13+
* Open *Run* menu | *Edit Configurations*
14+
* Add a new *Android Instrumented Tests* configuration
15+
* Choose the `app` module
16+
* Connect a device or start an emulator
17+
* Run the newly created configuration
18+
19+
If you are using Android Studio, the *Run* window will show the test results.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'kotlin-android'
4+
}
5+
apply plugin: 'kotlin-android'
6+
7+
android {
8+
compileSdk 32
9+
10+
defaultConfig {
11+
applicationId "com.example.android.testing.nativesample"
12+
minSdk 18
13+
targetSdk 32
14+
versionCode 1
15+
versionName "1.0"
16+
17+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18+
externalNativeBuild {
19+
cmake {
20+
arguments "-DANDROID_STL=c++_shared"
21+
}
22+
}
23+
}
24+
externalNativeBuild {
25+
cmake {
26+
path "src/main/cpp/CMakeLists.txt"
27+
}
28+
}
29+
30+
buildFeatures {
31+
prefab true
32+
}
33+
34+
buildTypes {
35+
release {
36+
minifyEnabled false
37+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
38+
}
39+
}
40+
compileOptions {
41+
sourceCompatibility JavaVersion.VERSION_1_8
42+
targetCompatibility JavaVersion.VERSION_1_8
43+
}
44+
kotlinOptions {
45+
jvmTarget = '1.8'
46+
}
47+
testOptions {
48+
managedDevices {
49+
devices {
50+
// run with ../gradlew nexusOneApi30DebugAndroidTest
51+
nexusOneApi30(com.android.build.api.dsl.ManagedVirtualDevice) {
52+
// A lower resolution device is used here for better emulator performance
53+
device = "Nexus One"
54+
apiLevel = 30
55+
// Also use the AOSP ATD image for better emulator performance
56+
systemImageSource = "aosp-atd"
57+
}
58+
}
59+
}
60+
}
61+
}
62+
63+
dependencies {
64+
androidTestImplementation "junit:junit:$rootProject.junitVersion"
65+
implementation "androidx.test.ext:junit-gtest:$rootProject.junitGtestVersion"
66+
implementation "com.android.ndk.thirdparty:googletest:$rootProject.googletestVersion"
67+
androidTestImplementation "androidx.test:runner:$rootProject.runnerVersion"
68+
androidTestImplementation "androidx.test.ext:junit-ktx:$extJUnitVersion"
69+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
22+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example.android.testing.nativesample
2+
3+
import androidx.test.ext.junitgtest.GtestRunner
4+
import androidx.test.ext.junitgtest.TargetLibrary
5+
import org.junit.runner.RunWith
6+
7+
8+
// Run our tests with the `GtestRunner`, which knows how to load and run GTest suites.
9+
@RunWith(GtestRunner::class)
10+
// Specify the name of the artifact which contains our tests, as configured in CMakeLists.txt
11+
@TargetLibrary(libraryName = "adder-test")
12+
class AdderTest
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.android.testing.nativesample">
4+
<application>
5+
</application>
6+
</manifest>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cmake_minimum_required(VERSION 3.10.2)
2+
3+
project(junit-gtest-example LANGUAGES CXX)
4+
5+
find_package(googletest REQUIRED CONFIG)
6+
find_package(junit-gtest REQUIRED CONFIG)
7+
8+
include_directories(include)
9+
10+
add_library(adder SHARED src/adder.cpp)
11+
12+
add_library(adder-test SHARED test/adder_test.cpp)
13+
14+
target_link_libraries(adder-test
15+
PRIVATE
16+
adder
17+
googletest::gtest
18+
junit-gtest::junit-gtest
19+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int add(int a, int b);

0 commit comments

Comments
 (0)