Android studio创建单元测试

在Android 项目开发中,需要对功能函数,api接口进行本地测试,于是需要android本地的测试,
首先,创建测试文件,在项目src目录下,创建androidtest,在这里插入图片描述
在Android项目中如下图
在这里插入图片描述
创建测试类,ExampleInstrumentedTest.java代码如下

package org.xmstring.www.wallet;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import android.util.Log;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

    /**
     * 测试所用flag
     */
    private static final String testFlag = "wjltest";
    @Test
    public void useAppContext() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("org.xmstring.www.wallet", appContext.getPackageName());
    }
 }

在项目目录的文件夹中的build.gradle,增加测试包的依赖,

defaultConfig {
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

运行测试
在这里插入图片描述
测试结果

在这里插入图片描述
测试完成,采用Android本地测试,可以调用Android系统的api,但是相对测试速度比较慢。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值