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

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

我有一个显示 "Hello World" 的 Android 项目。它是根据 Android Studio 的 "Blank Activity" 模板创建的。

然后我 add/create 在我的应用程序包中添加一个新的 java class(与我的 activity 相同的包)。我称之为 Shape 并添加一个简单的构造函数

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

太棒了。现在我有一个 class 根本没有触及 Android,我想对其进行单元测试。接下来我该做什么?

我的问题到此为止。下面我将介绍我尝试过的方法。

请注意,我之前确实从未在 Android 或 Java 中测试过。 "rookie" 错误请见谅。

  1. 在 Shape.java 我去 "Navigate" > "Test"
  2. 回车进入 select "Create new Test"
  3. 获取此弹出窗口,然后select JUNIT4。

  1. 然后我点击修复按钮修复找不到库
  2. 我收到这个弹出窗口[=52​​=]

  1. 我不太确定要 select,所以我 select default/highlighted。
  2. 我写我的测试

    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. 在这一点上,我不太确定如何 运行 我的测试,但请尝试这样做:

  4. 我在 运行ning

    时遇到这些错误

    Error:(3, 17) Gradle: error: package org.junit does not exist
    Error:(5, 24) Gradle: error: package org.junit does not exist
    Error:(8, 6) Gradle: error: cannot find symbol class Test

从 Android Studio 1.1 开始,有(实验性的)unit test support。该页面的几条引述:

You will have to specify your testing dependencies in the build.gradle file of your android module. 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:

  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)

  2. Add necessary testing dependencies to app/build.gradle (see above).

  3. Enable the unit testing feature in 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, i.e. 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.

  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. 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).

重要的是要知道有两种测试类型:androidTest 和普通 test

  • androidTest 主要用于在模拟器或设备上 运行 测试,例如仪器测试。在命令行中,您使用 ./gradlew connectedCheck 到 运行 这些。
  • test 用于您不想在设备上 运行 的测试,例如您编写的单元测试。你运行./gradlew test来运行这些测试。

如引用中所述,您可以通过更改测试工件在 Android Studio 中的 androidTesttest 之间切换。

当然,最好不要 运行 在设备或模拟器上进行测试,因为这会大大加快测试过程 。借助新的实验性单元测试支持,您无需使用设备即可访问已存根的 Android API。这使您可以将更多测试从 androidTest 移动到 test

如今 Android Studio(当前版本 1.4)具有完整的单元测试支持,没有任何解决方法。正如自动生成的 ExampleUnitTest 中所建议的那样:

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

对于 android studio 1.2 或更高版本,我包含了这个答案,因为这是 google 的第一个排名之一,这是一个关于如何设置单位的优秀且非常容易遵循的教程使用 Android Studio 进行测试,这是 link:https://io2015codelabs.appspot.com/codelabs/android-studio-testing#1

在浪费了 2 小时尝试 运行 测试后,我终于用上面的方法做到了 link,希望它对你和我一样有用。

转到设置,然后构建工具,然后 gradle 然后进行实验。在实验中取消选中启用所有测试工件。游戏结束