Android Testing Point

The test application demonstrates these key points:
An Android test is itself an Android application that is linked to the application under test by entries in its AndroidManifest.xml
file.
//一个Android的测试本身是由被测项目在AndroidManifest.xml文件链接到应用程序的Android应用程序, 这句话说明了两个问题: 测试本身也是安卓的一个应用;

Instead of Android components, an Android test application contains one or more test cases. Each of these is a separate class definition.
//一个安卓的测试应用包括一个或多个测试集合用来代替安卓的模块.每一个都是一个独立的类定义.

Android test case classes extend the JUnit TestCase
class.
//安卓的测试集合继承自JUnit TestCase类.

Android test case classes for activities extend JUnit and also connect you to the application under test with instrumentation. You can send keystroke or touch events directly to the UI.
//对于activities的测试类继承自JUnit并且也能使你在测试集下使用instrumentation.你可以直接操作UI通过输入和触碰事件.

You choose an Android test case class based on the type of component (application, activity, content provider, or service) you are testing.
//你可以选择在一个组件(application, activity, content provider, or service)的基础上进行你的测试.

Additional test tools in Eclipse/ADT provide integrated support for creating test applications, running them, and viewing the results.
//除此之外, 在Eclipse/ADT 的工具能够支持创建测试应用, 跑测试和查看结果的集成套件

The test application contains methods that perform the following tests:
Initial conditions test. Tests that the application under test initializes correctly. This is also a unit test of the application's onCreate()
method. Testing initial conditions also provides a confidence measure for subsequent tests.
//初始条件化的测试.在测试初始化正确下测试应用. 这也是一个应用的onCreate()
方法的单元测试.测试初始条件也包括了一个在随后的测试中提供一个可行的度量.

UI test. Tests that the main UI operation works correctly. This test demonstrates the instrumentation features available in activity testing. It shows that you can automate UI tests by sending key events from the test application to the main application.
//UI测试.测试那些能够被正确操作的主要UI.这个测试主要在activity测试中展示instrumentation特性的可用性. 它展示了你能够通过从测试应用发送主要事件到主要应用的方式自动化UI测试.

State management tests. Test the application's code for saving state. This test demonstrates the instrumentation features of the test runner, which are available for testing any component.
申明管理测试集. 为了保存状态而测试应用的代码.这个测试展示了test runner的instrumentation的功能, 它能够对于测试任何组件可用.

The next step is to define the test case class. In this tutorial, you'll be creating a test case class that includes:
Test setup. This use of the JUnit setUp()
method demonstrates some of the tasks you might perform before running an Android test.

getActivity()
. Gets a reference to the activity under test (SpinnerActivity
). This call also starts the activity if it is not already running.
findViewById(int)
. Gets a reference to the Spinner
widget of the application under test.
getAdapter()
. Gets a reference to the adapter (an array of strings) backing the spinner.

Add this code to the definition of SpinnerActivityTest
, after the constructor definition:
@Override protected void setUp() throws Exception { super.setUp(); setActivityInitialTouchMode(false); mActivity = getActivity(); mSpinner = (Spinner) mActivity.findViewById( com.android.example.spinner.R.id.Spinner01 ); mPlanetData = mSpinner.getAdapter(); } // end of setUp() method definition
Testing initial conditions. This test demonstrates a good testing technique. It also demonstrates that with Android instrumentation you can look at the application under test before the main activity starts. The test checks that the application's important objects have been initialized. If the test fails, you then know that any other tests against the application are unreliable, since the application was running in an incorrect state.Note: The purpose of testing initial conditions is not the same as using setUp()
. The JUnit setUp()
runs once beforeeach test method, and its purpose is to create a clean test environment. The initial conditions test runs once, and its purpose is to verify that the application under test is ready to be tested.

The initial conditions test verifies that the application under test is initialized correctly. It is an illustration of the types of tests you can run, so it is not comprehensive. It verifies the following:
The item select listener is initialized. This listener is called when a selection is made from the spinner.
The adapter that provides values to the spinner is initialized.
The adapter contains the right number of entries.

The actual initialization of the application under test is done in setUp()
, which the test runner calls automatically before every test. The verifications are done with JUnit Assert
calls. As a useful convention, the method name istestPreConditions()
:
public void testPreConditions() { assertTrue(mSpinner.getOnItemSelectedListener() != null); assertTrue(mPlanetData != null); assertEquals(mPlanetData.getCount(),ADAPTER_COUNT); } // end of testPreConditions() method definition
Add this member:
public static final int ADAPTER_COUNT = 9;
Testing the UI. This test shows how to control the main application's UI with instrumentation, a powerful automation feature of Android testing.

Testing state management. This test shows some techniques for testing how well the application maintains state in the Android environment. Remember that to provide a satisfactory user experience, your application must never lose its current state, even if it's interrupted by a phone call or destroyed because of memory constraints. The Android activity lifecycle provides ways to maintain state, and the SpinnerActivity
application uses them. The test shows the techniques for verifying that they work.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,546评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,224评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,911评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,737评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,753评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,598评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,338评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,249评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,696评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,888评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,013评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,731评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,348评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,929评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,048评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,203评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,960评论 2 355

推荐阅读更多精彩内容