如何让 Android Studio 将文件识别为源(测试)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21927447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:19:16  来源:igfitidea点击:

How to get Android Studio to recognize file as source (test)

androidjunitintellij-ideagradleandroid-studio

提问by David T.

I'm trying to create Robolectric tests for an android project (heck, i'd be happy to even make them unit tests)

我正在尝试为一个 android 项目创建 Robolectric 测试(哎呀,我什至很乐意让它们进行单元测试)

I have the folder directory as:

我的文件夹目录为:

MyApp
    - app
        - src
            - main
                - java
                    - com.myapp
                        HelloWorld
            - test
                - java
                    - com
                        - myapp
                            HelloWorldTest.java

The problem is that HelloWorldTest.javacan't be run because it's not being recognized as source. how do i set it up so that i can run this class as a test?????

问题是HelloWorldTest.java无法运行,因为它没有被识别为源。我如何设置它以便我可以运行这个类作为测试???????

if i try to do CMD + SHIFT + T(shortcut for creating tests), it prompts to create the tests under the same directory as my source file and i do NOT want that

如果我尝试这样做CMD + SHIFT + T(创建测试的快捷方式),它会提示在与我的源文件相同的目录下创建测试,我不希望那样

回答by Ben Clayton

In Android Studio 1.0 the scheme has changed a little bit.

在 Android Studio 1.0 中,方案发生了一些变化。

Your path should be (app)/src/androidTest/java/com/myapp/HelloWorldTest.java

你的路径应该是 (app)/src/androidTest/java/com/myapp/HelloWorldTest.java

Here's how I set up Unit Tests in a new Android Studio project:

以下是我在新的 Android Studio 项目中设置单元测试的方法:

  • Open app in Android Studio.
  • Set the Project explorer (left hand window) to display 'Project' mode. Tap the little drop-down at the top left and select 'Project'.
  • Right click the 'src' directory, 'New -> Directory'.
  • Call new directory androidTest
  • Right click androidTest and add a 'java' directory. It will appear in green (indicating it's a test src directory).
  • Now right-click again and add a package, e.g. com.mycompany.myapp.tests
  • Add a new class that extends AndroidTestCase.
  • 在 Android Studio 中打开应用程序。
  • 设置项目浏览器(左侧窗口)以显示“项目”模式。点击左上角的小下拉菜单并选择“项目”。
  • 右键单击“src”目录,“新建 -> 目录”。
  • 调用新目录 androidTest
  • 右键单击 androidTest 并添加一个“java”目录。它将显示为绿色(表示它是一个测试 src 目录)。
  • 现在再次右键单击并添加一个包,例如 com.mycompany.myapp.tests
  • 添加一个扩展 AndroidTestCase 的新类。

Now add a new configuration for testing:

现在添加一个新的测试配置:

  • Edit Configurations
  • Click + at top left
  • Select Android Tests
  • In General Tab select your main module.
  • Hit OK
  • 编辑配置
  • 点击左上角的 +
  • 选择 Android 测试
  • 在常规选项卡中选择您的主模块。
  • 点击确定

Now you can run the tests.

现在您可以运行测试了。

回答by TTransmit

In Android Studio 1.3:

在 Android Studio 1.3 中:

Build > Select Build Variant

构建 > 选择构建变体

In the Build Variants window, select Unit Testsas the Test Artifact.

在 Build Variants 窗口中,选择Unit Tests作为 Test Artifact。

回答by pyus13

Follow the guidelines available here :

请遵循此处提供的指南:

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing

and here

和这里

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Configuring-the-Structure

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Configuring-the-Structure

Summary :

概括 :

As the first link says you have to rename your testdirectory as instrumentTestso Studio can automatically detects the sourceSets for your test project

正如第一个链接所说,您必须重命名您的test目录,instrumentTest以便 Studio 可以自动检测您的测试项目的 sourceSets

or

或者

Alternate is you can have your testsdirectory in root(with you main directory) and sources in a manner like tests/java, as the second link says

或者,您可以将tests目录放在根目录中(与您的主目录一起),并以类似的方式提供源tests/java,如第二个链接所述

instrumenttest.setRoot("tests")

in sourceSets configuration under android tag

在 android 标签下的 sourceSets 配置中

From the document

从文件

Note: setRoot() moves the whole sourceSet (and its sub folders) to a new folder. This moves src/instrumentTest/* to tests/* This is Android specific and will not work on Java sourceSets.

注意:setRoot() 将整个 sourceSet(及其子文件夹)移动到一个新文件夹中。这会将 src/instrumentTest/* 移动到 tests/* 这是 Android 特定的,不适用于 Java sourceSets。

回答by mc.dev

I had similar problem for a long time and finally I solved that.

我有很长一段时间类似的问题,最后我解决了。

To make Android Studio to recognize src/test dir as source you have to pick 'Unit Tests' in 'Build Variants'->'Test Artifact:'

要使 Android Studio 将 src/test 目录识别为源,您必须在“Build Variants”->“Test Artifact:”中选择“Unit Tests”

However, in my project I hadn't had 'Unit Tests' I only had 'Android Instrumentation tests'. This is two different types of unit-tests in Android.

但是,在我的项目中,我没有“单元测试”,我只有“Android Instrumentation 测试”。这是 Android 中两种不同类型的单元测试。

See: http://developer.android.com/training/testing/unit-testing/index.html
1. Unit Tests - Local Unit Tests
2. Android Instrumentation tests - Instrumented Unit Tests

请参阅:http: //developer.android.com/training/testing/unit-testing/index.html
1. 单元测试 - 本地单元测试
2. Android 仪器测试 - 仪器单元测试

That is not obvious at all but inside 'Build Variants'->'Test Artifact:' you does not have 'Unit Tests' option when you have multiple modules in your project.

这一点都不明显,但在“构建变体”->“测试工件:”中,当您的项目中有多个模块时,您没有“单元测试”选项。

My project dir looked like this:
:project
-:module1
-:module2
-:module3
-:module4
-:module5
-settings.gradle -> include ':module1',':module2', ...
These modules are not really linked to each other, but it is how it has to be for many other developers who came from Eclipse. That was a problem.

我的项目目录如下所示:
:project
-:module1
-:module2
-:module3 -:module4
-
:module5 -settings.gradle
-> include ':module1',':module2', ...
这些模块没有真正链接对彼此而言,但对于来自 Eclipse 的许多其他开发人员来说,它必须如此。那是个问题。

Then I created separate project for my module only:
:project
-:module1
-settings.gradle -> include ':module1'

然后我只为我的模块创建了单独的项目:
:project
-:module1
-settings.gradle -> include ':module1'

After that 'Unit Tests' appeared in 'Build Variants'->'Test Artifact:' and I can choose between Local Unit Tests and Android Instrumentation tests now.

之后“单元测试”出现在“构建变体”->“测试工件:”中,我现在可以在本地单元测试和 Android Instrumentation 测试之间进行选择。