在Android中未按顺序调用黄瓜测试

时间:2019-04-11 14:13:10

标签: java android cucumber cucumber-java

我遇到2个问题
1-除非我在步骤类函数之前添加@After或@Before,否则步骤中的Cucumber Test函数不会被调用
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目录下的功能文件。

Feature: Login
Perform login on email and password are inputted

@login-scenarios
Scenario: Input email and password in correct format
Given   Login screen is launched
When    I input an email, "user@email.com"
When    I input a password, "abc12345"
When    I press on Log in button
Then    I Should get logged in and redirect to home screen

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

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

    private LoginActivity activity;

    final static String Tag = "CucumberLogin: ";

    @Before("@login-scenarios")
    public void setUp() {
    System.out.println(Tag + "setUp in LoginStepdefs");
    activityTestRule.launchActivity(new Intent());
    activity = activityTestRule.getActivity();
    }

    @After("@login-scenarios")
    @Given("^Login screen is launched$")
    public void testLoginScreenIsLaunched() {
    System.out.println(Tag + "testLoginScreenIsLaunched");
    }

    @When("^I input an email, \"([^\"]*)\"$")
    public void iInputAnEmail(String email) {
    System.out.println(Tag + "iInputAnEmail: " + email);
    }

    @When("^I input a password, \"([^\"]*)\"$")
    public void iInputAPassword(String password) {
    System.out.println(Tag + "iInputAPassword: " + password);
    }

    @After("@login-scenarios")
    @When("^I press on Log in button$")
    public void iPressOnLogInButton() {
    System.out.println(Tag + "iPressOnLogInButton");
    }

    @After("@login-scenarios")
    @Then("^I Should get logged in and redirect to home screen$")
    public void iShouldGetLoggedInAndRedirectToHomeScreen() {
    System.out.println(Tag + "iShouldGetLoggedInAndRedirectToHomeScreen");
    }

    @After("@login-scenarios")
    public void tearDown() {
    System.out.println(Tag + "tearDown in LoginStepdefs");
    activityTestRule.finishActivity();
    }
}

然后这是我的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);

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

    waitForIdleSync();
    instrumentationCore.start();
    }
}

现在,我正在尝试像这样使用Android Studio运行黄瓜测试:

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

现在我按此顺序打印。哪有错

CucumberLogin: setUp in LoginStepdefs
CucumberLogin: iPressOnLogInButton
CucumberLogin: iShouldGetLoggedInAndRedirectToHomeScreen
CucumberLogin: tearDown in LoginStepdefs
CucumberLogin: testLoginScreenIsLaunched

这些是完整的日志

