我最近迁移了我的项目以使用AndroidX,并使用以下文档在gradle上为我的espresso测试配置了测试编排器:
https://developer.android.com/training/testing/junit-runner#using-android-test-orchestrator
我有依赖:
androidTestUtil 'androidx.test:orchestrator:1.1.0-beta01'
Run Code Online (Sandbox Code Playgroud)
但是,我没有执行任何测试,看起来他们在运行gradle时失败运行以下adb shell命令,即:
adb shell 'CLASSPATH=$(pm path android.support.test.services) app_process / \
android.support.test.services.shellexecutor.ShellMain am instrument -w -e \
targetInstrumentation com.example.test/androidx.test.runner.AndroidJUnitRunner \
android.support.test.orchestrator/.AndroidTestOrchestrator'
Run Code Online (Sandbox Code Playgroud)
从上面看:似乎它试图用android支持版本而不是androidx版本执行此命令.
似乎没有记录什么用于androidx.
android android-testing android-espresso androidx androidx-test
在将代码和测试迁移到AndroidX之后,所有功能似乎都运行良好,但是由于NoClassDefFoundError:androidx / fragment / testing / R $ style异常,两个片段的Robolectric junit测试失败。堆栈跟踪:
java.lang.NoClassDefFoundError:androidx / fragment / testing / R $ style在androidx.fragment.app.testing.FragmentScenario $ EmptyFragmentActivity.onCreate(FragmentScenario.java:79)在android.app.Activity.performCreate(Activity.java:5933 )在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)在androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:674)在org.robolectric.android.controller.ActivityController.lambda $ create $ 0(ActivateController。 .java:69),位于org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:365),位于org.robolectric.android.controller.ActivityController.create(ActivityController.java:69),位于org.robolectric.android.controller。位于org.robolectric.android的ActivityController.create(ActivityController.java:74)。在androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:207)处的internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:39)在androidx.fragment.app.testing.FragmentScenario.internalLaunch(FragmentScenario.java:283)处在androidx.fragment.app.testing.FragmentScenario.launchInContainer(FragmentScenario.java:265)
为了测试片段,我正在使用FragmentScenario,似乎FragmentScenario.EmptyFragmentActivity指向包androidx.fragment.testing.R中缺少的R类:
setTheme(getIntent()。getIntExtra(THEME_EXTRAS_BUNDLE_KEY,R.style.FragmentScenarioEmptyFragmentActivityTheme));;
知道有什么问题吗?也许我错过了一个对我来说并不那么明显的依赖。
可在以下项目中重现:https : //github.com/marcinbak/androidx-test-error
还报告在Google的问题跟踪器中:https : //issuetracker.google.com/issues/122321150
最近我展示了一个关于 androidX 测试的谷歌 IO 视频,其中引用了“一次编写,到处运行”。这让我很高兴了解 androidX 测试库。
我发现经过很长时间谷歌为开/关设备的单元测试和仪器测试提出了很好的单一库。但是我发现在开/关设备上运行相同的测试有些困难。
基本上在 Android 中,我们创建了两个源根test/java,androidTest/java分别存储单元测试和仪器测试。单元测试在 JVM 上运行,Instrumentation 在设备/模拟上运行。
然后我为test/java目录中的片段之一编写了测试。
@RunWith(AndroidJUnit4::class)
class MyFragmenTest {
lateinit var scenario: FragmentScenario<MyFragment>
@Before
fun setUp() {
scenario = launchFragmentInContainer<MyFragment>()
}
@Test
fun `sample test`() {
scenario.onFragment {
// something
}
// some assertion
}
}
Run Code Online (Sandbox Code Playgroud)
所以当我使用小的绿色运行图标执行这个测试时,它在没有模拟器的 JVM 中运行这个测试,这很棒。但是要在设备上运行相同的测试,我必须移动此代码androidTest/java源根目录。
基本上我得到了相同的测试可以在任何地方运行,当我们使用 androidX 测试库时,您不必依赖不同的工具和库来完成相同的工作。
我试过的。
之后,在 google 上搜索我发现我们必须sharedTest/java使用下面的 gradle 行创建源根目录,以便它可以在设备上或设备外运行。
android {
...
sourceSets {
androidTest {
java.srcDirs += "src/sharedTest/java"
}
test …Run Code Online (Sandbox Code Playgroud) android android-testing android-jetpack androidx androidx-test
在运行完整的测试套件时,我经历了很多AppNotIdleException,但大多数情况下它们都是单独成功运行的(相关: https: //github.com/robolectric/robolectric/issues/7055)。我想确切地了解这两个 API 应如何组合以避免出现问题。
对于初学者来说,Robolectric 中有一个称为“Looper 模式”的配置,它基本上定义了何时执行不同的线程。从 Robolectric 4.5 开始,默认值是“PAUSED”,这意味着开发人员应该控制线程的执行时间。
此外,AndroidX 提供了createComposeRule(),这使得使用单个可组合项构建最小的单元测试成为可能 - 非常好。 此“撰写规则”附带一个“mainClock”,其默认行为为“自动前进”。
给出一个带有按钮的简单可组合项。我的测试的推荐设置(注释、时钟配置等)是什么?假设我希望测试针对 Android P (API=28) 运行。欢迎任何反馈。我想尽可能保持测试整洁。
这就是我今天编写测试的方式:
@Config(sdk = [Build.VERSION_CODES.P])
@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
class MyComposablesKtTest {
@get:Rule
val composeTestRule = createComposeRule()
private val buttonNode get() = composeTestRule.onNodeWithContentDescription("My Button")
@Before
fun setUp() {
composeTestRule.mainClock.autoAdvance = false
}
@Test
fun `MyComposable - …Run Code Online (Sandbox Code Playgroud) 我androidx.test在我的项目中使用库(我最近迁移到的)并使用自定义AndroidJUnitRunner. 迁移之前一切正常,但现在我收到此错误 -
Started running tests
Test running failed: Instrumentation run failed due to 'Process crashed.'
Empty test suite.
我使用的自定义跑步者类扩展自androidx.test.runner.AndroidJUnitRunner
在我的应用程序build.gradle文件中,我有以下设置 -
testInstrumentationRunner "com.example.CustomTestRunner"
具有依赖关系 -
androidTestImplementation "androidx.test.ext:junit:1.1.0"
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test:core:1.1.0'
androidTestImplementation "androidx.test:rules:1.1.1"
我所有的测试课都有@RunWith(androidx.test.ext.junit.runners.AndroidJUnit4.class)
我被困在这个问题上。任何帮助,将不胜感激。谢谢。
设置
背景
AndroidX 带来了测试片段的新方法:
来源:https : //developer.android.com/training/basics/fragments/testing
Robolectric 与 AndroidX 兼容,并打算弃用反映 AndroidX 功能的功能。
来源:http : //robolectric.org/androidx_test/
但是在 Robolectric 中,您可以测试选项菜单的行为,例如使用这样的东西(我知道它看起来很杂乱,但 FragmentController 在某些情况下不能很好地工作):
@Test
public void OnPrepareOptionsMenu_WhenX_ShowsMenuActionsCorrectly() {
setupX();
final Bundle instanceState = new Bundle();
instanceState.putString(FooActivity.ARG_UUID, x.getUuid());
final FooActivity activity = Robolectric.buildActivity(FooActivity.class)
.create(instanceState).start().visible().get();
activity.getSupportFragmentManager().beginTransaction()
.add(R.id.container_x_fragment_details, fragment).commit();
final Context context = fragment.requireContext();
final Menu menu = new RoboMenu(context);
fragment.onCreateOptionsMenu(menu, new MenuInflater(context));
fragment.onPrepareOptionsMenu(menu);
assertThat(menu.findItem(R.id.action_y).isVisible(), is(true));
assertThat(menu.findItem(R.id.action_z).isVisible(), is(true));
}
Run Code Online (Sandbox Code Playgroud)
在 AndroidX 中执行类似操作(不使用浓缩咖啡)的 API 是什么?该RoboMenu构造似乎不适用于 AndroidX,这不起作用:
fragmentScenario.onFragment(fragment -> { …Run Code Online (Sandbox Code Playgroud) 我在配置Android应用程序以进行AndroidX测试时遇到困难。
当我尝试运行初始测试时,出现此错误:-
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':app:processDebugAndroidTestManifest'.
> Could not resolve all task dependencies for configuration ':app:debugAndroidTestRuntimeClasspath'.
> Could not resolve com.google.guava:listenablefuture:1.0.
Required by:
project :app
> Cannot find a version of 'com.google.guava:listenablefuture' that satisfies the version constraints:
Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.ext:truth:1.1.0' --> 'com.google.guava:guava:27.0.1-android' --> 'com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava'
Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.espresso:espresso-contrib:3.1.1' --> 'androidx.core:core:1.1.0-alpha05' --> 'com.google.guava:listenablefuture:1.0'
Dependency path 'JayUnit:app:unspecified' --> 'androidx.test.espresso:espresso-contrib:3.1.1' --> 'androidx.core:core:1.1.0-alpha05' --> 'androidx.concurrent:concurrent-futures:1.0.0-alpha02' --> 'com.google.guava:listenablefuture:1.0' …Run Code Online (Sandbox Code Playgroud) 在我fragment-testing向我的项目添加依赖项之后:
// Testing dependencies
espressoVersion = '3.2.0-beta01'
testCoreVersion = '1.1.0'
runnerVersion = '1.1.0'
extJunitVersion = '1.1.0'
testRulesVersion = '1.1.0'
fragmentVersion = '1.1.0-alpha09'
orchestratorVersion = '1.1.0'
uiAutomatorVersion = '2.2.0'
junitVersion = '4.12'
mockitoVersion = '2.7.22'
robolectricVersion = '4.2.1'
liveDataTestingVersion = '1.1.0'
androidArchCoreTestingVersion = '2.0.0'
androidTestImplementation("androidx.test.espresso:espresso-core:$rootProject.ext.espressoVersion")
androidTestImplementation "androidx.test:core:$rootProject.ext.testCoreVersion"
androidTestImplementation("androidx.test:runner:$rootProject.ext.runnerVersion")
androidTestImplementation "androidx.test.ext:junit:$rootProject.ext.extJunitVersion"
androidTestUtil ("androidx.test:orchestrator:$rootProject.ext.orchestratorVersion")
androidTestImplementation("androidx.test.espresso:espresso-intents:$rootProject.ext.espressoVersion")
implementation "androidx.test.espresso:espresso-idling-resource:$rootProject.ext.espressoVersion"
androidTestImplementation "androidx.test.uiautomator:uiautomator:$rootProject.ext.uiAutomatorVersion"
testImplementation "junit:junit:$rootProject.ext.junitVersion"
androidTestImplementation("androidx.test:rules:$rootProject.ext.testRulesVersion")
// required if you want to use Mockito for unit tests
testImplementation "org.mockito:mockito-core:$rootProject.ext.mockitoVersion"
// required if you want to …Run Code Online (Sandbox Code Playgroud)