10.Android UiAutomator Junit 断言函数的使用

一、断言函数介绍

1.断言函数:

确定被测试的方法是否按照预期的效果正常工作

  • 比如说:
if (假设成立){
    通过测试
}else{
    报错并终止当前用例测试
}

2.断言函数用例结构:

  • 一个完整的测试用例必需要有断言函数
setUp//初始化
//测试用例,junit4版本才可以使用多条用例
test        初始化场景与数据
test        模拟操作步骤
test        断言
test        恢复场景    
tearDown//回收初始化垃圾

3.断言函数Java错误类型:

1)Error:

一般是指与虚拟机相关的问题,如系统崩溃,虚拟机错误,内存空间不足,方法调用栈溢出等。对于这类错误导致的应用程序中断,仅靠程序本身无法恢复和预防(断言)

2)Exeeption:

表示程序可以处理的异常,可以捕获且可能恢复。遇到这类异常,应该尽可能处理异常,使程序恢复运行,而不应该随意终止异常(最常见的是UI对象找不到的异常)

二、断言函数API:

1.断言函数分类:

这里写图片描述

  • 例如:
//断言两个对象是否相等
asserEquals(String message,Object expected,Object actual){
    if (expected==null && actual==null){
        return ;    
    }
    if (expected!=null && expected.equals(actual)){
        return
    }
    failNotEquals(message,expected,actual);
}

 

参数

说明

Message 可选消息,在断言失败后会抛出这个消息
Expected 期望的值
Actual 实际的值

 

2.相关API:

1)
方法 说明
assertEquals(boolean,boolean) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(String,boolean,boolean) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(byte,byte) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(String,byte,byte) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(char,char) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(String,char,char) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(int,int) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(String,int,int) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(long,long) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(String,long,long) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(Object,Object) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(String,Object,Object) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(short,short) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(String,short,short) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(String,String) 如果期望(expected)和实际(actual)相等则通过,否则失败
assertEquals(String,String,String) 如果期望(expected)和实际(actual)相等则通过,否则失败

 

  • API示例:
public void testDemo1() throws UiObjectNotFoundException{
    //断言相等的例子
    assertEquals(5, add(2,3));
    //断言不相等的例子
    assertEquals(6, add(2,3));
}
//新建一个加法方便使用断言函数
public int add(int a,int b){
    return a+b;
}
2)

浮点运算不是万全精确的,所以比较浮点数值的时候引入精确程度

assertEquals(double expected,double actual,double delta)

方法

说明

assertEquals(double,double,double) 如果期望(expected)和实际(actual)相差不超过精度值(delta)则通过,否则失败
assertEquals(String,double,double,double) 如果期望(expected)和实际(actual)相差不超过精度值(delta)则通过,否则失败
assertEquals(float,float,float) 如果期望(expected)和实际(actual)相差不超过精度值(delta)则通过,否则失败
assertEquals(String,float,float,float) 如果期望(expected)和实际(actual)相差不超过精度值(delta)则通过,否则失败

 

  • API示例:
public void testDemo1() throws UiObjectNotFoundException{
    //断言不相等的例子
    assertEquals(0.3333, Double.valueOf(1)/3,0);
    //断言相等的例子
    assertEquals(0.3333, Double.valueOf(1)/3,4);
    //断言相等的例子
    assertEquals(0.3333, Double.valueOf(1)/3,0.0001);
}
//新建一个加法方便使用断言函数
public int add(int a,int b){
    return a+b;
}
3)

方法

说明

assertFalse(boolean) 如果条件(condition)为False则通过,否则失败
assertFalse(String,boolean) 如果条件(condition)为False则通过,否则失败
assertTrue(boolran) 如果条件(condition)为True则通过,否则失败
assertTrue(String,boolran) 如果条件(condition)为True则通过,否则失败
assertNotNull(Object) 如果条件(condition)为非空则通过,否则失败
assertNotNull(String,Object) 如果条件(condition)为非空则通过,否则失败
assertNull(Object) 如果条件(condition)为空则通过,否则失败
assertNull(String,Object) 如果条件(condition)为空则通过,否则失败
assertNotSame(Object,object) 如果期望(expected)和实际(actual)引用不同的内存对象对象则通过,否则失败
assertNoteSame(String,Object,Object) 如果期望(expected)和实际(actual)引用不同的内存对象对象则通过,否则失败
assertSame(Object,Object) 如果期望(expected)和实际(actual)引用相同的内存对象对象则通过,否则失败
assertSame(String,Object,Object) 如果期望(expected)和实际(actual)引用相同的内存对象对象则通过,否则失败

 

