Android Studio 2.2.3
安装Android支持库- ver。44.0.0
我在Espresso的官方网站上设置了所有这些:
https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html
我尝试在package androidTest中编写插装测试(Espresso)。所以我在文件夹StringUtilAndroidTest中创建了src/androidTest/java/com/mycompany/
我的StringUtilAndroidTest代码:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class StringUtilAndroidTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);
@Test
public void myTest() {
assert(true);
}
}
在我的build.gradle:
android.defaultconfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
我的依赖关系:
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1
但是在StringUtilAndroidTest中,我得到了编译错误:
@RunWith(AndroidJUnit4.class)
无法解析符号RunWith
为什么?
发布于 2017-02-22 09:25:18
简短的回答:把这个加到你的依赖项中,你就很棒了。
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
较长的答覆:
在默认配置中,Android项目有两个不同的测试“变体”:test
& androidTest
。前者使用'src/test/java',后者使用'src/androidTest/java‘(这是您的场景)。
两者之间有一个很大的区别:androidTest
需要一个模拟器或设备来运行,而test
不需要。这意味着test
运行速度要快得多(通常在IDE上几秒钟),但它不能访问Android (比如活动、上下文等)。另一方面,androidTest
运行时间要长得多(更不用说模拟器本身的等待时间了),但它确实有安卓框架(因为它在一个框架中运行)。
因为它们是两个独立的变体,所以您也需要分别声明它们的依赖关系。testCompile
和androidTestCompile
各自只将这种依赖添加到自己的变体中。要同时使用JUnit,必须声明两者都依赖--本质上是“重复”行。
请注意,当您使用compile
时,这会将它添加到所有变体中,因此您不必重复非测试依赖项。
发布于 2017-02-22 09:38:24
你可能错过了一些依赖。
//App's dependencies, including test
compile 'com.android.support:support-annotations:22.2.0'
// Testing-only dependencies
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
希望这能修正你的代码。
https://stackoverflow.com/questions/42398022
复制