我基于“调试”构建变体创建了一个名为“bitrise”的新构建类型。使用“调试”构建变体时,已检测的 androidTests 构建并运行良好,但是当我切换到新的“bitrise”构建变体时,出现以下错误:
Process finished with exit code 1
Class not found: "com.mycompany.app.ui.race.RaceFragmentUiTest"
Run Code Online (Sandbox Code Playgroud)
当我选择“编辑配置...”->“Android Instrumented Tests”时出现此警告

版本
'com.android.tools.build:gradle:3.5.2'
安卓工作室 3.5.2
这是我的 Gradle 文件的摘录:
buildTypes {
debug {
debuggable true
testCoverageEnabled = false
applicationIdSuffix ".debug"
buildConfigField("String", "BASE_ENDPOINT", BASE_ENDPOINT_DEBUG)
}
release {
minifyEnabled true
zipAlignEnabled true
shrinkResources true
buildConfigField("String", "BASE_ENDPOINT", BASE_ENDPOINT_PROD)
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
bitrise {
initWith debug
buildConfigField("String", "BASE_ENDPOINT", BASE_ENDPOINT_PROD)
}
}
flavorDimensions "version"
productFlavors {
mycompany {
dimension "version"
applicationId "com.mycompany.app"
}
second {
dimension "version"
applicationId …Run Code Online (Sandbox Code Playgroud) android gradle android-testing android-gradle-plugin android-gradle-3.0
我们可以通过 stringResource 来获取 Composable 中的字符串资源,例如
@Composable
fun Heading(
@StringRes textResource: Int
) {
Text(
text = stringResource(id = textResource),
color = colorBlack,
)
}
Run Code Online (Sandbox Code Playgroud)
但是我们如何在可组合测试中获取这个字符串资源。
class HeadingTest {
@get:Rule
val composeTestRule = createComposeRule()
@ExperimentalComposeUiApi
@Test
fun headingTest() {
// Start the app
composeTestRule.setContent {
AppTheme {
// In Compose world
Heading(textResource = R.string.some_text)
}
}
//How can I access string resource here
composeTestRule.onNodeWithText(???).assertExists()
}
}
Run Code Online (Sandbox Code Playgroud) android android-testing android-jetpack android-jetpack-compose
我开始为我的公司实施仪器测试,以提高我们的代码质量和部署速度。我正在重写应用程序的一个模块,并尝试实施 TDD 原则。我的代码目前无法运行,因此我想将调试器附加到我的仪器测试中以验证其执行情况。除了在每次调试编译时我都会遇到错误Test run failed to complete. Instrumentation run failed due to Process crashed。
通过二分法,我创建了一个仅包含一个模块的简单项目,并启动了作为示例给出的仪器测试。
/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.mylibrary.test", appContext.packageName)
}
}
Run Code Online (Sandbox Code Playgroud)
相同的结果。然后我在应用程序模块中启动了相同的仪器测试,令人惊讶的是,调试器已连接并触发了断点。
无法将调试器附加到模块中的仪器测试中?当我运行测试时,该测试有效并得到验证
您将在这里找到我的库的 build.gradle 文件
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}
android {
namespace 'com.example.mylibrary'
compileSdk 33
defaultConfig {
minSdk 24
targetSdk …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Espresso for Android进行一系列测试.看来,在运行之间,活动不会被关闭.无论一次测试后的应用程序状态如何,它都会进行下一次测试.
我需要从一个全新的应用程序开始运行我的每个测试.在Robotium中,这是使用tearDown()方法中的solo.finishOpenedActivites()来处理的.
http://robotium.googlecode.com/svn/doc/com/robotium/solo/Solo.html#finishOpenedActivities()
如何用Espresso完成这项工作?
所以我在这里根据参考指南设置了power mock .这一切似乎都可以通过单个测试类完美运行.但是当执行多个JUnit测试时,我在第二个测试类上遇到以下错误.
正如您从下面的堆栈跟踪中看到的,我正在尝试模拟一个otto Bus实例.它似乎在第一个测试类上正确模拟,但在第二个类上我得到了这个类转换异常.
在堆栈跟踪上我得到了禁用Objenisis缓存的建议,但我不知道如何实现这一点,如果这实际上是根本原因,因为我正在使用classloading-xstream:1.6.2根据上面附带的Robolectric Wiki链接.
如果我运行一个JUnit测试类,那么我的设置很有效,但是一旦我尝试在一个包中运行所有测试,只有第一个测试可以工作,后续测试将获得类强制转换异常.
org.mockito.exceptions.base.MockitoException:
ClassCastException occurred while creating the mockito proxy :
class to mock : 'com.squareup.otto.Bus', loaded by classloader : 'org.robolectric.internal.bytecode.InstrumentingClassLoader@1593948d'
created class : 'com.squareup.otto.Bus$$EnhancerByMockitoWithCGLIB$$82a3b196', loaded by classloader : 'org.robolectric.internal.bytecode.InstrumentingClassLoader@1593948d'
proxy instance class : 'com.squareup.otto.Bus$$EnhancerByMockitoWithCGLIB$$82a3b196', loaded by classloader : 'org.mockito.internal.creation.util.SearchingClassLoader@618ff5c2'
instance creation by : ObjenesisInstantiator
You might experience classloading issues, disabling the Objenesis cache *might* help (see MockitoConfiguration)
at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:61)
at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:49)
at org.powermock.api.mockito.repackaged.CglibMockMaker.createMock(CglibMockMaker.java:24)
at org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.createMock(PowerMockMaker.java:45)
at com.acme.android.myapp.services.gcm.handlers.RequestLogoutHandlerTest.setup(RequestLogoutHandlerTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at …Run Code Online (Sandbox Code Playgroud) unit-testing powermock robolectric android-testing powermockito
我正在Android应用程序上运行Espresso测试.测试是片状的.它可以可靠地声明数据模型已更新.我的问题是ViewMatchers无法匹配View中的相同值,因为ViewDataBinding尚未更新Views.(至少大部分时间都是测试运行.)
当ViewDataBinding在视图上没有挂起的更改时,是否会出现IdlingResource这样的事情?
我的解决方法是调用executePendingBindings()和一个小的Thread.sleep(...)的组合.
android android-testing android-espresso android-databinding
我正在尝试ViewModel使用Robolectric.
这是我的 ViewModel
问候视图模型.kt
@FlowPreview
@ExperimentalCoroutinesApi
class GreetingsViewModel : ViewModel() {
private val greetingsChannel = ConflatedBroadcastChannel<String>()
/**
* Whenever greetingsChannel offers a value, prepend `Hello ` with it and return.
*/
val greetingsResponse = greetingsChannel.asFlow()
.flatMapLatest { name ->
flowOf("Helo $name")
}.asLiveData(viewModelScope.coroutineContext)
/**
* To get new greetings
*/
fun askGreetings(name: String) {
greetingsChannel.offer(name)
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试文件
问候ViewModelTest.kt
@FlowPreview
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class GreetingsViewModelTest2 {
@Test
fun greeting_askGreetings_getWithName() {
val testViewModel = GreetingsViewModel()
testViewModel.askGreetings("john")
testViewModel.greetingsResponse.getOrAwaitValue().should.equal("Hello john")
} …Run Code Online (Sandbox Code Playgroud) android robolectric android-testing android-architecture-lifecycle kotlin-coroutines
我正在使用 Espresso 进行仪器测试,但在堆栈跟踪上出现此错误:

该错误是由于缺少类引起的,如下所示:
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.hamcrest.Matchers" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/android.test.mock.jar", zip file "/system/framework/android.test.base.jar", zip file "/data/app/~~vnZzxGNKnS4V6YkEf4falA==/com.example.android.architecture.blueprints.reactive.test-K_x0_yJ0hJeDHaJkDmHXRw==/base.apk", zip file "/data/app/~~oeYx2MgTcILbk-vq_WPx1A==/com.example.android.architecture.blueprints.reactive-0wMHYEe95hx_1cnbdAoZAw==/base.apk"],nativeLibraryDirectories
Run Code Online (Sandbox Code Playgroud)
它首先发生在我在片段测试中添加此代码后:

这些是我在 Gradle 上的相关库:

我有这些进口:
import androidx.fragment.app.testing.launchFragmentInContainer
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.hamcrest.core.IsNot.not
Run Code Online (Sandbox Code Playgroud) dependencies android gradle android-testing android-espresso
当我运行 UI 测试时,它们工作得很好,并且我可以看到测试已通过日志传递。但 Android Studio 面板上的测试状态从未更新。
有人遇到过同样的问题吗?
测试结果作为日志:
Step2TableWithHeadersNextDirectionRow[SM-A505FN - 11] SUCCESS
....
2022-03-15T10:52:14.245+0100 [INFO] [com.android.build.gradle.internal.tasks.DeviceProviderInstrumentTestTask] [XmlResultReporter]: XML test result file generated at /Users/mySuperApp/app/build/outputs/androidTest-results/connected/flavors/superApp/TEST-SM-A505FN - 11-app-superApp.xml. Total tests 2, passed 2,
2022-03-15T10:52:14.247+0100 [QUIET] [system.out]
....
Run Code Online (Sandbox Code Playgroud)
Android Studio Bumblebee | 2021.1.1 Patch 2
Build #AI-211.7628.21.2111.8193401, built on February 17, 2022
Runtime version: 11.0.11+0-b60-7772763 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 12.2.1
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 10
Registry: external.system.auto.import.disabled=true, ide.images.show.chessboard=true
Non-Bundled Plugins: org.jetbrains.kotlin …Run Code Online (Sandbox Code Playgroud) android android-testing android-studio android-espresso android-studio-girafee
因此,我正在使用针对 API 33 (Android 13) 的应用程序运行仪器测试。我注意到一些测试由于浓缩咖啡滑动功能无法正常工作而失败。
onView(withId(android.R.id.content)).perform(swipeUp())
Run Code Online (Sandbox Code Playgroud)
测试不会在此调用中失败,但稍后会失败,因为我的测试取决于评估 UI 的操作。
作为参考,这是 Espresso swipeUp 函数中的代码:
public static ViewAction swipeUp() {
return actionWithAssertions(
new GeneralSwipeAction(
Swipe.FAST,
GeneralLocation.translate(GeneralLocation.BOTTOM_CENTER, 0, -EDGE_FUZZ_FACTOR),
GeneralLocation.TOP_CENTER,
Press.FINGER));
Run Code Online (Sandbox Code Playgroud)
}
我正在使用浓缩咖啡 3.3.0。在 API 33 模拟器上进行测试之前一切正常。
有其他人遇到过这个问题吗?如果没有,有解决方法吗?
android integration-testing android-testing android-espresso
android-testing ×10
android ×9
gradle ×2
robolectric ×2
android-architecture-lifecycle ×1
dependencies ×1
powermock ×1
powermockito ×1
testing ×1
unit-testing ×1