Android Testing学习02 HelloTesting 项目建立与执行
Android Testing学习02 HelloTesting 项目建立与执行
Android测试,分为待测试的项目和测试项目,这两个项目会生成两个独立的apk,但是内部,它们会共享同一个进程。
下面,新建一个Android待测试的项目,即普通的Android工程,这里起名为:MainProject;
新建测试工程
再建一个测试项目,叫MainProjectTest,对MainProject进行测试。
可以直接右键New->Project…->Android Test Project:

项目名起为:MainProjectTest
点击Next,选择要测试的项目为待测试的项目,这里即为MainProject。
选择好后,测试项目就建立成功了。

IDE帮我们做了什么?
测试项目虽然建立成功了,但是IDE帮我们做了什么呢?
首先,比较明显的是生成了对应的包名,在原来的包名后加了.test:
com.shengqishiwind.myproject.test
其次,打开测试项目的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shengqishiwind.myproject.test"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.shengqishiwind.myproject" /> <application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
</application> </manifest>
AndroidManifest.xml
解释标注如下:

最后一个改变,右键MainProjectTest项目,Properties->Java Build Path->Projects,可以看到已经加入了MainProject,引入了被测试工程,所以测试工程可以访问被测试工程的类。

创建测试
创建测试仍然可以直接利用IDE:
在测试项目的包上右键->New->JUnit Test Case:
创建如下:

点击Next,可以选择生成一些方法,到时候按照需要选择吧。

