我在尝试创建一个新的buildType时会遇到问题,该类型将用于在“无连接”下对应用程序进行测试。状态,理想情况下,此构建将用于测试不同的错误消息以及应用程序在无法连接到服务器后端或其他服务时应处于的状态。
我有一个MainActivityTest.java
并且android.support.test.espresso
的所有导入都失败了,Android Studio表示它无法解析这些符号。
这是我的app模块的gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "company.sampleapp"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
buildConfigField "String", "SERVER", "\"http://10.209.67.118:3000\""
// Run code coverage reports by default on debug builds.
testCoverageEnabled = true
return true
}
noconnection {
buildConfigField "String", "SERVER", "\"http://0.0.0.0\""
debuggable true
return true
}
}
// Always show the result of every unit test, even if it passes.
testOptions.unitTests.all {
testLogging {
events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
}
}
return void
}
dependencies {
// App's dependencies, including test
compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:design:$rootProject.supportLibraryVersion"
compile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
compile "com.android.support:support-v4:$rootProject.supportLibraryVersion"
compile "com.google.guava:guava:$rootProject.guavaVersion"
compile "com.github.bumptech.glide:glide:$rootProject.glideVersion"
compile "com.android.support.test.espresso:espresso-idling-resource:$rootProject.ext.espressoVersion"
// Dependencies for local unit tests
testCompile "junit:junit:$rootProject.ext.junitVersion"
testCompile "org.mockito:mockito-all:$rootProject.ext.mockitoVersion"
testCompile "org.hamcrest:hamcrest-all:$rootProject.ext.hamcrestVersion"
testCompile "org.powermock:powermock-module-junit4:$rootProject.ext.powerMockito"
testCompile "org.powermock:powermock-api-mockito:$rootProject.ext.powerMockito"
// Android Testing Support Library's runner and rules
androidTestCompile "com.android.support.test:runner:$rootProject.ext.runnerVersion"
androidTestCompile "com.android.support.test:rules:$rootProject.ext.rulesVersion"
// Espresso UI Testing dependencies.
androidTestCompile "com.android.support.test.espresso:espresso-core:$rootProject.ext.espressoVersion"
androidTestCompile "com.android.support.test.espresso:espresso-contrib:$rootProject.ext.espressoVersion"
androidTestCompile "com.android.support.test.espresso:espresso-intents:$rootProject.ext.espressoVersion"
compile fileTree(include: ['*.jar'], dir: 'libs')
compile files('libs/javax.mail.jar')
compile 'com.android.volley:volley:1.0.0'
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'com.google.android.gms:play-services-maps:10.0.0'
compile 'com.google.android.gms:play-services-location:10.0.0'
}
/*
Resolves dependency versions across test and production APKs, specifically, transitive
dependencies. This is required since Espresso internally has a dependency on support-annotations.
*/
configurations.all {
resolutionStrategy.force "com.android.support:support-annotations:25.0.1"
}
/*
All direct/transitive dependencies shared between your test and production APKs need to be
excluded from the test APK! This is necessary because both APKs will contain the same classes. Not
excluding these dependencies from your test configuration will result in an dex pre-verifier error
at runtime. More info in this tools bug: (https://code.google.com/p/android/issues/detail?id=192497)
*/
configurations.compile.dependencies.each { compileDependency ->
println "Excluding compile dependency: ${compileDependency.getName()}"
configurations.androidTestCompile.dependencies.each { androidTestCompileDependency ->
configurations.androidTestCompile.exclude module: "${compileDependency.getName()}"
}
}
并开始在src/androidTest/java/company/sampleapp/MainActivityTest.java
目前除了一些样板之外什么都没有:
package company.sampleapp;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingResource;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
/**
* Tests for main activity
* trying out functional & visual testing with applitools eyes
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {
private IdlingResource testIdleRes;
@Rule
public ActivityTestRule<MainActivity> actRule = new ActivityTestRule<>(MainActivity.class);
@Before
public void before() throws Exception {
testIdleRes = actRule.getActivity().getIdlingResource();
Espresso.registerIdlingResources(testIdleRes);
}
@Test
public void signIn_noConnection() {
}
@Test
public void signIn_emptyFields() throws Exception {
onView(withText(R.string.button_signin))
.perform(click());
onView(withId(R.id.errorSignin))
.check(matches(isDisplayed()));
}
@After
public void after() throws Exception {
Espresso.unregisterIdlingResources(testIdleRes);
}
}
在Android studio中,当我选择构建变体debug
时,项目成功编译,当我切换到noconnection
变体时,所有espresso导入都失败了。造成这种情况的原因是什么以及如何解决?
谢谢!