0

I want to run an Espresso test suit inside APK.

Here is how I have tried

runTest("am instrument -w -m -e debug false -e class 'com.demo.ic.BrightnessTest' com.demo.ic.athens.test/androidx.test.runner.AndroidJUnitRunner")

public String runTest(String command) {
        try {
            Process process = Runtime.getRuntime().exec(command);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            int read;
            char[] buffer = new char[4096];
            StringBuffer output = new StringBuffer();
            while ((read = reader.read(buffer)) > 0) {
                output.append(buffer, 0, read);
            }
            reader.close();
            // Waits for the command to finish.
            process.waitFor();
            return output.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

Getting below error

TestLoader: Could not find class: 'com.demo.ic.BrightnessTest' java.lang.ClassNotFoundException: Invalid name: 'com.demo.ic.BrightnessTest'

Do I need to define anything to include test case into apk or any other way to do?

Mayank Jain
  • 271
  • 3
  • 7
  • Not really sure what you're trying to achieve, but all the espresso tests gets bundled into `androidTest.apk`, and your release apk doesn't contain any test classes. – Shivam Pokhriyal Jun 02 '21 at 04:46

1 Answers1

0

You can't include your test classes inside your debug/release apk.

All the android tests classes are bundled into a separate androidTest.apk which you can generate using a simple gradle command:

./gradlew assembleVARIANT_NAMEAndroidTest

Make sure to replace VARIANT_NAME with the specific build variant in your project. If you don't specify a variant name, gradle will look through all the module and will build apk for each variant. The apk generated using above will be located inside app/build/outputs/apk/androidTest/ directory of your project.

If you simply want to run the tests, you can use this command:

./gradlew connectedVARIANT_NAMEAndroidTest

Here also, you'll need to specify the target variant otherwise it'll run tests for each variant.

Shivam Pokhriyal
  • 1,044
  • 11
  • 26
  • Thanks, Shivam, The solution which I am looking for currently is to run test suit directly from apk not connecting the device with android studio and running ADB shell cmd. I hope you are getting my point because I have both apk in the device App under test and Test APK. – Mayank Jain Jun 03 '21 at 09:53
  • You can't run the `adb shell` shell command from inside of your app. That simply won't work since your app has no knowledge of androidTest apk. And I'm not sure if android will let you run `adb` commands in your app. `adb` is a command line tool which isn't supposed to run from the device. – Shivam Pokhriyal Jun 03 '21 at 11:00
  • Thank Shivam for the update. Anyway I found the similar solution here which I am looking https://stackoverflow.com/questions/14339216/how-to-start-instrumentation-project-programmatically-using-android-intent – Mayank Jain Jun 03 '21 at 12:12