Android测试框架

来源:互联网 发布:分区软件哪个好 编辑:程序博客网 时间:2024/06/05 19:15

Android开发测试


下面引用官方对测试框架的定义:

Testing your app is an integral part of the app development process. Testing allows you to verify the correctness, functional 
behavior, and usability of your app before you release it publicly.

测试应用程序是应用程序开发过程中不可或缺的一部分。测试允许您在公开发布之前验证应用程序的正确性、功能行为和可用性。


android测试分为两个部分

1.本地测试,运行在开发机器上的测试用例:运行在本地开发机器JVM上,不能调用android api

module-name/src/test/java/

2.运行在Android手机或模拟器上的测试用例:运行在自己App安装的机器上,并且能够与app进行交换

Located at module-name/src/androidTest/java/.

使用测试框架测试app的功能:

1.配置测试框架

2.自动用户UI测试:

UIAutomator:跨应用测试

Espresso:单个应用测试

3.测试service、contentprovider:测试组件集成

       4.测试UI性能


下面引入三张图片来介绍测试用例的目录和测试用例直观表示:

两个不同的测试用例在AndroidStudio中目录为:



下面引用官方的一张图片来说明本地测试和集成测试的区别:(图片很好的说明了测试用例的运行位置,这里不再说明)



测试中我们常使用到的注解:

1.@Before:在调用测试方法时调用

2.@After:在调用测试方法之后调用

3.@Test:表示测试方法

4.@Rule:定义测试规则

5.@BeforeClass:定义一个静态方法,在整个测试方法中只调用一次

6.@AfterClass:同上

7.@Test(timeout=):指定测试超时时间


Junit测试注解:测试注解连接

android注解使用:android注解库


使用下面的测试用例来测试注解中的内容:

public class ExampleUnitTest {    @Test    public void addition_isCorrect() throws Exception {        assertEquals(4, 2 + 2);    }    @BeforeClass    public static void testBeforeClass() throws Exception {        System.out.println("testBeforeClass " + System.currentTimeMillis());    }    @AfterClass    public static void testAfterClass() throws Exception {        System.out.println("testAfterClass " + System.currentTimeMillis());    }    @Before    public void testBefore() throws Exception {        System.out.println("testBefore " + System.currentTimeMillis());    }    @Test    public void testCase() throws Exception {        System.out.println("testCase " + System.currentTimeMillis());    }    @After    public void testAfter() throws Exception {        System.out.println("testAfter " + System.currentTimeMillis());    }}

输出结果:

testBefore 1499910665876:测试之前调用结果testCase 1499910665876 :测试用例testAfter 1499910665876:测试之后调用结果testBeforeClass 1499910665874:静态方法调用testAfterClass 1499910665877:静态方法最后调用


使用UiAutomator来获取当前控件的信息,方便查找当前的控件


UIAutomator相关的关键类

使用到的三个工具:

1.    UI AutoMator Viewer

2.    Accessing device StateàUIDevice:模拟手指操作

1.    改变设备方向

2.    按键或点击按钮

3.    按返回键,home键、菜单键

4.    打开通知

5.    截取当前屏幕

3.    UI Automator APIs

1.    UiCollection

2.    UiObject

3.    UiScrollable

4.    UiSelector

5.    Configurator




原创粉丝点击