Android 单元测试

一、浏览 Android 单元测试 官网

Android 的单元测试有两种方式:JUnit test(本地虚拟机测试) 和 instrumented test(设备测试)。

a JUnit test that runs on the local JVM or an instrumented test that runs on a device

Local unit testsLocated at module-name/src/test/java/ 可以本地虚拟机测试,只能调用Java API ,不能调用 Android API 。不过,可以通过依赖 android.jar 或者 第三方库(如:Mockito)调用 Android API。
Instrumented testsLocated at module-name/src/androidTest/java/ 在设备上运行,可以调试Android API,并可自定义测试配置文件。通过依赖第三方库(如:Espresso)可以实现 UI testing.

二、环境配置

the test library dependencies in your app module's build.gradle file:

dependencies {  
    // Required for local unit tests (JUnit 4 framework) 
    testCompile 'junit:junit:4.12' 
     // Required for instrumented tests  
    androidTestCompile 'com.android.support:support-annotations:24.0.0' 
    androidTestCompile 'com.android.support.test:runner:0.5'
}

单元测试在项目中的目录结构:


Paste_Image.png

配置问题:

  1. Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (23.3.0) and test app (23.1.1) differ
    解决方案 :参考官网

To use JUnit 4 test classes, make sure to specify AndroidJUnitRunner as the default test instrumentation runner in your project by including the following setting in your app's module-level build.gradle file:

android { 
   defaultConfig { 
       testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
   }
}

上面是配置,然后添加下面这句:

 android{
    configurations.all {  
        resolutionStrategy.force 'com.android.support:support-annotations:23.1.0'
    }
}

三、创建 Test 文件

Create a Local Unit Test Class
import org.junit.Test;
import java.util.regex.Pattern;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class EmailValidatorTest { 
   @Test 
   public void emailValidator_CorrectEmailSimple_ReturnsTrue() { 
       assertThat(EmailValidator.isValidEmail("name@email.com"), is(true)); 
   }  
  ...}
Create an Instrumented Unit Test Class
import org.junit.Before;import org.junit.Test;
import org.junit.runner.RunWith;
/**
 *   RxJava simple test
 * Created by ys on 2016/10/14 0014.
 */
@RunWith(AndroidJUnit4.class)
@SmallTest
public class RxJavaSimpleTest { 

   private RxJavaSimple mRxJavaSimple; 
   @Before 
   public void createRxJavaSimple(){ 
       mRxJavaSimple = new RxJavaSimple(); 
   }  
  @Test  
  public void testScheduler(){ 
        //打印 Log
       mRxJavaSimple.testScheduler(); 
   }
}

四、Run Test

To run your instrumented tests, follow these steps:

  1. Be sure your project is synchronized with Gradle by clicking Sync Project
    in the toolbar.
  2. Run your test in one of the following ways:
  • To run a single test, open the Project window, and then right-click a test and click Run
    .
  • To test all methods in a class, right-click a class or method in the test file and click Run
    .
  • To run all tests in a directory, right-click on the directory and select Run tests

三种方式:

  1. 运行单个 Test,右键 -> Run。
  2. 运行单个Test 文件,右键 class -> Run。
  3. 运行文件夹中所有Test,右键文件夹-> Run tests。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,663评论 2 45
  • 关键词:Android Testing, JUnit, Mockito, Espresso, Robolectri...
    sylcrq阅读 4,177评论 0 0
  • 夏日烈火照。近当午,酷热难忍乍逃,蝉声凄凄,叫卖声绝, 桃梨欲掉。微风轻拂热浪,亦是启唇难言笑。 看万物,难耐日晒...
    孤独者高阅读 1,430评论 0 0
  • 一般情况下,调用WebView.loadUrl(url)方法即可加载需要展示的Html页面了,如果需要传参数的话,...
    knock_knock阅读 14,892评论 0 2
  • 山间的小溪解冻了, 路边的小草变绿了, 田里的幼苗发芽了, 早晨的阳光更加温暖了, 夜间的风也变和煦了, 人们脱掉...
    原野杏子阅读 1,066评论 0 0