`

JUnit测试小小demo

阅读更多

运行效果图:
[img]

[/img]





项目结构图:
[img]

[/img]



activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/ResultView"
        android:text="測試結果"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
    <TextView
        android:id="@+id/BarView"
        android:text="barView"
        android:layout_marginTop="50dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/MessageView"
        android:text="message"
        android:layout_marginTop="50dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/LunchButton"
        android:text="button"
        android:layout_marginTop="50dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>


TestMath
要测试的两个地方
package com.example.junitandroidtest;

import android.test.AndroidTestCase;

public class TestMath extends AndroidTestCase{
	
	 private int i1;    
	    private int i2;    
	    static final String LOG_TAG = "MathTest";    
	        
	    @Override    
	    protected void setUp() throws Exception {    
	        i1 = 2;    
	        i2 = 3;    
	    }    
	        
	    public void testAdd() {    
	        assertTrue("testAdd failed", ((i1 + i2) == 6));    
	    }    
	        
	    public void testDec() {    
	        assertTrue("testDec failed", ((i2 - i1) == 0));    
	    }    
	    
	    @Override    
	    protected void tearDown() throws Exception {    
	        super.tearDown();    
	    }    
	    
	    @Override    
	    public void testAndroidTestCaseSetupProperly() {    
	        super.testAndroidTestCaseSetupProperly();    
	        //Log.d( LOG_TAG, "testAndroidTestCaseSetupProperly" );    
	    }    
}




ExampleSuite
package com.example.junitandroidtest;

import junit.framework.TestSuite;

public class ExampleSuite extends TestSuite {    
    
    public ExampleSuite() {    
        addTestSuite(TestMath.class);    
    }    
    
}    




MainActivity.java
package com.example.junitandroidtest;

import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Color;
import android.test.AndroidTestRunner;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private TextView resultView;

	private TextView barView;

	private TextView messageView;

	private Thread testRunnerThread;

	private static final int SHOW_RESULT = 0;

	private static final int ERROR_FIND = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		resultView = (TextView) findViewById(R.id.ResultView);
		barView = (TextView) findViewById(R.id.BarView);
		messageView = (TextView) findViewById(R.id.MessageView);
		Button lunch = (Button) findViewById(R.id.LunchButton);
		lunch.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				startTest();
			}
		});
	}

	private void showMessage(String message) {
		hander.sendMessage(hander.obtainMessage(ERROR_FIND, message));
	}

	private void showResult(String text) {
		hander.sendMessage(hander.obtainMessage(SHOW_RESULT, text));
	}

	private synchronized void startTest() {
		if (testRunnerThread != null && testRunnerThread.isAlive()) {
			testRunnerThread = null;
		}
		if (testRunnerThread == null) {
			testRunnerThread = new Thread(new TestRunner(this));
			testRunnerThread.start();
		} else {
			Toast.makeText(this, "Test is still running", Toast.LENGTH_SHORT)
					.show();
		}
	}

	public Handler hander = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SHOW_RESULT:
				resultView.setText(msg.obj.toString());
				break;
			case ERROR_FIND:
				messageView.append(msg.obj.toString());
				barView.setBackgroundColor(Color.RED);
				break;
			default:
				break;
			}
		}
	};

	class TestRunner implements Runnable, TestListener {

		private Activity parentActivity;

		private int testCount;

		private int errorCount;

		private int failureCount;

		public TestRunner(Activity parentActivity) {
			this.parentActivity = parentActivity;
		}

		@Override
		public void run() {
			testCount = -1;
			errorCount = 0;
			failureCount = 0;

			ExampleSuite suite = new ExampleSuite();
			AndroidTestRunner testRunner = new AndroidTestRunner();
			testRunner.setTest(suite);
			testRunner.addTestListener(this);
			testRunner.setContext(parentActivity);
			testRunner.runTest();
		}

		@Override
		public void addError(Test test, Throwable t) {
			errorCount++;
			showMessage(t.getMessage() + "\n");
		}

		@Override
		public void addFailure(Test test, AssertionFailedError t) {
			failureCount++;
			showMessage(t.getMessage() + "\n");
		}

		@Override
		public void endTest(Test test) {
			showResult(getResult());
		}

		@Override
		public void startTest(Test test) {
			testCount++;
		}

		private String getResult() {
			int successCount = testCount - failureCount - errorCount;
			return "Test:" + testCount + " Success:" + successCount
					+ " Failed:" + failureCount + " Error:" + errorCount;
		}

	}

}






AndroidManifest.xml
注意里面第26行的添加
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.junitandroidtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.junitandroidtest.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>
        
         <uses-library android:name="android.test.runner" /> 
        
    </application>

</manifest>








  • 大小: 14 KB
  • 大小: 63 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics