diff options
author | Yiming Pan <yimingpan@google.com> | 2021-12-22 16:49:32 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2021-12-22 16:49:32 +0000 |
commit | 0ead8ff4ee2d76558b730c21bd8cd5bec1514f00 (patch) | |
tree | fc3113d90bf0758205150db65402f9e9384d5582 | |
parent | 3379f884b14222a8dea2c979b05d1a602f73c989 (diff) | |
parent | 35dfd0dd15f8d3674f565d694f31bbf78111aa0a (diff) | |
download | platform_testing-0ead8ff4ee2d76558b730c21bd8cd5bec1514f00.tar.gz |
Merge "Create a code coverage smoke test."
7 files changed, 126 insertions, 0 deletions
diff --git a/tests/codecoverage/hosttest/Android.bp b/tests/codecoverage/hosttest/Android.bp new file mode 100644 index 000000000..786fba92f --- /dev/null +++ b/tests/codecoverage/hosttest/Android.bp @@ -0,0 +1,14 @@ +java_test_host { + name: "CoverageSmokeTest", + + srcs: ["src/**/*.java"], + + libs: [ + "tradefed", + "junit", + ], + + data: [ + ":CoverageInstrumentationSampleTest", + ], +} diff --git a/tests/codecoverage/hosttest/src/android/platform/test/CoverageSmokeTest.java b/tests/codecoverage/hosttest/src/android/platform/test/CoverageSmokeTest.java new file mode 100644 index 000000000..226340dcd --- /dev/null +++ b/tests/codecoverage/hosttest/src/android/platform/test/CoverageSmokeTest.java @@ -0,0 +1,45 @@ +package android.platform.test.coverage; + +import static com.google.common.truth.Truth.assertThat; + +import com.android.tradefed.device.DeviceNotAvailableException; +import com.android.tradefed.metrics.proto.MetricMeasurement.Metric; +import com.android.tradefed.result.TestRunResult; +import com.android.tradefed.targetprep.TargetSetupError; +import com.android.tradefed.testtype.DeviceJUnit4ClassRunner; +import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; +import com.android.tradefed.testtype.junit4.DeviceTestRunOptions; + +import org.junit.Test; +import org.junit.runner.RunWith; + +/** Runs an instrumentation test and verifies the coverage report. */ +@RunWith(DeviceJUnit4ClassRunner.class) +public final class CoverageSmokeTest extends BaseHostJUnit4Test { + + private static final String COVERAGE_MEASUREMENT_KEY = "coverageFilePath"; + + @Test + public void instrumentationTest_generatesJavaCoverage() + throws DeviceNotAvailableException, TargetSetupError { + installPackage("CoverageInstrumentationSampleTest.apk"); + + runCoverageDeviceTests(); + + TestRunResult testRunResult = getLastDeviceRunResults(); + Metric devicePathMetric = testRunResult.getRunProtoMetrics().get(COVERAGE_MEASUREMENT_KEY); + assertThat(devicePathMetric).isNotNull(); + String testCoveragePath = devicePathMetric.getMeasurements().getSingleString(); + assertThat(testCoveragePath).isNotNull(); + } + + private void runCoverageDeviceTests() throws DeviceNotAvailableException { + DeviceTestRunOptions options = + new DeviceTestRunOptions("android.platform.test.coverage") + .setTestClassName( + "android.platform.test.coverage.CoverageInstrumentationTest") + .setTestMethodName("testCoveredMethod") + .addInstrumentationArg("coverage", "true"); + runDeviceTests(options); + } +} diff --git a/tests/codecoverage/instrumentation/Android.bp b/tests/codecoverage/instrumentation/Android.bp new file mode 100644 index 000000000..a2f702588 --- /dev/null +++ b/tests/codecoverage/instrumentation/Android.bp @@ -0,0 +1,12 @@ +android_test { + name: "CoverageInstrumentationSampleTest", + + srcs: ["src/**/*.java"], + sdk_version: "current", + min_sdk_version: "21", + + static_libs: ["androidx.test.runner"], + certificate: "platform", + + test_suites: ["device-tests"], +} diff --git a/tests/codecoverage/instrumentation/AndroidManifest.xml b/tests/codecoverage/instrumentation/AndroidManifest.xml new file mode 100644 index 000000000..69b0807d0 --- /dev/null +++ b/tests/codecoverage/instrumentation/AndroidManifest.xml @@ -0,0 +1,10 @@ +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="android.platform.test.coverage" > + + <application/> + + <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner" + android:targetPackage="android.platform.test.coverage" + android:label="Coverage smoke test"/> + +</manifest> diff --git a/tests/codecoverage/instrumentation/AndroidTest.xml b/tests/codecoverage/instrumentation/AndroidTest.xml new file mode 100644 index 000000000..eaeb45409 --- /dev/null +++ b/tests/codecoverage/instrumentation/AndroidTest.xml @@ -0,0 +1,14 @@ +<configuration description="Runs sample instrumentation test."> + <option name="test-suite-tag" value="apct" /> + <option name="test-suite-tag" value="apct-instrumentation" /> + <target_preparer class="com.android.tradefed.targetprep.TestAppInstallSetup"> + <option name="test-file-name" value="CoverageInstrumentationSampleTest.apk" /> + </target_preparer> + + <option name="test-suite-tag" value="apct" /> + <option name="test-tag" value="SampleInstrumentationTest" /> + <test class="com.android.tradefed.testtype.AndroidJUnitTest" > + <option name="package" value="android.platform.test.coverage" /> + <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" /> + </test> +</configuration> diff --git a/tests/codecoverage/instrumentation/src/android/platform/test/CoverageInstrumentationTest.java b/tests/codecoverage/instrumentation/src/android/platform/test/CoverageInstrumentationTest.java new file mode 100644 index 000000000..b4d92a161 --- /dev/null +++ b/tests/codecoverage/instrumentation/src/android/platform/test/CoverageInstrumentationTest.java @@ -0,0 +1,22 @@ +package android.platform.test.coverage; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class CoverageInstrumentationTest { + + private Example mExample; + + @Before + public void setUp() { + mExample = new Example(); + } + + @Test + public void testCoveredMethod() { + mExample.coveredMethod(); + } +} diff --git a/tests/codecoverage/instrumentation/src/android/platform/test/Example.java b/tests/codecoverage/instrumentation/src/android/platform/test/Example.java new file mode 100644 index 000000000..ecda37737 --- /dev/null +++ b/tests/codecoverage/instrumentation/src/android/platform/test/Example.java @@ -0,0 +1,9 @@ +package android.platform.test.coverage; + +/** A minimal class used to produce coverage information. */ +public class Example { + + public void coveredMethod() {} + + public void notCoveredMethod() {} +} |