简体   繁体   English

如何在Android Studio 1.1中运行简单的JUnit4测试?

[英]How to run a simple JUnit4 test in Android Studio 1.1?

I have an Android project that shows "Hello World". 我有一个显示“Hello World”的Android项目。 It was created from the "Blank Activity" template from Android Studio. 它是从Android Studio的“空白活动”模板创建的。

I then add/create a new java class in my application package (the same package that has my activity). 然后我在我的应用程序包中添加/创建一个新的java类(与我的活动相同的包)。 I call it Shape and add a simple constructor 我称之为Shape并添加一个简单的构造函数

public class Shape {
    public Shape(int i){
        if (i==0){
            throw new IllegalArgumentException("Cant have 0");
        }
    }
}

Great. 大。 Now I have a class that isn't touching Android at all, and I want to unit test it. 现在我有一个完全没有触及Android的课程,我想对它进行单元测试。 What should I do next? 接下来我该怎么办?

This is where my question stops. 这是我的问题停止的地方。 Below I'll go through what I tried. 下面我将介绍我尝试过的内容。

Please note that I really have never tested before in Android or Java. 请注意,我以前从未在Android或Java中测试过。 Excuse me for "rookie" mistakes. 对不起“菜鸟”的错误。

  1. While in the Shape.java I go to "Navigate" > "Test" 在Shape.java中,我转到“导航”>“测试”
  2. Hit enter to select "Create new Test" 按Enter键选择“创建新测试”
  3. Get this popup, and select JUNIT4. 获取此弹出窗口,然后选择JUNIT4。

在此输入图像描述

  1. I then hit the fix button to fix the library not being found 然后我点击修复按钮修复未找到的库
  2. I get this popup 我得到这个弹出窗口

在此输入图像描述

  1. I'm not really sure what to select, so I select the default/highlighted. 我不确定要选择什么,所以我选择默认/突出显示。
  2. I write my test 我写了我的考试

     package com.eghdk.getjunit4towork; import org.junit.Test; import static org.junit.Assert.*; public class ShapeTest { @Test(expected = IllegalArgumentException.class) public void testShapeWithInvalidArg() { new Shape(0); } } 
  3. At this point, I'm not really sure how to run my tests, but try to do this: 此时,我不确定如何运行我的测试,但尝试这样做: 在此输入图像描述

  4. I get these errors when running 我跑步时遇到这些错误

    Error:(3, 17) Gradle: error: package org.junit does not exist 错误:(3,17)Gradle:错误:包org.junit不存在
    Error:(5, 24) Gradle: error: package org.junit does not exist 错误:(5,24)Gradle:错误:包org.junit不存在
    Error:(8, 6) Gradle: error: cannot find symbol class Test 错误:(8,6)Gradle:错误:找不到符号类Test

Since Android Studio 1.1, there is (experimental) unit test support . 自Android Studio 1.1起,就有(实验性) 单元测试支持 A couple of quotes from that page: 该页面的几个引用:

You will have to specify your testing dependencies in the build.gradle file of your android module. 您必须在Android模块的build.gradle文件中指定测试依赖项。 For example: 例如:

 dependencies { testCompile 'junit:junit:4.12' testCompile "org.mockito:mockito-core:1.9.5" } 

