我正在运行Android Studio 0.5.0和Gradle 1.11。我正在尝试从浓缩咖啡:浓缩咖啡:1.1-R2安装Espresso库。由于某些原因,在项目同步之后无法识别Espresso类。因此,每当我试图在import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;文件夹文件中导入androidTest时,它都会将其标记为无效。
这是我的build.gradle:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.0.2'
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.squareup.dagger:dagger-compiler:1.2.1'
compile 'com.squareup.dagger:dagger:1.2.1'
androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r2') {
exclude group: 'com.squareup.dagger'
}
}外部图书馆:

发布于 2014-04-26 00:32:37
所以这基本上是Android的一个bug (我猜)。
参考资料:
解决办法(直到修复了bug ):
在gradle文件中添加一个重复提供的依赖项,如下所示:
dependencies {
// ...
provided 'com.jakewharton.espresso:espresso:1.1-r2'
androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r2') {
exclude group: 'com.squareup.dagger'
}
}发布于 2014-12-04 06:42:54
这个问题把我逼疯了。在Android中,它似乎是一个已知的bug。在我的例子中,在我将Build从版本更改为父应用程序的Debug之后,它就解决了。希望这对某人有帮助
发布于 2015-01-02 09:02:03
浓缩咖啡2.0
最近,Espresso2.0发布,使其成为Android支持库的一部分。这是在android博客上宣布。
安装指南
有了这一点,他们也链接更新的安装指南。在这里,您可以找到从零开始配置或更新现有espresso配置为2.0的说明。
其他窍门
更改是上述两个链接包含您需要的所有信息。如果没有,我在下面列出了一些常见的错误
将Android升级到1.0.*
首先升级你的android构建。您应该能够从稳定的构建通道(=default)中获得至少1.0。所以,只需使用菜单选项Android >检查更新.。
要从最新版本中获取最新消息,您还可以进入首选项,搜索更新并将频道更改为金丝雀频道。
将安卓支持库更新为v 11+
Espresso包含在版本11的支持库中,所以您必须至少获得该版本。检查使用Android管理器进行的更新。支持库位于底部的Extras树中。
新的依赖项和命名空间
如果从较旧的espresso发行版中升级,则必须更新依赖项和命名空间。对于新项目,只需将这些添加到dependencies文件中的build.gradle文件中即可。
dependencies {
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}而且,由于命名空间发生了更改,您必须更新所有导入:
android.support.test.espresso注意,使用静态导入更容易。以一些常用的进口产品为例:
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;对于断言使用hamcrest,也有一些例子:
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;仪表转轮
测试运行程序需要配置在build.gradle文件中的defaultConfig中,以及用于从Android启动测试的运行配置中。
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}在您的运行配置中,使用它作为工具运行程序(仅全名):
android.support.test.runner.AndroidJUnitRunner示例测试用例
并给出了一个测试用例。请注意,MainActivity是您要测试的行为。测试本身是以测试开始的公共方法,如下面示例中的testListGoesOverTheFold。
@LargeTest
public class HelloWorldEspressoTest extends ActivityInstrumentationTestCase2<MainActivity> {
public HelloWorldEspressoTest() {
super(MainActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testListGoesOverTheFold() {
onView(withText("Hello world")).check(isDisplayed());
}
}有关编写测试的更多信息,请访问https://github.com/googlesamples/android-testing。
https://stackoverflow.com/questions/22246183
复制相似问题