android - 为什么我的 Cucumber 定义步骤在 Android Studio 中未定义

标签 android android-studio cucumber cucumber-jvm

我有一个 android 应用程序项目,我想在其中添加自动化测试。为此,我想在 Android Studio 中使用 Cucumber for java 并直接在我的 IDE 中执行这些测试(使用运行/调试配置)。

我在 Windows 7 sp1 64 位上使用 Android Studio 0.8.9。我添加了插件 Gherkin 版本 134.1007 和 Cucumber for Java 版本 134.1007。我将以下库用于 cucumber :

  • cucumber -android-1.1.8
  • cucumber 核心-1.1.8
  • cucumber -html-0.2.3
  • cucumber -java-1.1.8
  • cucumber -junit-1.1.8
  • cucumber -jvm-deps-1.0.3
  • 小 cucumber -2.12.2
  • robotium-solo-5.2.1

这是我的项目结构:

TruckCalibrator/
    .idea/
    app/
        build/
        libs/
            [libraries listed above in .jar format]
        src/
            main/
                java/
                    com/
                        novomnetworks/
                            formeval/
                                truckcalibrator/
                                    MainActivity.java
                res/
                AndroidManifest.xml
            test/
                assets/
                    features/
                        app_start.feature
                java/
                    com/
                        novomnetworks/
                            formeval/
                                truckcalibrator/
                                    test/
                                        CucumberTest.java
            build.gradle
    build/
    gradle/

这是我的 gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 20
    buildToolsVersion '20.0.0'

    defaultConfig {
        applicationId "com.novomnetworks.formeval.truckcalibrator"
        minSdkVersion 16
        targetSdkVersion 20
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "cucumber.api.android.CucumberInstrumentation"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        instrumentTest {
            java.srcDirs = ['src/test/java']
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.github.satyan:sugar:1.3'
}

这是我的 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.novomnetworks.formeval.truckcalibrator"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <instrumentation
        android:name="cucumber.api.android.CucumberInstrumentation"
        android:targetPackage="com.novomnetworks.formeval.truckcalibrator.test" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/launcher_icon"
        tools:replace="icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:name="com.orm.SugarApp">
        <uses-library android:name="android.test.runner" />
        <meta-data android:name="DATABASE" android:value="form-eval.db" />
        <meta-data android:name="VERSION" android:value="1" />
        <meta-data android:name="QUERY_LOG" android:value="true" />
        <meta-data android:name="DOMAIN_PACKAGE_NAME" android:value="com.novomnetworks.formeval.truckcalibrator.database" />
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是我的 Cucumber 步骤定义文件 (CucumberTest.java):

package com.novomnetworks.formeval.truckcalibrator.test;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import cucumber.api.CucumberOptions;
import cucumber.api.PendingException;
import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import cucumber.api.junit.Cucumber;

import com.novomnetworks.formeval.truckcalibrator.MainActivity;
import com.novomnetworks.formeval.truckcalibrator.R;
import com.robotium.solo.Solo;

import org.junit.runner.RunWith;

/**
 * Created by rroyer on 2014-10-24.
 */
@RunWith(Cucumber.class)
/*@CucumberOptions(monochrome = true,
        tags = "@tags",
        features = "src/test/assets/features/",
        format = { "pretty","html: cucumber-html-reports", "json: cucumber-html-reports/cucumber.json" },
        dryRun = false,
        glue = "com.novomnetworks.formeval.truckcalibrator.test" )*/
public class CucumberTest extends ActivityInstrumentationTestCase2<MainActivity> {
    private static final String TAG = "CucumberTest";

    Solo solo;

    public CucumberTest() {
        super(MainActivity.class);
    }

    @Before
    protected void before() throws Exception {
        Log.d(TAG, "setUp");
        super.setUp();
        solo = new Solo(getInstrumentation(), getActivity());
        getActivity().resetDB();
    }

    @After
    protected void after() throws Exception {
        Log.d(TAG, "tearDown");
        solo.finishOpenedActivities();
        super.tearDown();
    }

    @Given("^I started the app$")
    public void i_started_the_app() throws Throwable {
        solo.waitForActivity(MainActivity.class);
        throw new PendingException();
    }

    @Then("I should see the action bar")
    public void I_should_see_the_action_bar() throws Exception {
        assertNotNull(getActivity().getMenu());
    }

    @Given("I am on the clients list")
    public void I_am_on_the_clients_list() throws Exception {
        solo.waitForFragmentById(R.layout.fragment_clients);
    }

    @Then("^I should see the clients list header$")
    public void I_should_see_the_clients_list_header() throws Exception {
        assertTrue(solo.searchText(solo.getString(R.string.clients)));
        assertTrue(solo.searchText(solo.getString(R.string.sorted_by)));
        assertNotNull(solo.getView(R.id.clients_spinner));
    }

    @Then("I should see the new client button")
    public void I_should_see_the_new_client_button() throws Exception {
        assertNotNull(solo.getView(R.id.action_new_client).isShown());
    }

    @Then("^I should see the clients list$")
    public void I_should_see_the_clients_list() throws Exception {
        assertNotNull(solo.getView(R.id.clients));
    }
}

这是我的特征文件:

Feature: Démarrage de l'app
  Scenario: La barre de menu devrait s'afficher
    Given I started the app
    Then I should see the action bar
  Scenario: La liste des clients devrait s'afficher
    Given I started the app
    Then I should see the clients list header
    And I should see the new client button
    And I should see the clients list

当我运行我的 Cucumber 测试配置时,(屏幕截图如下)Run/Debug configuration screenshot of my Cucumber test file找到了这些功能,但没有找到我的步骤定义。输出说

Undefined step: Given  I started the app
Undefined step: Then  I should see the action bar
Undefined step: Given  I started the app
Undefined step: Then  I should see the clients list header
Undefined step: And  I should see the new client button
Undefined step: And  I should see the clients list

2 Scenarios (2 undefined)
6 Steps (6 undefined)
0m0,000s

You can implement missing steps with the snippets below:

@Given("^I started the app$")
public void i_started_the_app() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

[other similar suggestions...]

我在网上搜索了一整天都没有找到链接我的步骤定义文件的正确方法。我找到的每个帖子似乎都告诉人们在配置字段 Glue 中提到测试包,这就是我所做的,但没有用。如果您仔细查看我的 CucumberTest.java 文件,您会发现我也尝试使用 @CucumberOptions 注释,但它并没有改变结果。我还尝试了使用和不使用 @RunWith(Cucumber.class) 注释。

我做错了什么?

最佳答案

你正在抛出异常:throw new PendingException();

@Given("^I started the app$")
    public void i_started_the_app() throws Throwable {
        solo.waitForActivity(MainActivity.class);
        throw new PendingException();
    }

关于android - 为什么我的 Cucumber 定义步骤在 Android Studio 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26592281/

相关文章:

android-studio - 为 Android Studio 中的方法生成 KDoc

带有 AndroidAnnotations 的 Android Studio -> 这些类型将不会进行注释处理

java - cucumber \Java : Defining a step with both optional words and parameter

java - 在 Cucumber 中的步骤定义之间共享状态

ios - "Make sure you' ve set APP_BUNDLE_PATH to a build supported by this simulator version“在测试套件运行中出现错误

android - 使用 StartApp 添加广告时出错

java - 制作精准的Android节拍器

java - 流程应用程序执行失败 :dexDebug after adding BaseGameUtils

android - 如何理解Android Studio中的内存分配?

java - Android TextView水平滚动不起作用