我希望能够在我的 android 项目的库模块中拥有通用的测试代码,以便项目中的不同应用程序可以使用它们。
问题是,应用程序无法从 导入类<library>/src/androidTest/java,如果我将代码中的类移动到 中src\main,则必须将依赖项从 移动androidTestCompile到compile(更多依赖项到发布版本)。
现在唯一的解决方案是创建一个单独的库来保存共享的测试类,但是这样做的缺点是在项目结构中添加了一个新库,这没什么大不了的,但我还是想知道有更好的解决方案。
如果有任何 Gradle(Android 插件)向导可以帮助我找到一个,我宁愿在这一点上实施 Gradle hack。
有没有办法使用 Gradle 在 Android 上只运行失败的测试集?
目前,我按如下方式运行测试。
./gradle connectedDebugAndroidTest
Run Code Online (Sandbox Code Playgroud)
有些测试偶尔会由于难以控制的环境问题而失败,我想做的是只能运行那些失败的测试并将结果与之前的测试结果合并。
例如,如果我有 100 个测试并且 90 个成功,我想重新运行失败的 10 个测试。如果这 10 个第二次通过,我想将这些结果与原始测试运行合并。
看起来 Gradle 已经多次讨论过这个问题,但似乎还没有解决方案。
https://github.com/gradle/gradle/issues/4068
https://github.com/gradle/gradle/issues/4450
https://github.com/gradle/gradle/issues/1283
Run Code Online (Sandbox Code Playgroud)
谢谢!
我正在为一个活动编写测试,该活动连续多次调用服务器。我的 MockWebServer 混合了响应序列。例如,当我发出两个连续的请求 request1 和 request2 时,它有时会返回 request2 的 Json 以响应 request1 和 request1 的 Json 以响应 request2。如何指定 MockWebServer 必须返回指定请求的响应?
server.enqueue(new MockResponse()
.setResponseCode(200)
.setBody(readFromFile("response1 path"));
server.enqueue(new MockResponse()
.setResponseCode(200)
.setBody(readFromFile("response2 path"));
Run Code Online (Sandbox Code Playgroud)
在文档中说“将脚本响应排入队列以返回到按顺序发出的请求。第一个请求由第一个入队响应提供服务;第二个请求由第二个入队响应提供服务;依此类推。”
在并行请求的情况下,此序列不起作用。
最近,当我尝试调试 android 测试仪器 APK 时,每次启动应用程序时,我都会在 logcat 中看到类似的内容
W/com.myapp: Verification of void com.myapp.test.hiptest.Actionwords.function0() took 158.211ms
W/com.myapp: Verification of void com.myapp.test.hiptest.Actionwords.function1() took 101.701ms
W/com.myapp: Verification of void com.myapp.test.hiptest.Actionwords.function2() took 110.852ms
W/com.myapp: Verification of void com.myapp.test.hiptest.Actionwords.function3() took 211.494ms
W/com.myapp: Verification of void com.myapp.test.hiptest.Actionwords.function4() took 102.497ms
W/com.myapp: Verification of void com.myapp.test.hiptest.Actionwords.function5() took 126.639ms
W/com.myapp: Verification of void com.myapp.test.hiptest.Actionwords.function6() took 138.077ms
W/com.myapp: Verification of void com.myapp.test.hiptest.Actionwords.function7() took 131.311ms
W/com.myapp: Verification of void com.myapp.test.hiptest.Actionwords.function8() took 253.859ms
W/com.myapp: Verification of void com.myapp.test.hiptest.Actionwords.function9() took 111.431ms
W/com.myapp: Verification of …Run Code Online (Sandbox Code Playgroud) androidTest目录下写了一些测试用例。testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner")添加的依赖项:
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
还添加了 JaCoCo 作为依赖项。
createDebugAndroidTestCoverageReportGradle 任务时,所有测试用例都成功运行,但报告中的覆盖率为 0%我猜这是由于这个错误:
V/InstrumentationResultParser:错误:无法生成 Emma/JaCoCo 覆盖范围。V/InstrumentationResultParser:INSTRUMENTATION_CODE:-1
我的代码库使用了一些 Java 8 语法,如 lambdas,并且已经运行了很长时间。
最近,我在一个模块中的仪器测试停止使用臭名昭著的消息:
AGPBI: {"kind":"error","text":"Invoke-customs 仅支持从 Android O (--min-api 26)","sources":[{}],"tool":" D8"}
这是一个已知问题(有很多 问题参考它),但我的 1.8 中有 Java compileOptions:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Run Code Online (Sandbox Code Playgroud)
这个问题只出现在仪器测试(即androidTest)中。单元测试和应用程序本身都很好。我已经注释掉了仪器测试中的所有测试,但问题仍然存在。
我的单元测试和 Android 测试具有相同的依赖项。我转换为 AndroidX 测试,但问题仍然存在。
dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.google.android.gms:play-services-gcm:16.1.0'
androidTestImplementation 'org.mockito:mockito-core:2.27.0'
androidTestImplementation 'androidx.test:core:1.1.0'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.ext:truth:1.1.0'
androidTestImplementation 'com.google.truth:truth:0.42'
testImplementation 'org.mockito:mockito-core:2.27.0'
testImplementation 'androidx.test:core:1.1.0'
testImplementation 'androidx.test:runner:1.1.1'
testImplementation 'androidx.test:rules:1.1.1'
testImplementation 'androidx.test.ext:junit:1.1.0'
testImplementation 'androidx.test.ext:truth:1.1.0'
testImplementation 'com.google.truth:truth:0.42'
}
Run Code Online (Sandbox Code Playgroud)
我正在使用最新的编译和目标版本(我也尝试了 26 和 27)并且还构建了工具。最小 SDK 版本是 14,应该是。
compileSdkVersion 28 …Run Code Online (Sandbox Code Playgroud) 有什么意义
android.os.Debug.waitForDebugger();
Run Code Online (Sandbox Code Playgroud)
我知道我们 [有时] 需要使用此函数来调试 a Service,但我的问题是:
Service必须以这种方式调试,而不是 an Activityor BroadcastReceiver?有人会认为在项目中调试任何类型的代码都很简单。除了它不是。Service反正不是为了。
这个特殊程序存在的原因是什么?它还有什么用途?
等待调试器连接。一旦调试器附加,它就会返回,所以如果你想立即开始跟踪,你需要在 waitForDebugger() 调用之后放置一个断点。
该为什么仍然没有答案。
更新:
我发现了此功能的一个用例:当应用程序进程终止后自动重启 Android 应用程序时。当用户在应用程序设置中切换运行时权限时,该进程将被终止。更一般地说,一个应用程序进程被杀死并重新启动,只要...
android.os.Process.killProcess(android.os.Process.myPid());
Run Code Online (Sandbox Code Playgroud)
叫做。
要在进程重新启动后调试应用程序,请编写android.os.Debug.waitForDebugger()代码,然后将调试器附加到当前进程。
这是此功能的一个用例。
参考资料:
调试服务。
可能相关:
android android-service android-testing android-debug android-developer-api
我dagger hilt在我的项目中使用。我想写UI test一些片段。我需要viewModel在测试类中模拟 并将其与测试下的片段相关联.. 我阅读了dagger hilt文档,但没有找到任何解决方案。
class HomeViewModel @ViewModelInject constructor(
private val repository: MainRepository,
prefManager: PrefManager,
private val firebaseAnalytics: FirebaseAnalytics,
@Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel() {
/////
}
@AndroidEntryPoint
class HomeFragment : BaseFragment() {
private val viewModel: HomeViewModel by viewModels()
/////
}
Run Code Online (Sandbox Code Playgroud) 我有一个来自我创建的活动的意图,如下所示:
private fun startShareIntent() {
val sendIntent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "Watch ${viewmodel.movie.value?.title} with me!\n\n${viewmodel.movie.value?.summary}")
type="text/plain"
}
val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)
}
Run Code Online (Sandbox Code Playgroud)
当我单击工具栏上的图标时,该功能就会运行。我想对其运行 Espresso UI 测试,以检查启动的活动是否确实符合上述意图。为此,我只是首先检查意图是否实际上是上述意图,如下所示:
@Test
fun test_clicking_share_icon_shows_sharing_sheet() {
val scenario = activityScenarioRule.scenario
Intents.init()
val expectedIntent = allOf(
hasAction(Intent.ACTION_SEND),
hasExtra(Intent.EXTRA_TEXT, "Watch ${dummyMovieData.title} with me!\n\n${dummyMovieData.summary}"),
hasType("text/plain")
)
onView(withText(dummyMovieData.title)).perform(click())
onView(withId(R.id.share_detail)).perform(click())
intended(expectedIntent)
Intents.release()
}
Run Code Online (Sandbox Code Playgroud)
但是,这会返回一个AssertionFailedError:
junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.
IntentMatcher: (has action: is "android.intent.action.SEND" and has extras: has bundle …Run Code Online (Sandbox Code Playgroud) 这是我的测试课:
class RocketListVMTest {
@get:Rule
var instantTaskExecutorRule = InstantTaskExecutorRule()
private lateinit var sut: RocketListVM
private var activeOnlyToggle = false
private val repo: Repo = mock()
@Before
fun setUp() {
sut = RocketListVM(repo)
activeOnlyToggle = false
}
@Test
fun toggleActiveOnlyWithTrueCallsRepository() {
sut.toggleActiveOnly(true)
verify(repo).getActiveOnlyLocalRockets()
}
}
Run Code Online (Sandbox Code Playgroud)
具有以下依赖项:
androidTestImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito-inline:2.21.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'android.arch.core:core-testing:1.1.1'
Run Code Online (Sandbox Code Playgroud)
我已经src/androidTest/resources/mockito-extensions/org.mockito.plugins.MockMaker用mock-maker-inline里面创建了。
测试类失败,因为
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker …Run Code Online (Sandbox Code Playgroud) android-testing ×10
android ×9
androidx ×1
dagger-hilt ×1
dalvik ×1
gradle ×1
jacoco ×1
java ×1
mockito ×1