简体   繁体   中英

Running Espresso test inside APK

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM