为什么我的黄瓜定义步骤在Android Studio中没有定义
我有一个android应用程序项目,我想在其中添加自动测试。为此,我希望在Android Studio中使用Cucumber For java,并直接在我的IDE中执行这些测试(使用运行/调试配置) 我使用的是64位Windows7SP1,使用的是Android Studio 0.8.9。我添加了为什么我的黄瓜定义步骤在Android Studio中没有定义,android,android-studio,cucumber,cucumber-jvm,Android,Android Studio,Cucumber,Cucumber Jvm,我有一个android应用程序项目,我想在其中添加自动测试。为此,我希望在Android Studio中使用Cucumber For java,并直接在我的IDE中执行这些测试(使用运行/调试配置) 我使用的是64位Windows7SP1,使用的是Android Studio 0.8.9。我添加了Gherkinversion134.1007和cucumberforjavaversion134.1007的插件。我为Cucumber使用以下库: cucumber-android-1.1.8 黄瓜核
Gherkin
version134.1007和cucumberforjava
version134.1007的插件。我为Cucumber使用以下库:
- cucumber-android-1.1.8
- 黄瓜核-1.1.8
- cucumber-html-0.2.3
- cucumber-java-1.1.8
- 黄瓜-junit-1.1.8
- cucumber-jvm-deps-1.0.3
- 小黄瓜-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'
}
这是我的舱单:
<?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>
这是我的功能文件:
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测试配置时,(下面的屏幕截图)可以找到这些特性,但没有找到我的步骤定义。输出说
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)
注释
我做错了什么?您正在抛出一个异常:
抛出新的PendingException()代码>
在
我遇到了同样的问题,我想我解决了。首先,我们需要在所有cucumber相关库上使用androidTestCompile
配置。注意,我们需要为android库使用@jar限定符,以避免gradle获取apklib文件而不是jar文件。例如:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
...
androidTestCompile 'info.cukes:cucumber-core:1.2.0'
androidTestCompile 'info.cukes:cucumber-java:1.2.0'
androidTestCompile 'info.cukes:gherkin:2.12.2'
androidTestCompile 'info.cukes:cucumber-html:0.2.3'
androidTestCompile 'info.cukes:cucumber-android:1.2.0@jar'
}
其次,我们不需要为Android Studio使用cucumber java插件。我们需要在模拟器/设备上运行测试,我找不到一种方法用cucumber java插件来设置它。相反,只需在运行配置中使用Android测试,并将特定的Instrumentation Runner设置为cucumber.api.Android.CucumberInstrumentation
我注意到的最后一件事是,您不需要使用@RunWith
注释。只要在你的一个类上使用@CucumberOptions
,它就可以工作。你的测试应该在src/androidTest
文件夹中,而不是src/test
此外,我还将CucumberTest.java与配置注释分离为CucumberRunner的一个类,并从包含粘合代码的ActivityInstrumentationTestCase2
扩展为另一个类
这里有一个Android Studio使用Cucumber和Espresso代替Robotium的运行示例:
首先看,似乎测试应用程序清单中的目标包是错误的-com.novomnetworks.formeval.truckcalibrator.test-remove。testI已尝试使用和不使用,我还尝试将CucumberTest类不放在测试包中(因此它与我的MainActivity类是同一个包),但我也犯了同样的错误我不明白你想告诉我什么。Cucumber甚至找不到i\u启动了\u app
方法,那么它的内容是什么呢?另外,这正是错误消息告诉我要放入方法中的内容;从你的代码。这一行正好抛出你在跟踪中看到的信息。cucumber的意思是,您可以使用该模板,但必须删除该行。删除该行并使用CucumberOptions而不是@RunWithI删除了抛出新的PendingException()代码>行,并将@RunWith
注释替换为@cucucumberoptions
注释,但我得到了完全相同的错误。这个错误没有提到要删除抛出异常的任何地方,它告诉我们使用抛出异常命令创建方法……另一个问题可能是构造函数。我有publiccumbertest(SomeDependency){super(MainActivity.class);}
SomeDependency是什么?我的类应该具有什么样的依赖关系?我可以确认您的测试正在android studio中运行,如果我们使用CucumberTestRunner.java运行它,是否有任何方法可以一次运行一个场景以实现更简单的测试方法?我使用标记,然后在启动测试时,我会按标记筛选以仅运行一个测试用例,比如:./gradlew connectedAndroidTest-Ptags=“@test-scenario-whatever-id-001”
@Given("^I started the app$")
public void i_started_the_app() throws Throwable {
solo.waitForActivity(MainActivity.class);
throw new PendingException();
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
...
androidTestCompile 'info.cukes:cucumber-core:1.2.0'
androidTestCompile 'info.cukes:cucumber-java:1.2.0'
androidTestCompile 'info.cukes:gherkin:2.12.2'
androidTestCompile 'info.cukes:cucumber-html:0.2.3'
androidTestCompile 'info.cukes:cucumber-android:1.2.0@jar'
}