首页 技术 正文
技术 2022年11月14日
0 收藏 649 点赞 2,902 浏览 1274 个字

加入单元测试

android单元测试很多都是使用Instrumentation进行的,这里讲的是试用JUnit,为什么用JUnit呢?因为使用Instrumentation需要打包apk安装,然后再进行测试,即需要用一个项目去测试另一个项目。我现在想做的单元测试是在打包前进行一些测试验证,所以需要用JUnit。至于使用Instrumentation的单元测试,请看这里

maven默认的源代码src目录和测试代码test目录的结构是

src/main/java

test/java

实际使用中发现以下目录结构maven也可以使用

src/

test/

这个应该是因为项目源代码和test代码需要分开编译,在target中也是分开放置的。如果直接用eclipse的android项目结构(在eclipse中test需要放在src下才能运行)放到maven中运行,会出现类似package junit.framework does not exist 的错误,所以需要在构建前执行命令将test目录移动到src之外。

单元测试代码,新的JUnit中,测试方法已经不需要用test开头,只需加上 @Test。

package com.example.demo;
import com.example.util.PublicData;
import junit.framework.Assert;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;@RunWith(JUnit4.class)
public class TestUrl { @Test
public void prodApi() {
String equals = PublicData.HTTP_URL;
Assert.assertEquals("http://prod.xxx.com", equals);
} @Test
public void devApi() {
String equals = PublicData.HTTP_URL;
Assert.assertEquals("http://dev.xxx.com", equals);
}
/*
@Test
@Ignore
public void thisIsIgnored() {
}
*/
}

使用Maven进行编译打包,会将test目录中test开头的类视为测试类,如果使用 testsuite,testsuite的类名用Test开头,testcase的类名不需要用test开头,避免重复被执行。

package com.example.demo;import org.junit.runner.RunWith;
import org.junit.runners.Suite;@RunWith(Suite.class)
@Suite.SuiteClasses({
CheckAppInfo1.class,
CheckAppInfo2.class
})public class TestGroup { }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,861