To use unit testing support in AS, you have to do the following steps: 要在AS中使用单元测试支持,您必须执行以下步骤:

  1. Update build.gradle to use the android gradle plugin version 1.1.0-rc1 or later (either manually in build.gradle file or in the UI in File > Project Structure) 更新build.gradle以使用android gradle插件版本1.1.0-rc1或更高版本(在build.gradle文件中手动或在File> Project Structure中的UI中)

  2. Add necessary testing dependencies to app/build.gradle (see above). 向app / build.gradle添加必要的测试依赖项(参见上文)。

  3. Enable the unit testing feature in Settings > Gradle > Experimental. 在Settings> Gradle> Experimental中启用单元测试功能。

  4. Sync your project. 同步您的项目。

  5. Open the "Build variants" tool window (on the left) and change the test artifact to "Unit tests". 打开“构建变体”工具窗口(左侧)并将测试工件更改为“单元测试”。

  6. Create a directory for your testing source code, ie src/test/java. 为测试源代码创建一个目录,即src / test / java。 You can do this from the command line or using the Project view in the Project tool window. 您可以从命令行或使用“项目”工具窗口中的“项目”视图执行此操作。 The new directory should be highlighted in green at this point. 此时,新目录应以绿色突出显示。 Note: names of the test source directories are determined by the gradle plugin based on a convention. 注意:测试源目录的名称由gradle插件根据约定确定。

  7. Create your test. 创建您的测试。 You can do this by opening a class, right-clicking its name and selecting "Go to > Test". 您可以通过打开一个类,右键单击其名称并选择“转到>测试”来完成此操作。 Add some test cases. 添加一些测试用例。
  8. Right click your new test class or method and select "Run ...". 右键单击新的测试类或方法,然后选择“运行...”。
  9. (Optional) You can decrease the compilation time by using Gradle directly. (可选)您可以直接使用Gradle缩短编译时间。 To do this, go to the Run menu and select "Edit configurations". 为此,请转到“运行”菜单并选择“编辑配置”。 There, find the default JUnit template, remove the "Make" before-launch step and add a "Gradle aware make" step instead (leave the task name empty). 在那里,找到默认的JUnit模板,在启动前删除“Make”步骤并添加“Gradle aware make”步骤(将任务名称保留为空)。

It is important to know that there are two test types: androidTest and plain test . 重要的是要知道有两种测试类型: androidTest和普通test

  • androidTest is primarily for tests you run on an emulator or device, such as instrumentation tests. androidTest主要用于在模拟器或设备上运行的测试,例如检测测试。 From the command line, you use ./gradlew connectedCheck to run these. 在命令行中,使用./gradlew connectedCheck来运行它们。
  • test is for tests you don't want to run on a device, such as the unit test you wrote. test适用于您不想在设备上运行的测试,例如您编写的单元测试。 You run ./gradlew test to run these tests. 您运行./gradlew test来运行这些测试。

As stated in the quote, you switch between androidTest and test in Android Studio by changing the test artifact. 如引用中所述,您可以通过更改测试工件在Android Studio中的androidTesttest之间切换。

Naturally, it is preferred to not run tests on a device or emulator, since this speeds up the testing process a lot . 当然,优选的是不运行的设备或仿真器上的测试,因为这加速了测试过程很多 With the new experimental unit test support, you gain access to stubbed Android API's without using a device. 通过新的实验单元测试支持,您可以在不使用设备的情况下访问存根的Android API。 This lets you move more tests from androidTest to test . 这使您可以从androidTest移动更多测试进行test

For android studio 1.2 or greater, I include this answer since this is one of the first ranking at google and this is an excelent and VERY easy to follow tutorial on how to set unit tests with Android Studio, this is the link: https://io2015codelabs.appspot.com/codelabs/android-studio-testing#1 对于android studio 1.2或更高版本,我包括这个答案,因为这是谷歌的第一个排名之一,这是一个非常简单,非常容易遵循如何使用Android Studio设置单元测试的教程,这是链接: https:/ /io2015codelabs.appspot.com/codelabs/android-studio-testing#1

After wasting 2 hours trying to run test I finally did it with the above link, hope it is as useful for you as for me. 在浪费了2个小时试图进行测试后,我终于用上面的链接做了,希望它对你和我一样有用。

Nowadays Android Studio (current ver. 1.4) has full Unit test support without any workarounds. 如今Android Studio(当前版本1.4)具有完整的单元测试支持,没有任何解决方法。 Just as suggested in the automatically generated ExampleUnitTest: 正如自动生成的ExampleUnitTest中所建议的那样:

To work on unit tests, switch the Test Artifact in the Build Variants view.

屏幕截图

Go to settings then build tools then gradle and then experimental. 转到设置,然后构建工具,然后进行gradle,然后进行实验。 In experimental uncheck enable all test artifacts. 在实验性取消选中启用所有测试工件。 Thats it game over 多数民众赞成游戏结束

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

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