E/libprocessgroup: failed to make and chown /acct/uid_10088: Read-only file system
04-12 12:04:07.246 3481-3481/? W/Zygote: createProcessGroup failed, kernel missing CONFIG_CGROUP_CPUACCT?
04-12 12:04:07.246 3481-3481/? I/art: Not late-enabling -Xcheck:jni (already on)
04-12 12:04:07.262 3481-3481/com.my.app W/System.err: java.io.FileNotFoundException: /jacoco.exec: open failed: EROFS (Read-only file system)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at org.jacoco.agent.rt.internal_8ff85ea.output.FileOutput.openFile(FileOutput.java:67)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at org.jacoco.agent.rt.internal_8ff85ea.output.FileOutput.startup(FileOutput.java:49)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at org.jacoco.agent.rt.internal_8ff85ea.Agent.startup(Agent.java:122)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at org.jacoco.agent.rt.internal_8ff85ea.Agent.getInstance(Agent.java:50)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at org.jacoco.agent.rt.internal_8ff85ea.Offline.<clinit>(Offline.java:31)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at com.my.app.di.MyApp.$jacocoInit(MyApp.java)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at com.my.app.di.MyApp.<init>(MyApp.java)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at java.lang.reflect.Constructor.newInstance(Native Method)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at java.lang.Class.newInstance(Class.java:1606)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at android.app.Instrumentation.newApplication(Instrumentation.java:995)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at android.app.Instrumentation.newApplication(Instrumentation.java:980)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at android.app.LoadedApk.makeApplication(LoadedApk.java:558)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4526)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at android.app.ActivityThread.access$1500(ActivityThread.java:151)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at android.os.Looper.loop(Looper.java:135)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err: Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at libcore.io.Posix.open(Native Method)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:442)
04-12 12:04:07.263 3481-3481/com.my.app W/System.err:   ... 23 more
04-12 12:04:07.264 3481-3481/com.my.app I/MultiDex: VM with version 2.1.0 has multidex support
04-12 12:04:07.264 3481-3481/com.my.app I/MultiDex: Installing application
04-12 12:04:07.264 3481-3481/com.my.app I/MultiDex: VM has multidex support, MultiDex support library is disabled.
04-12 12:04:07.291 3481-3481/com.my.app I/MonitoringInstr: Instrumentation started on process com.my.app
04-12 12:04:07.298 3481-3481/com.my.app I/MonitoringInstr: Setting context classloader to 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.my.app.test-2/base.apk", zip file "/data/app/com.my.app-2/base.apk"],nativeLibraryDirectories=[/data/app/com.my.app-2/lib/x86, /vendor/lib, /system/lib]]]', Original: 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.my.app.test-2/base.apk", zip file "/data/app/com.my.app-2/base.apk"],nativeLibraryDirectories=[/data/app/com.my.app-2/lib/x86, /vendor/lib, /system/lib]]]'
04-12 12:04:07.321 3481-3481/com.my.app D/cucumber-android: Found CucumberOptions in class com.my.app.test.CucumberInstrumentation
04-12 12:04:07.454 3481-3512/com.my.app I/MonitoringInstr: Setting context classloader to 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.my.app.test-2/base.apk", zip file "/data/app/com.my.app-2/base.apk"],nativeLibraryDirectories=[/data/app/com.my.app-2/lib/x86, /vendor/lib, /system/lib]]]', Original: 'dalvik.system.PathClassLoader[DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.my.app.test-2/base.apk", zip file "/data/app/com.my.app-2/base.apk"],nativeLibraryDirectories=[/data/app/com.my.app-2/lib/x86, /vendor/lib, /system/lib]]]'
04-12 12:04:07.456 3481-3508/com.my.app W/AnalyticsUserIDStore: initStore should have been called before calling setUserID
04-12 12:04:07.456 3481-3512/com.my.app D/cucumber-android: Feature: Login (features/login.feature)
    Perform login on email and password are inputted
