无法为 Java 中的 Android 应用程序调用 Cucumber 测试

Unable to invoke Cucumber Tests for Android app in Java

我想运行黄瓜测试。我无法调用或 运行 这些测试。这就是我所做的:

app/build.gradle

defaultConfig {
testApplicationId "com.my.app.test"
testInstrumentationRunner "com.my.app.test.CucumberInstrumentation"
}

sourceSets {
androidTest { assets.srcDirs = ['src/androidTest/assets'] }
}

buildTypes {
debug {
    testCoverageEnabled true
    buildConfigField "String", "TEST_TAGS", "\"${getTestTags()}\""
}
}

def getTestTags() {
    return project.hasProperty("tags") ? project.getProperties().get("tags") : ""
}

dependencies {
    androidTestImplementation 'info.cukes:cucumber-android:1.2.5'
    androidTestImplementation 'info.cukes:cucumber-picocontainer:1.2.5'
}

这是我在 src/androidTest/assets/features 目录下的功能文件。

功能:输入登录详细信息

  @login-feature
  Scenario Outline: Successful Login
    Given   App is launch and user is not logged in
    When    I Click on Log in with Email button
    And Login screen is launched
    And I input an email, "<Email>"
    And I input a password, "<Password>"
    And I press on Log in button
    Then    I Should get logged in and redirect to home screen

    Examples:
      | Email              | Password   |
      | user@login.com     | mypasword123  |

这是我在 src/androidTest/java/com/my/app/test 目录下的登录 StepDefinitions 文件。

class LoginStepdefs {
@Rule
private ActivityTestRule<LoginActivity> activityTestRule = new ActivityTestRule<>(LoginActivity.class);

private LoginActivity activity;

@Before("@login-feature")
public void setUp() {
activityTestRule.launchActivity(new Intent());
activity = activityTestRule.getActivity();
}

@After("@login-feature")
public void tearDown() {
activityTestRule.getActivity().finish();
}


@Given("^App is launch and user is not logged in$")
public void appIsLaunchAndUserIsNotLoggedIn() throws Throwable {
System.out.println("appIsLaunchAndUserIsNotLoggedIn");
}

// and other functions
}

那么这是我的 运行ner 文件。

@CucumberOptions(
features = "features",
glue = "com.my.app.test")
public class CucumberInstrumentation extends MonitoringInstrumentation {
    private final CucumberInstrumentationCore instrumentationCore = new CucumberInstrumentationCore(this);
    @Override
    public void onCreate(Bundle arguments) {
    super.onCreate(arguments);

    String tags = BuildConfig.TEST_TAGS;
    if (!tags.isEmpty()) {
        arguments.putString("tags", tags.replaceAll(",", "--").replaceAll("\s",""));
    }

    instrumentationCore.create(arguments);
    start();
    }
    @Override
    public void onStart() {
    super.onStart();
    waitForIdleSync();
    instrumentationCore.start();
    }
}

并在 AndroidManifest 文件下。我添加了这个

<application
    <uses-library android:name="android.test.runner" />
</application>
<instrumentation
    android:name="cucumber.api.android.CucumberInstrumentation"
    android:targetPackage="com.my.app.test" />

现在我正在尝试 运行 使用 Android Studio 进行黄瓜测试,如下所示:

因此,这是控制台输出。我还使用断点检查了它,调试器从不停止。

Testing started at 6:24 PM ...
02/12 18:24:35: Launching CucumberTests
$ adb push /home/sajid/Git/app-android/app/build/outputs/apk/debug/app-debug.apk /data/local/tmp/com.my.app
$ adb shell pm install -t -r "/data/local/tmp/com.my.app"
    pkg: /data/local/tmp/com.my.app
Success
APK installed in 2 s 118 ms
$ adb push /home/sajid/Git/app-android/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk /data/local/tmp/com.my.app.test
$ adb shell pm install -t -r "/data/local/tmp/com.my.app.test"
    pkg: /data/local/tmp/com.my.app.test
Success
APK installed in 738 ms
Running tests

$ adb shell am instrument -w -r   -e debug false com.my.app.test/com.my.app.test.CucumberInstrumentation
Client not ready yet..
Started running tests
Tests ran to completion.
Test running failed: Unable to find instrumentation info for: ComponentInfo{com.my.app.test/cucumber.api.android.CucumberInstrumentation}

Empty test suite.

请指导我哪里出错了,以及我需要什么才能完成这项工作。

编辑: 你的 Manifest 应该是这样的

<application
    android:allowBackup="true"
    android:label="@string/app_name">

    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

Obs:如果您放入 Gradle,则无需在此处放置仪器。

:

检查文件 "CucumberInstrumentation" "package" 路径 像 com.my.app.test 复制此路径并将其粘贴到您的 app/build.gradle。更改 com.my.app.test 为您的真实路径,例如 com.sajid.app.test

不知道是什么原因。但是在我的功能文件中

当我把"Scenario Outline:"改成"Scenario:"

开始工作