Android之单元测试

本文地址:http://blog.csdn.net/qq_16628781/article/details/61202111

知识点:

1、单元测试;


常见测试方法有以下几个:

黑盒测试: 是以用户的角度,从输入数据与输出数据的对应关系出发进行测试的。

白盒测试: 又称结构测试、透明盒测试、逻辑驱动测试或基于代码的测试。

单元测试: 又称模块测试,是开发者编写的一小段代码,用于检验被测代码的一个很小的、很明确的功能是否正确。

功能测试: 根据产品特性、操作描述和用户方案,测试一个产品的特性和可操作行为以确定它们满足设计需求。

压力测试: 主体向被观察者布置一定量任务和作业,借以观察个体完成任务的行为。

集成测试: 是单元测试的逻辑扩展。它的最简单的形式是:两个已经测试过的单元组合成一个组件,并且测试它们之间的接口

备注:黑盒测试,不看代码结构,从用户的角度测试功能,例如玩某一款游戏来测试。白盒测试:通过代码测试功能。


来说说Android中的单元测试,在建立项目的时候,IDE默认给我们建立了两个测试的文件,一个是可以在IDE里头直接测试的,另外一个是可以要运行在Android手机上面的。如下图所示


其中AndroidTest是需要在Android手机上运行才能跑的。

而test则是可以运行在IDE中的。

下面我们来看看这两个文件

首先是ExampleInsrumentedTest类

/**
 * Instrumentation test, which will execute on an Android device.
 * 运行在Android设备上的单元测试
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() throws Exception {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getTargetContext();

        assertEquals("com.yaojt", appContext.getPackageName());
    }

    @Test
    public void addTest() {
        HomeActivity homeActivity = new HomeActivity();
        int count = homeActivity.add(1, 2);
        System.out.println("count on dvice: " + count);
    }
}

ExampleUnitTest类:

/**
 * Example local unit test, which will execute on the development machine (host).
 * 运行在开发机器上的本地单元测试
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
public class ExampleUnitTest {
    @Test
    public void addition_isCorrect() throws Exception {
        assertEquals(4, 2 + 2);
    }

    //@Test是修饰符,必须的
    @Test
    public void addTest() {
        HomeActivity homeActivity = new HomeActivity();
        int count = homeActivity.add(1, 2);
        System.out.println("count: " + count);
    }

}


我们运行IDE里头可以跑得单元测试,效果如图所示:



这里有一个小小的错误,就是当我引用我自己写的log类的时候,会出现这个问题:

java.lang.RuntimeException: Method i in android.util.Log not mocked. See http://g.co/androidstudio/not-mocked for details.

	at android.util.Log.i(Log.java)
	at com.yaojt.utils.LogUtil.logInfo(LogUtil.java:21)
	at com.yaojt.ExampleUnitTest.addTest(ExampleUnitTest.java:27)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:497)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)


因为我使用了

LogUtil.logInfo("count");//自定义方法

我看了下,大概是说,这里不允许使用Android里头的类,因为本地单元测试使用的是Java。






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值