如何在 Android Studio 1.1 中运行简单的 JUnit4 测试? [英] How to run a simple JUnit4 test in Android Studio 1.1?

查看:44
本文介绍了如何在 Android Studio 1.1 中运行简单的 JUnit4 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an Android project that shows "Hello World". It was created from the "Blank Activity" template from Android Studio.

然后我在我的应用程序包(与我的活动相同的包)中添加/创建一个新的 java 类.我称之为Shape并添加一个简单的构造函数

I then add/create a new java class in my application package (the same package that has my activity). I call it Shape and add a simple constructor

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

太好了.现在我有一个根本不涉及 Android 的类,我想对其进行单元测试.接下来我该怎么办?

Great. Now I have a class that isn't touching Android at all, and I want to unit test it. What should I do next?

我的问题到此为止.下面我将介绍我的尝试.

  1. 在 Shape.java 中,我转到导航">测试"
  2. 按回车键选择创建新测试"
  3. 获取此弹出窗口,然后选择 JUNIT4.

  1. 然后我点击了修复按钮来修复未找到的库
  2. 我收到了这个弹出窗口

  1. 我不确定要选择什么,所以我选择了默认/突出显示.
  2. 我写我的测试

  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);
    }
}

  • 此时,我不确定如何运行我的测试,但尝试这样做:

  • At this point, I'm not really sure how to run my tests, but try to do this:

    我在运行时遇到这些错误

    I get these errors when running

    错误:(3, 17) Gradle: 错误: 包 org.junit 不存在
    错误:(5, 24) Gradle:错误:包 org.junit 不存在
    错误:(8, 6) Gradle:错误:找不到符号类测试

    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 开始,就有(实验性的)单元测试支持.来自该页面的几句话:

    Since Android Studio 1.1, there is (experimental) unit test support. A couple of quotes from that page:

    您必须在 build.gradle 中指定测试依赖项你的 android 模块的文件.例如:

    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"
    }
    

    要在 AS 中使用单元测试支持,您必须执行以下步骤:

    To use unit testing support in AS, you have to do the following steps:

    1. 更新 build.gradle 以使用 1.1.0-rc1 或更高版本的 android gradle 插件(手动在 build.gradle 文件中或在文件 > 项目结构的 UI 中)

    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)

    向 app/build.gradle 添加必要的测试依赖项(见上文).

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

    在设置">Gradle">实验性"中启用单元测试功能.

    Enable the unit testing feature in Settings > Gradle > Experimental.

    同步您的项目.

    打开构建变体"工具窗口(在左侧)并将测试工件更改为单元测试".

    Open the "Build variants" tool window (on the left) and change the test artifact to "Unit tests".

    为您的测试源代码创建一个目录,即 src/test/java.您可以从命令行或使用项目工具窗口中的项目视图.新目录应该是此时以绿色突出显示.注:测试源名称目录由 gradle 插件根据约定确定.

    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.

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

    It is important to know that there are two test types: androidTest and plain test.

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

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

    As stated in the quote, you switch between androidTest and test in Android Studio by changing the test artifact.

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

    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. This lets you move more tests from androidTest to test.

    这篇关于如何在 Android Studio 1.1 中运行简单的 JUnit4 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