zoukankan      html  css  js  c++  java
  • Android 单元测试

    1:包结构如下:

    2:MyService.java

    public class MyService {
    	public int add(int a,int b){
    		return a+b;
    	}
    	
    	public int cal(int a,int b){
    		return a*b;
    	}
    }
    

    3:MyServiceTest.java

    public class MyServiceTest extends AndroidTestCase {
    	MyService ms=new MyService();
    	
    	public void testAdd(){
    		int sum=ms.add(1, 2);
    		Assert.assertEquals(3, sum);
    	}
    	
    	public void testCal(){
    		int sum=ms.cal(1, 2);
    		Assert.assertEquals(2, sum);
    	}
    }
    

     4:要修改AndroidManifest.xml配置文件

    <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="com.yshy.demo.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>
    
        <instrumentation 
            android:name="android.test.InstrumentationTestRunner"  
            android:label="Test for my app"  
            android:targetPackage="com.yshy.demo"/>

    5:在MyServiceTest.java 右键Run as ->Android Junit Test

  • 相关阅读:
    Codeforces 946D
    Codeforces 817F
    Codeforces 931F
    Codeforces 932D
    Graph HDU
    Chef and Graph Queries CodeChef
    Lucky Array Codeforces
    Calculation 2 HDU
    洛谷 P3455 [POI2007]ZAP-Queries || 洛谷P2522,bzoj2301
    洛谷 P2398 GCD SUM || uva11417,uva11426,uva11424,洛谷P1390,洛谷P2257,洛谷P2568
  • 原文地址:https://www.cnblogs.com/yshyee/p/3363093.html
Copyright © 2011-2022 走看看