Android单元测试/Ui测试+JaCoCo覆盖率统计

2024-05-08 22:32

本文主要是介绍Android单元测试/Ui测试+JaCoCo覆盖率统计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android单元测试/Ui测试+JaCoCo覆盖率统计

背景说明

  • 单元测试
    从源代码着手,对源码中的最小可测试单元进行检查和验证,在对源代码有较深的理解下,编写测试单元,工作量大,不管从编写单元测试用例再到用例的维护上,成本都会比较高,但是通过这种方式可靠性很强。

  • UI测试
    从UI层面着手,对UI操作进行检查和验证,可以不需要对代码有深层次的了解,成本相对较低,工作量相对也低一些,但是可靠性相比之下会弱一点。

  • 覆盖率的统计
    我们有了多种测试方式,那么问题来了,这些测试的性能怎么样,是不是所有的代码都被测试过了?这时候就需要加入覆盖率的统计了,如果一个工程的待测数量为M,测试用例的数量为N,那么代码覆盖率F则为:

    F=N/M

    本文将介绍一个代码覆盖率的工具JaCoCo,通过这个工具,我们可以知道哪些方法被测试了,哪些方法没有被测试到。

1. 先新建一个Android工程,大致的内容是有一个MainActivity,输入两个数,可以计算出二者相加、相乘的结果,并通过toast显示出计算结果。

  • app界面如下:

这里写图片描述


  • 源代码
    activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.learn.learnjacoco.MainActivity"><TextView
        android:layout_width="match_parent"android:layout_height="wrap_content"android:text="输入两个数:" /><RelativeLayout
        android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"android:orientation="horizontal"><EditText
            android:hint="number A"android:id="@+id/edt_numA"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:inputType="number" /><EditText
            android:hint="number B"android:id="@+id/edt_numB"android:layout_width="100dp"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:inputType="number" /></RelativeLayout><RelativeLayout
        android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="10dp"><Button
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:onClick="add"android:text="add" /><Button
            android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:onClick="plus"android:text="plus" /></RelativeLayout></LinearLayout>

MainActivity

package com.learn.learnjacoco;import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {public EditText edtNumA, edtNumB;int a, b;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);edtNumA = (EditText) findViewById(R.id.edt_numA);edtNumB = (EditText) findViewById(R.id.edt_numB);}public void add(View v) {if (getMyNum()) {String info = String.valueOf(MyUtils.add(a, b));Toast.makeText(this, "计算结果:" + info, Toast.LENGTH_SHORT).show();}else {Toast.makeText(this, "数字不合法", Toast.LENGTH_SHORT).show();}}public void plus(View v) {if (getMyNum()) {String info = String.valueOf(MyUtils.plus(a, b));Toast.makeText(this, "计算结果:" + info, Toast.LENGTH_SHORT).show();}else {Toast.makeText(this, "数字不合法", Toast.LENGTH_SHORT).show();}}public boolean getMyNum() {String aStr = edtNumA.getText().toString();String bStr = edtNumB.getText().toString();if (!TextUtils.isEmpty(aStr) && !TextUtils.isEmpty(bStr)) {a = Integer.parseInt(aStr);b = Integer.parseInt(bStr);return true;}return false;}
}

MyUtils

package com.learn.learnjacoco;public class MyUtils {public static int add(int a, int b) {return (a + b);}public static int plus(int a, int b) {return (a * b);}
}

以上完成了之后,先试运行一下,保证app能正常运行起来。

2. 编写单元测试

将视图切换到Android视图,如下:
这里写图片描述

可以看到com.learn.learnjacoco有三个,第一个存放工程的源码,后面两个放的是测试代码,分别为androidTest和test,这里顺带说明一下,androidTest一般存放的是和Android相关的测试,比如需要用到context等时候应该把测试放到该包下,而test对应的是Java相关的测试,也android无关的应该放到此包下。我们直接使用AndroidTest的包。

打开MyUtils.java,对着类名右键,Go to – Test – CreateNewTest – 把要测试的方法打上勾 – 选择存放在AndroidTest包下,如图:

