无法在Java中调用Android应用程序的Cucumber Tests

问题描述 投票:0回答:2

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

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   |
      | [email protected]     | mypasword123  |

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

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
}

那么这是我的Runner文件。

@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运行黄瓜测试,如下所示:

  • 打开“编辑配置”
  • 单击左侧面板上的+并选择“Android Instrumented Tests”
  • 在顶部的“名称”字段中输入您要记住的名称,然后选择“确定”。
  • 点击“运行”

结果这是控制台输出。我还使用断点检查它,调试器永远不会停止。

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.

请指导我哪里出错了以及我需要做些什么。

android cucumber integration-testing cucumber-jvm cucumber-java
2个回答
0
投票

编辑:你的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,不需要在这里放置仪器。

旧:

检查你的文件“黄瓜仪器”的“包”路径像qazxsw poi这样的路径复制这条路径,并将它过去你的com.my.app.test。改变你的真实路径的app/build.gradle,如com.my.app.test


0
投票

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

当我将“Scenario Outline:”更改为“Scenario:”时

它开始工作

© www.soinside.com 2019 - 2024. All rights reserved.