JUnit测试小小demo

阅读更多

运行效果图:
[img]
JUnit测试小小demo_第1张图片
[/img]





项目结构图:
[img]
JUnit测试小小demo_第2张图片
[/img]



activity_main.xml


    
    
    
    
    


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行的添加



    

    
        
            
                

                
            
        
        
          
        
    









  • JUnit测试小小demo_第3张图片
  • 大小: 14 KB
  • JUnit测试小小demo_第4张图片
  • 大小: 63 KB
  • 查看图片附件

你可能感兴趣的:(junit,android)