仅在 JVM 上测试 AndroidX 片段(不启动 emulator/device)

Testing AndroidX fragments on JVM only (without launching an emulator/device)

我正在寻找一种无需在模拟器中启动片段即可对 AndroidX 片段进行单元测试的方法。 Robolectric 允许我们 运行 对 JVM 中的活动进行单元测试,但我不确定 AndroidX 片段的等效方法是什么。

Google Android 最近引入了 FragmentScenario,它允许我们启动和测试图形片段,但这种方法在 device/emulator 上启动片段(类似于 ActivityScenario). Robolectric website 似乎认可这种启动片段的方式。

是否可以通过 Robolectric 或其他方式 - 运行 AndroidX 片段单元测试完全在 JVM 上进行(没有 emulator/device)?

看起来 fragmentScenario 可以在 Robolectric 测试中使用,它们 运行 在 JVM 上。见本文结尾: https://proandroiddev.com/isolated-fragments-unit-tests-that-run-both-instrumented-and-on-the-jvm-with-the-same-source-code-283db2e9be5d