zoukankan      html  css  js  c++  java
  • 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/

     

  • 相关阅读:
    @bzoj
    @bzoj
    @codeforces
    @codeforces
    @bzoj
    @codeforces
    @codeforces
    @codeforces
    @NOIP2018
    反转字符串--C和Python
  • 原文地址:https://www.cnblogs.com/mengdd/p/3172580.html
Copyright © 2011-2022 走看看