这里写图片描述

补充MyUtilsTest的内容如下:

package com.learn.learnjacoco;import org.junit.Assert;
import org.junit.Test;public class MyUtilsTest {@Testpublic void add() throws Exception {Assert.assertEquals(3,MyUtils.add(1,2));}@Testpublic void plus() throws Exception {Assert.assertEquals(30,MyUtils.plus(10,3));}}

3. 编写UI测试

这里我们使用robotium编写UI测试用例(你也可以使用uiautomator去编写,效果是一样的),首先,我们需要配置gradle(app),添加robotium的依赖。

dependencies {...androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.1'
}

接下来,在androidTest包下,我们创建一个MainActivity的测试类,内容如下:

package com.learn.learnjacoco;import android.test.ActivityInstrumentationTestCase2;import com.robotium.solo.Solo;import junit.framework.Assert;public class MainActivityTest extends ActivityInstrumentationTestCase2 {private Solo solo;private MainActivity mActivity;public MainActivityTest() {super(MainActivity.class);}@Overridepublic void setUp() throws Exception {super.setUp();solo = new Solo(getInstrumentation(), getActivity());mActivity = (MainActivity) getActivity();}@Overridepublic void tearDown() throws Exception {super.tearDown();}public void testInputA() throws Exception {solo.enterText(mActivity.edtNumA, "10");Assert.assertEquals("10", mActivity.edtNumA.getText().toString());}public void testInputB() throws Exception {solo.enterText(mActivity.edtNumB, "3");Assert.assertEquals("3", mActivity.edtNumB.getText().toString());}
}

4. 运行connectedAndroidTest

在Android Studio的右侧垂直栏中,有一个小的gradle图标,点开,如下图(如果没有内容,可以点击刷新):

这里写图片描述

运行完毕之后,进入到工程目录 $app\build\reports\androidTests\connected下,打开index.xml,这个文件中会存放这本次测试的结果,即使测试不通过,也会保留错误信息,如下图:

这里写图片描述

这篇关于Android单元测试/Ui测试+JaCoCo覆盖率统计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/971617

相关文章

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

在Linux终端中统计非二进制文件行数的实现方法

《在Linux终端中统计非二进制文件行数的实现方法》在Linux系统中,有时需要统计非二进制文件(如CSV、TXT文件)的行数,而不希望手动打开文件进行查看,例如,在处理大型日志文件、数据文件时,了解... 目录在linux终端中统计非二进制文件的行数技术背景实现步骤1. 使用wc命令2. 使用grep命令

使用Python进行GRPC和Dubbo协议的高级测试

《使用Python进行GRPC和Dubbo协议的高级测试》GRPC(GoogleRemoteProcedureCall)是一种高性能、开源的远程过程调用(RPC)框架,Dubbo是一种高性能的分布式服... 目录01 GRPC测试安装gRPC编写.proto文件实现服务02 Dubbo测试1. 安装Dubb

Python的端到端测试框架SeleniumBase使用解读

《Python的端到端测试框架SeleniumBase使用解读》:本文主要介绍Python的端到端测试框架SeleniumBase使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全... 目录SeleniumBase详细介绍及用法指南什么是 SeleniumBase?SeleniumBase

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

VS配置好Qt环境之后但无法打开ui界面的问题解决

《VS配置好Qt环境之后但无法打开ui界面的问题解决》本文主要介绍了VS配置好Qt环境之后但无法打开ui界面的问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目UKeLvb录找到Qt安装目录中designer.UKeLvBexe的路径找到vs中的解决方案资源

详解如何使用Python从零开始构建文本统计模型

《详解如何使用Python从零开始构建文本统计模型》在自然语言处理领域,词汇表构建是文本预处理的关键环节,本文通过Python代码实践,演示如何从原始文本中提取多尺度特征,并通过动态调整机制构建更精确... 目录一、项目背景与核心思想二、核心代码解析1. 数据加载与预处理2. 多尺度字符统计3. 统计结果可