生成的测试类代码如下,加上了print语句:
package com.shengqishiwind.myproject.test; import junit.framework.TestCase; public class MyFirstTest extends TestCase
{ public MyFirstTest(String name)
{
super(name);
System.out.println("MyFirstTest");
} protected void setUp() throws Exception
{
super.setUp();
System.out.println("setUp()");
} protected void tearDown() throws Exception
{
super.tearDown();
System.out.println("tearDown()");
} public void testSomething()
{
System.out.println("testSomething()");
fail("Not implemented yet"); } }
因为单元测试框架是基于JUnit 3的,所以方法需要以test开头。
这里插一段:
在JUnit 3.8中,测试方法需要满足如下原则:
1.public的。
2.void的。
3.无方法参数。
4.方法名称必须以test开头。 (它通过反射找出所有方法,然后找出以test开头的方法)。
运行测试
1.最简单的方法:运行所有测试:
右键测试项目Run As ->Android JUnit Test。
这样将运行项目中所有的测试。
2.运行一些test case:
右键项目:Run As -> Run Configurations,然后在其中选择Run a single test.
这里看到也可以自选一些test case来一起运行。
运行后的结果在左边的JUnit小窗口中显示,LogCat中也有相关显示。
3.通过命令行来运行测试
先adb shell之后就可以通过如下命令行命令来运行测试:
测试结果只能从LogCat中查看了。
am instrument [flags] <COMPONENT>
-r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
-e <NAME> <VALUE>: set argument <NAME> to <VALUE>
-p <FILE>: write profiling data to <FILE>
-w: wait for instrumentation to finish before returning
关于命令参数的更多,可以查看:http://developer.android.com/tools/testing/testing_otheride.html
和 http://developer.android.com/reference/android/test/InstrumentationTestRunner.html
参考资料
《Android Application Testing Guide》
Android Testing官网链接:
http://developer.android.com/tools/testing/index.html
http://developer.android.com/tools/testing/activity_test.html
http://developer.android.com/tools/testing/testing_otheride.html
Testing Fundamentals
http://developer.android.com/tools/testing/testing_android.html
AndroidTestCase
http://developer.android.com/reference/android/test/AndroidTestCase.html
ViewAsserts
http://developer.android.com/reference/android/test/ViewAsserts.html
MoreAsserts
http://developer.android.com/reference/android/test/MoreAsserts.html
正则表达式:
http://developer.android.com/reference/java/util/regex/package-summary.html
InstrumentationTestRunner文档,其中有命令行运行测试相关:
http://developer.android.com/reference/android/test/InstrumentationTestRunner.html
关于JUnit 3的基础,本博客之前有文:
http://www.cnblogs.com/mengdd/archive/2013/03/26/2983565.html
博客:
http://mintelong.iteye.com/blog/460903
http://mobile.tutsplus.com/tutorials/android/android-sdk-junit-testing/
Android Testing学习02 HelloTesting 项目建立与执行的更多相关文章
- Android Testing学习01 介绍 测试测什么 测试的类型
Android Testing学习01 介绍 测试测什么 测试的类型 Android 测试 测什么 1.Activity的生命周期事件 应该测试Activity的生命周期事件处理. 如果你的Activ ...
- android菜鸟学习笔记16----Android项目打包安装过程(Run as Android Application)
右击项目名称,Run as Android Appication之后,Android项目打包安装过程: 1.打包生成.apk文件: 1)把源码中的.java文件编译生成.class文件 2)将所有的. ...
- android菜鸟学习笔记4----android项目结构
src: 应用程序源代码存放目录 gen: 自动生成的目录,目录中存放所有由Android开发工具自动生成的文件. 目录中最重要的就是R.java文件. 这个文件由Android开发工具自动产生的.A ...
- android开发学习---基础知识学习、如何导入已有项目和开发一个电话拨号器
一.基础知识点学习 1.Android体系结构 如图所示,android 架构分为三层: (1)最底层是linux内核,主要是各种硬件的驱动,如相机驱动(Camera Driver),闪存驱动(Fl ...
- Flutter实战视频-移动电商-02.Flutter实战建立项目和编写入口文件
02.Flutter实战建立项目和编写入口文件 创建项目: flutter create flutter_shop 创建完成之后呢,它会提示我们, 进入flutter_shop的目录,然后执行flut ...
- Android Studio安卓学习笔记(一)安卓与Android Studio运行第一个项目
一:什么是安卓 1.Android是一种基于Linux的自由及开放源代码的操作系统. 2.Android操作系统最初由AndyRubin开发,主要支持手机. 3.Android一词的本义指“机器人”, ...
- android studio学习----添加项目依赖包总结
Gradle Library Projects Gradle 项目可以依赖于其它组件.这些组件可以是外部二进制包,或者是其它的 Gradle 项目. 在本例中, app/build.gradle 中有 ...
- Android Animation学习(二) ApiDemos解析:基本Animators使用
Android Animation学习(二) ApiDemos解析:基本Animatiors使用 Animator类提供了创建动画的基本结构,但是一般使用的是它的子类: ValueAnimator.O ...
- Android开发学习总结(一)——搭建最新版本的Android开发环境
Android开发学习总结(一)——搭建最新版本的Android开发环境(转) 最近由于工作中要负责开发一款Android的App,之前都是做JavaWeb的开发,Android开发虽然有所了解,但是 ...
随机推荐
- Cannot override the final method from SherlockFragmentActivity
调用ActionBarSherlock后页面找不到onCreateOptionsMenu报错 com.actionbarsherlock.app.SherlockFragmentActivity.on ...
- postgres中的中文分词zhparser
postgres中的中文分词zhparser postgres中的中文分词方法 基本查了下网络,postgres的中文分词大概有两种方法: Bamboo zhparser 其中的Bamboo安装和使用 ...
- 如何使用laravel搭建后台登录系统
今天想用laravel搭建一个后台系统,就需要最简单的那种,有用户登录系统,试用了下,觉得laravel的用户登录这块做的还真happy.当然,前提就是,你要的用户管理系统是最简单的那种,就是没有用户 ...
- GPUimage实时滤镜的实现
GPUIMAGE中GPUImageStillCamera可以调用系统相机,并实现实时滤镜,但是我没有找到相机全屏的方法,望知道的说一下 GPUImageStillCamera继承自GPUImageVi ...
- Android SDK Manager国内更新代理
在Android SDK Manager Setting 窗口设置HTTP Proxy server和HTTP Proxy Port这个2个参数,分别设置为: HTTP Proxy server:mi ...
- StgCreateDocfileOnILockBytes复合文档
CRichEditCtrl 的ole技术 ------------ IRichEditOle --------------------------- 如需向CRichEditCtrl里面插入Ole对象 ...
- 127.0.0.1\SQLEXPRESS连接异常
当你的数据库为SQLEXPRESS时,在程序的数据库连接字符串的服务Server使用127.0.0.1\SQLEXPRESS时,如下: 它会显示一异常: Server Error in '/' App ...
- iOS学习笔记——多控制器管理
NavigationController 在StoryBoard中添加NavigationController 在上网看到很多都是用xib添加,使用StoryBard的有两种办法,但我觉得下面用到那种 ...
- Scalaz(4)- typeclass:标准类型-Equal,Order,Show,Enum
Scalaz是由一堆的typeclass组成.每一个typeclass具备自己特殊的功能.用户可以通过随意多态(ad-hoc polymorphism)把这些功能施用在自己定义的类型上.scala这个 ...
- Android中的checkbox和RadioButton的区别
1.单个RadioButton在选中后,通过点击无法变为未选中 单个CheckBox在选中后,通过点击可以变为未选中 2.一组RadioButton,只能同时选中一个 一组Che ...