• 使用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 {  
        
    }  
  • 相关阅读:
    HTML5印章绘制电子签章图片,中文英文椭圆章、中文英文椭圆印章
    送别2019,期待2020!
    [系列] Go
    当查询的数据来自多个数据源,有哪些好的分页策略?
    究竟什么样的开发流程是规范的?
    Git 分支设计规范
    API 接口设计规范
    一线技术管理者究竟在管什么事?
    一个人被提拔,不仅仅是能力,而是信任
    [系列] Go 如何解析 JSON 数据?
  • 原文地址:https://www.cnblogs.com/pasco/p/3739121.html
Copyright © 2020-2023  润新知