04-12 12:04:07.457 3481-3508/com.my.app W/UserDataStore: initStore should have been called before calling setUserID
04-12 12:04:07.462 3481-3512/com.my.app I/System.out: CucumberLogin: setUp in LoginStepdefs
04-12 12:04:07.468 3481-3481/com.my.app D/LifecycleMonitor: Lifecycle status change: com.my.app.activities.LoginActivity@1958eb70 in: PRE_ON_CREATE
04-12 12:04:07.470 3481-3481/com.my.app W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-12 12:04:07.493 3481-3481/com.my.app I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
04-12 12:04:07.495 3481-3481/com.my.app I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
04-12 12:04:07.497 3481-3481/com.my.app D/LifecycleMonitor: Lifecycle status change: com.my.app.activities.LoginActivity@1958eb70 in: CREATED
04-12 12:04:07.497 3481-3481/com.my.app D/LifecycleMonitor: Lifecycle status change: com.my.app.activities.LoginActivity@1958eb70 in: STARTED
04-12 12:04:07.497 3481-3481/com.my.app D/LifecycleMonitor: Lifecycle status change: com.my.app.activities.LoginActivity@1958eb70 in: RESUMED
04-12 12:04:07.499 3481-3519/com.my.app D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
04-12 12:04:07.500 3481-3481/com.my.app D/Atlas: Validating map...
04-12 12:04:07.527 3481-3519/com.my.app I/OpenGLRenderer: Initialized EGL, version 1.4
04-12 12:04:07.527 3481-3519/com.my.app W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
04-12 12:04:07.532 3481-3519/com.my.app D/EGL_emulation: eglCreateContext: 0xa1805dc0: maj 2 min 0 rcv 2
04-12 12:04:07.536 3481-3519/com.my.app D/EGL_emulation: eglMakeCurrent: 0xa1805dc0: ver 2 0
04-12 12:04:07.537 3481-3519/com.my.app D/OpenGLRenderer: Enabling debug mode 0
04-12 12:04:07.541 3481-3519/com.my.app D/EGL_emulation: eglMakeCurrent: 0xa1805dc0: ver 2 0
04-12 12:04:07.601 3481-3512/com.my.app D/cucumber-android: Scenario: Input email and password in correct format
04-12 12:04:07.602 3481-3512/com.my.app I/System.out: CucumberLogin: iPressOnLogInButton
04-12 12:04:07.603 3481-3512/com.my.app I/System.out: CucumberLogin: iShouldGetLoggedInAndRedirectToHomeScreen
04-12 12:04:07.603 3481-3512/com.my.app I/System.out: CucumberLogin: tearDown in LoginStepdefs
04-12 12:04:07.631 3481-3481/com.my.app D/LifecycleMonitor: Lifecycle status change: com.my.app.activities.LoginActivity@1958eb70 in: PAUSED
04-12 12:04:07.631 3481-3481/com.my.app D/LifecycleMonitor: running callback: android.support.test.rule.ActivityTestRule$LifecycleCallback@19928631
04-12 12:04:07.631 3481-3481/com.my.app D/LifecycleMonitor: callback completes: android.support.test.rule.ActivityTestRule$LifecycleCallback@19928631
04-12 12:04:07.639 3481-3512/com.my.app I/System.out: CucumberLogin: testLoginScreenIsLaunched
04-12 12:04:07.640 3481-3512/com.my.app I/MonitoringInstr: Unstopped activity count: 1
04-12 12:04:07.641 3481-3481/com.my.app I/MonitoringInstr: Activities that are still in CREATED to STOPPED: 1
04-12 12:04:07.665 3481-3519/com.my.app D/EGL_emulation: eglMakeCurrent: 0xa1805dc0: ver 2 0
04-12 12:04:07.690 3481-3512/com.my.app I/MonitoringInstr: Unstopped activity count: 1
04-12 12:04:07.719 3481-3481/com.my.app W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
04-12 12:04:07.741 3481-3512/com.my.app I/MonitoringInstr: Unstopped activity count: 1
04-12 12:04:07.763 3481-3481/com.my.app D/LifecycleMonitor: Lifecycle status change: com.my.app.activities.LoginActivity@1958eb70 in: STOPPED
04-12 12:04:07.763 3481-3481/com.my.app D/LifecycleMonitor: running callback: android.support.test.rule.ActivityTestRule$LifecycleCallback@19928631
04-12 12:04:07.763 3481-3481/com.my.app D/LifecycleMonitor: callback completes: android.support.test.rule.ActivityTestRule$LifecycleCallback@19928631
04-12 12:04:07.763 3481-3481/com.my.app D/LifecycleMonitor: Lifecycle status change: com.my.app.activities.LoginActivity@1958eb70 in: DESTROYED
04-12 12:04:07.763 3481-3481/com.my.app D/LifecycleMonitor: running callback: android.support.test.rule.ActivityTestRule$LifecycleCallback@19928631
04-12 12:04:07.763 3481-3481/com.my.app D/LifecycleMonitor: callback completes: android.support.test.rule.ActivityTestRule$LifecycleCallback@19928631
04-12 12:04:07.791 3481-3512/com.my.app I/MonitoringInstr: waitForActivitiesToComplete() took: 151ms

请帮助我

我的设置:
Gradle版本:5.0
Android插件版本:3.3.0
黄瓜图书馆版本:1.2.5

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。而且我的测试现在运行良好。

我对上述代码进行了以下更改。

在依赖项中添加了这一行以及黄瓜的依赖项

androidTestImplementation 'com.android.support:support-annotations:27.1.1'

还在我的自定义黄瓜赛跑者的@CucumberOptions中添加了这一行

  

format = {“ pretty”},

将@CucumberOptions设置为:

@CucumberOptions(
features = "features",
format = {"pretty"},
glue = "com.my.app.test.steps")

除此之外,我的代码与上述代码相同。现在开始工作。