android studio 上junit4的使用

本文介绍了在Android Studio中配置和使用JUnit4进行单元测试的步骤。首先,通过SDK Manager安装Support Repository,然后在app的build.gradle文件中进行配置。接着,创建测试类并使用@RunWith注解来指定测试运行器。文章详细讲解了Categories和Suite的区别,并提供了运行单个测试类的方法。在执行测试时,可能会遇到找不到instrumentation info的问题,原因可能是defaultConfig缺少配置或Run/Debug Configuration设置不正确。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置:

1.Tool-->Android-->SDK Manager -->SDK Tools-->Support Repositoty. install 即可

2.app-->build gradle 配置:


android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"
    defaultConfig {
        applicationId "com.example.administrator.myapplication"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"  //junit4 相关配置
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    testCompile 'junit:junit:4.12'    //junit4相关配置
}

Junit4的使用:

1.新建一个类用于测试:

public class add {

    public int runadd(int a,int b){
        return a+b;
    }
}
2.新建测试类:@before @after 测试方法前后执行  @test 被执行的方法  @category类别器

import android.util.Log;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import static org.junit.Assert.*;

public class TestJunit {
    @Before
    public void testA(){
        Log.d("LC","before");
    }

    @Category(Categotysuite1.class) //category 类别器的名称
    @Test
    public void testB(){
        add A = new add();
        int r = A.runadd(2,2);
        assertNotEquals(r,3);
    }

    @Test
    public void testC(){
        add A = new add();
        int r = A.runadd(1,2);
        assertEquals(r,3);
    }

    @After
    public void testD(){
        Log.d("LC","after");
    }


}

3.runner类(打包测试类):

import org.junit.experimental.categories.Categories;
import org.junit.runner.RunWith;


import org.junit.runners.Suite;

@RunWith(Categories.class)  //指明以类别器运行,也可以指定为套件运行(Suite.class@Categories.IncludeCategory(CategorySuite1.class)   //
@Suite.SuiteClasses(TestJunit.class)
public class AllTestCases {
}


@RunWith(Categories.class)当指定以类别器运行时,只会运行有@Category的测试方法,同时会结合Categories.IncludeCategory(运行指定方法)和ExcludeCategory(去除此标签内的测试方法)进行过滤。

但是当@RunWith(Suite.class)指定以Suite运行是,Categories.IncludeCategory()和ExcludeCategory()会失效,会运行所有带@test标签的测试方法


可以用以下代码分别进行验证:

@RunWith(Suite.class)
@Categories.ExcludeCategory(CategorySuite1.class)
@Suite.SuiteClasses(TestJunit.class)
public class AllTestCases {
}

@RunWith(Categories.class)
@Categories.ExcludeCategory(CategorySuite1.class)
@Suite.SuiteClasses(TestJunit.class)
public class AllTestCases {
}

junit4 执行单个测试类执行,例如,可将光标定位到测试类TestJunit名称处,右击鼠标选择run,即可单独执行TestJunit类,而不需要每次都执行,方便调试


可能遇到的问题:

运行case的时候,如果没有执行case,提示:Test running failed: Unable to find instrumentation info for: ComponentInfo{com.umeng.message.example.test/android.test.InstrumentationTestRunner} Empty test suite.

可能的原因

1.在build.gradle 中,defaultConfig下,没有配置如下语句

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

2、在Run/Debug Configuration中,Specific instrumentation runner(optial) 中,添加的配置与第一步中的配置名称不一致

android.support.test.runner.AndroidJUnitRunner

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值