Android中的单元测试? [英] Unit Testing in Android?

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

问题描述

我正在使用Android Studio 1.2

I am using Android Studio 1.2

说我有一个简单的方法添加到MyAdder类中

Say I have a simple method add in some class MyAdder

public int add(int a,int b) {
    return a+b;
}

我想执行单元测试,并使用断言对上述代码进行测试.

I want to perform the unit test and use assertions to perform the test for the above-mentioned code.

首先,我发现很难从DEV官方站点上的"Testing Fundamentals"(测试基础知识)开始工​​作,因此不胜感激示例代码或用于执行单元测试的详细教程.

I found it tough, to begin with, Testing Fundamentals from the official DEV site so a sample code or a detailed tutorial for performing unit tests would be appreciated.

推荐答案

支持两种类型的测试,可从Android Studio Build Variants工具窗口的下拉菜单中找到:

Two types of tests are supported, available in a drop-down menu in the Build Variants tool window of Android Studio:

  1. Android Instrumentation测试:使用设备或模拟器上,通常称为Android测试
  2. 单元测试:普通在提供了存根android.jar的本地JVM上运行的JUnit测试
  1. Android Instrumentation Tests: integration/functional testing using a running app on a device or on an emulator, often referred to as Android Tests
  2. Unit Tests: plain JUnit tests that run on a local JVM, with a stubbed android.jar provided

测试基础页面主要讨论了Android Instrumentation测试,其中包括您注意到,入门有点困难.

The Testing Fundamentals page mostly discusses Android Instrumentation Tests, with which, as you noted, is a bit tough to get started.

但是,对于您的问题,您只需要单元测试.

For your question, however, you just need Unit Tests.

来自单元测试支持页面:

  1. 更新build.gradle以使用android gradle插件版本1.1.0-rc1或更高版本(可以在build.gradle文件中手动操作,也可以在用户界面中的File> Project Structure中)
  2. 向app/build.gradle添加必要的测试依赖项

dependencies {
  testCompile "junit:junit:4.12"
}

  1. 在设置">"Gradle">实验"中启用单元测试功能. (已启用,并且从Android Studio 1.2开始不再处于实验状态)
  2. 同步您的项目.
  3. 打开构建变体"工具窗口(在左侧),然后将测试工件更改为单元测试".
  4. 为您的测试源代码创建一个目录,即src/test/java.您可以从命令行或使用项目"工具窗口中的项目"视图来执行此操作.此时,新目录应以绿色突出显示.注意:测试源目录的名称由gradle插件根据约定确定.
  1. Enable the unit testing feature in Settings > Gradle > Experimental. (enabled and no longer experimental as of Android Studio 1.2)
  2. Sync your project.
  3. Open the "Build variants" tool window (on the left) and change the test artifact to "Unit tests".
  4. 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.

下面是一些示例代码,用于测试您的问题中的实例方法(将com/domain/appname替换为您的包名称所创建的路径):

Here is some sample code to test the instance method in your question (replace com/domain/appname with the path created by your package name):

项目名称/app/src/test/java/com/domain/appname/MyAdderTest.java

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class MyAdderTest {
    private MyAdder mMyAdder;

    @Before
    public void setUp() throws Exception {
        // Code that you wish to run before each test
        mMyAdder = new MyAdder();
    }

    @After
    public void tearDown() throws Exception {
        // Code that you wish to run after each test
    }

    @Test
    public void testAdd() {
        final int sum = mMyAdder.add(3, 5);
        assertEquals(8, sum);        
    }
}

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

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