简体   繁体   English

在 Android Studio 中运行所有单元测试

[英]Run all unit tests in Android Studio

I have this project in Android Studio :我在 Android Studio 中有这个项目:

在此处输入图片说明

I wish to run all unit tests in all project with one click.我希望一键运行所有项目中的所有单元测试。

How i can do it ?我该怎么做?

Not sure what version of Android Studio this was added in, but I'm using Android Studio 3.4 .不确定这是在哪个版本的 Android Studio 中添加的,但我使用的是Android Studio 3.4

安卓工作室 3.4 版

In the Project Explorer Window, select the Project View .在 Project Explorer 窗口中,选择Project View

项目视图选择

Right click on your root project and select "Create 'All Tests'..."右键单击您的根项目并选择“创建'所有测试'...”

创建“所有测试”

... this window will appear (these defaults need to be changed) ...将出现此窗口(这些默认值需要更改)

默认编辑配置窗口

Change the following options:更改以下选项:

  • Search for tests:搜索测试:

    • In whole project在整个项目中
  • JRE: JRE:

    • Most recent version of Android API __ Platform that you have available in this menu (for me, Android API 28 Platform您在此菜单中可用的最新版本的 Android API __ 平台(对我而言, Android API 28 平台
  • Click Apply点击应用

编辑测试配置

  • Select "All Tests" from the drop down menu从下拉菜单中选择“所有测试”

在此处输入图片说明

  • View output from all your tests查看所有测试的输出

在此处输入图片说明

First, you can list all the test tasks available in your project with首先,您可以列出项目中可用的所有测试任务

./gradlew tasks

Then you can choose the tasks you want to execute.然后您可以选择要执行的任务。 If you want to execute all tests for all flavors ans all buildTypes, you just have to run如果你想为所有风格和所有构建类型执行所有测试,你只需要运行

./gradlew test connectedAndroidTest

If you don't want to remember all the gradle test command each time you want to run the tests, you can create a file "custom_tasks.gradle" and add如果不想在每次运行测试时记住所有 gradle test 命令,可以创建一个文件“custom_tasks.gradle”并添加

task testAll(dependsOn: ['test', 'connectedAndroidTest']) {
   group = 'custom_tasks'
   description = "Run all tests"
}

Then, you just have to run然后,你只需要运行

./gradlew testAll
  1. In "Project" panel (CMD+1 to show) Right click on "java".在“项目”面板(显示 CMD+1)中右键单击“java”。
  2. Click "Run tests in Java单击“在 Java 中运行测试

You can try to use this command line on the android terminal: Mac:您可以尝试在 android 终端上使用此命令行:Mac:

./gradlew test connectedAndroidTest

Windows:视窗:

gradlew test connectedAndroidTest

Test from the command line 从命令行进行测试

Running unit tests with Gradle 使用Gradle运行单元测试

Local unit test 本地单元测试

./gradlew test
./gradlew :<moduleName>:test<variantName>UnitTest --tests <pattern>

Instrumented unit test 仪表单元测试

./gradlew connectedAndroidTest
./gradlew :<moduleName>:connected<variantName>AndroidTest -Pandroid.testInstrumentationRunnerArguments.class=<package.TestClassName>

Read more here , here 这里阅读更多内容

If the question is about running by the android studio test runner.. No it's not possible, still now, as I write this but you can have gradle test task in run configurations and run them just by pressing button. 如果问题是关于由android studio测试运行器运行..不,现在仍然不可能,因为我写这个但你可以在运行配置中进行gradle测试任务,只需按下按钮即可运行它们。

在此输入图像描述

You can open gradle side panel, open root project tasks and find test task in verification task folder and click it. 您可以打开gradle侧面板,打开根项目任务并在验证任务文件夹中找到测试任务并单击它。 You can also assign shortcut to it or save it to run configurations. 您也可以为其指定快捷方式或将其保存为运行配置。 Still this will run it in Run panel as gradle task. 仍然会在运行面板中将其作为gradle任务运行。

(I was looking for a way how to run all tests from inside of android test runner, not command line or gradle task but found out this) (我一直在寻找一种方法如何从android测试运行器内部运行所有测试,而不是命令行或gradle任务,但发现了这一点)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM