在Android Studio中的单元测试 [英] Unit testing in android studio

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

问题描述

我采用了android的gradle工作室1.2.3插件的最新版本。我无法了解如何创建它的单元测试;所有可用的答案是旧版本的Andr​​oid。帮帮我吧。

I am using newest version of android studio gradle plugin 1.2.3. I am unable to understand how to create unit tests in it; all the available answers are for older version of android. Help me.

推荐答案

首先你需要一个不同的文件夹结构单元测试的。
机器人工作室自动generats为仪器测试androidTest文件夹,但你不能把你的单元测试在那里。所以你必须创建一个测试文件夹:

first of all you need a different folder structure for your unit tests. android studio automatically generats the androidTest folder for instrumentation tests, but you can't put your unit tests in there. so you have to create a "test" folder:

- src
  -- androidTest //for instrumentation tests
  -- main        //source code
  -- test        //for unit tests

使用相同的封装结构作为你的测试为你要测试的类。

use the same package structure for your tests as for the class you want to test.

您可以在Android的仪器测试和单元测试之间的机器人工作室的构建变种切换。

you can switch in your build variants of android studio between Android Instrumentation Tests and Unit Tests.

根据您的选择测试或androidTest文件夹将显示项目中的标签。

depending on your selection test or androidTest folder will be show in your project tab.

最后你不得不在的JUnit的gradle添加到您的依赖关系:

finally you have to add junit to your dependencies in gradle:

dependencies {
  testCompile 'junit:junit:4.12'
}

在您的测试文件夹都可以测试类比如像这样:

the test classes in your test folder can for example look like this:

package package.of.class.to.test

import org.junit.Before;
import org.junit.Test;
...
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

public class TestSomething{

  @Before
  public void setup(){
    // test setup
  }

  @Test
  public void testSomething(){
    // your unit tests like these simple examples
    assertThat(anyBooleanResult, is(expectedBooleanResult));
    assertEquals(anyIntResult, expectedIntResult);
  }
}

有关更多信息,您还可以对<一看href=\"http://stackoverflow.com/questions/28843304/how-to-run-a-simple-junit4-test-in-android-studio-1-1\">this螺纹。

for further information you can also take a look on this thread.

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

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