使用Jenkins进行android项目的自动构建(4)

加入单元测试

 

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 {  
    
}  

 

posted on 2014-05-23 16:27  pasco  阅读(429)  评论(0编辑  收藏  举报

导航