zoukankan      html  css  js  c++  java
  • Android中使用自身携带的Junit新建一个测试工程

    1、新建立一个Android工程

    package com.shellway.junit;
    
    public class Service {
          public int divide(int a,int b){
              return a/b;
          }
    }
    Service.java.作为被测试类
    package com.shellway.junit;
    
    import junit.framework.Assert;
    import android.test.AndroidTestCase;
    
    public class TestT extends AndroidTestCase {
        public void test1(){
            Service service = new Service();
            System.out.println(service.divide(10, 2));
        }
        public void test2(){
            Service service = new Service();
            System.out.println(service.divide(10, 0));
        }
        public void test3(){
            Service service = new Service();
            Assert.assertEquals(2.5, service.divide(10, 4));
        }
    }
    TestT继承AndroidTestCase,作为测试类
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.shellway.junit"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="16"
            android:targetSdkVersion="21" />
           <instrumentation
            android:targetPackage="com.shellway.junit"
            android:name="android.test.InstrumentationTestRunner" />
        
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <uses-library android:name="android.test.runner" />
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>
    AndroidManifest.xml

    注意:在配置文件中要加入的内容是:

     <instrumentation
          android:targetPackage="com.shellway.junit"
          android:name="android.test.InstrumentationTestRunner" />
    
    <uses-library android:name="android.test.runner" />
    View Code

    并且要注意他们加入的位置。这是一种测试自身工程的方法。

    另一种方法是:新建一个Android Junit工程来测试另外的一个工程,这里需要注意的是建立这个测试工程名的时候不能以Test来命名,不然出现错误都很难找出问题所在,下面给出以Test来命名工程的出错信息。

  • 相关阅读:
    python操作文件
    匿名函数 列表生成式 字典生成式 递归
    pecan API调用
    lvm磁盘分区
    PasteDeploy部署Pecan API 服务
    oslo_service服务
    python pbr 打包
    stevedore动态加载模块
    mongodb 副本集部署
    python装饰器
  • 原文地址:https://www.cnblogs.com/shellway/p/4036372.html
Copyright © 2011-2022 走看看