API示例:

//示例一: 
public void testDemo1() throws UiObjectNotFoundException{ 
//自己声明一个布尔值为了测试断言函数的使用 
boolean is=true; 
//断言成功的例子 
assertTrue(is); 
//断言失败的例子 
assertFalse(is); 
} 
//示例二:

public void testDemo1() throws UiObjectNotFoundException{
    int a = 100;
    int b = 100;
    Integer c=new Integer(100);
    //断言成功的例子,值的比较
    assertSame(a, b);
    //断言失败的例子
    assertNotSame(a, b);
    //断言失败的例子,内存地址比较
    assertSame(a, c);
    //断言成功的例子
    assertSame((Object)a, c);
}

 

4)

方法

说明

fail() 用例立即失败
fail(String) 用例立即失败,且抛出指定消息
failNotEquals(String,Object,Object) 用例立即失败,且抛出指定消息与期望、实际值不相等的消息
failNotSame(String,String,String) 用例立即失败,且抛出指定消息与期望、实际值不相等的消息
failSame(String) 用例立即失败,且抛出指定消息

 

  • API示例:
public void testDemo1() throws UiObjectNotFoundException{
    UiDevice.getInstance().pressBack();
    UiDevice.getInstance().pressMenu();
    UiDevice.getInstance().pressHome();
    //用例失败,且抛出异常
    fail("Failed");
}

三、使用断言函数的实例演示

//开始
protected void setUp() throws Exception{
    super.setUp();
}
public void testDemo1() throws UiObjectNotFoundException{

    //初始化场景
    UiDevice.getInstance().pressBack();
    UiDevice.getInstance().pressBack();
    UiDevice.getInstance().pressBack();
    UiDevice.getInstance().pressHome();
    //打开文件管理
    UiObject app=new UiObject(new UiSelector().description("应用"));
    UiObject file=new UiObject(new UiSelector().text("文件管理器"));
    app.clickAndWaitForNewWindow();
    file.clickAndWaitForNewWindow();
    //验证开启成功
    String packageName=UiDevice.getInstance().getCurrentPackageName();
    assertEquals("File open succes!","com.cyanogenmod.filemanager", packageName);
    //检查目录名为test的目录
    UiObject test=new UiObject(new UiSelector().text("test"));
    UiScrollable listView=new UiScrollable(new UiSelector().className("android.widget.ListView"));
    boolean FindResult=listView.scrollIntoView(test);
    //如果有该目录则停止执行用例
    if (FindResult==true){
        fail("test目录已经存在");
    }
    //如果没有该目录则新建test目录
    UiObject dos=new UiObject(new UiSelector().description("操作"));
    dos.clickAndWaitForNewWindow();
    UiObject newFile=new UiObject(new UiSelector().text("新建文件夹"));
    newFile.clickAndWaitForNewWindow();
    UiObject input=new UiObject(new UiSelector().className("android.widget.EditText"));
    input.setText("test");
    UiObject ok=new UiObject(new UiSelector().text("确定"));
    ok.click();
    //验证新建成功    
    boolean FindResult2=listView.scrollIntoView(test);
    assertTrue("test目录创建成功",FindResult2);
}
//完成
protected void tearDown() throws Exception{
    super.tearDown();
    UiDevice.getInstance().pressBack();
    UiDevice.getInstance().pressBack();
    UiDevice.getInstance().pressBack();
    UiDevice.getInstance().pressHome();
}
posted @ 2016-02-28 17:50  许忠慧  阅读(2038)  评论(0编辑  收藏  举报