Tests on test(AVD) - failed: Instrumentation run f

2020-07-22 16:45发布

问题:

I get this error when running my Cucumber-jvm tests with Gradle on an Android emulator.

The exact same tests run perfectly on a device but I need to run them on emulator to perform the tests on Travis CI

The debug error:

Executing task ':app:connectedDebugAndroidTest' (up-to-date check took 0.0 secs) due to:
  Task has not declared any outputs.
deleteDir(/home/travis/build/neoranga55/Experiment-CI/app/build/outputs/androidTest-results/connected) returned: true
deleteDir(/home/travis/build/neoranga55/Experiment-CI/app/build/outputs/code-coverage/connected) returned: true
Starting 0 tests on test(AVD) - 5.0.2
Tests on test(AVD) - 5.0.2 failed: Instrumentation run failed due to 'java.io.IOException'

com.android.builder.testing.ConnectedDevice > No tests found.[test(AVD) - 5.0.2] [31mFAILED [0m
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack @Test annotations).
deleteDir(/home/travis/build/neoranga55/Experiment-CI/app/build/reports/androidTests/connected) returned: true
:app:connectedDebugAndroidTest FAILED
:app:connectedDebugAndroidTest (Thread[main,5,main]) completed. Took 3 mins 13.635 secs.

FAILURE: Build failed with an exception.

The full log of execution and fail: https://s3.amazonaws.com/archive.travis-ci.org/jobs/81209650/log.txt

This is the CucumberTestCase.class I created to configure which tests will be executed by Cucumber and where the results will be placed using the @CucumberOptions:

import cucumber.api.CucumberOptions;

/**
 * This class configures the Cucumber test framework and Java glue code
 */
@CucumberOptions(features = "features", // Test scenarios
        glue = {"com.neoranga55.cleanguitestarchitecture.cucumber.steps"}, // Steps definitions
        format = {"pretty", // Cucumber report formats and location to store them in phone
                "html:/mnt/sdcard/cucumber-reports/html-report",
                "json:/mnt/sdcard/cucumber-reports/cucumber.json",
                "junit:/mnt/sdcard/cucumber-reports/cucumber.xml"
        },
        tags={"~@manual", "@login-scenarios"}
)
public class CucumberTestCase {
}

The command to launch is:

./gradlew connectedAndroidTest -PdisablePreDex --stacktrace --info

回答1:

The problem 'java.io.IOException' is because Cucumber-jvm can't write to the location indicated in @CucumberOptions under format.

In my case, it's because the path exists on the device but does not exist on the emulator, that's why it fails only on emulator.

The solution is to change the path to the html, json and junit reports to a path that is writable in both device and emulator.

My initial path (/mnt/sdcard/cucumber-reports/html-report) was great for placing the html report and retrieving it after execution is finished. The path that works in both device and emulator is the example below, but this path will be deleted after application is removed by Gradle test execution:

@CucumberOptions(features = "features", // Test scenarios
        glue = {"com.neoranga55.cleanguitestarchitecture.cucumber.steps"}, // Steps definitions
        format = {"pretty", // Cucumber report formats and location to store them in phone
                "html:/data/data/com.neoranga55.cleanguitestarchitecture/cucumber-reports/html-report",
                "json:/data/data/com.neoranga55.cleanguitestarchitecture/cucumber-reports/cucumber.json",
                "junit:/data/data/com.neoranga55.cleanguitestarchitecture/cucumber-reports/cucumber.xml"
        },
        tags={"~@manual", "@login-scenarios"}
)