参考:http://www.open-open.com/lib/view/open1328152424546.html
搭建Junit
http://zhidao.baidu.com/link?url=vMCO-E0_utuzonHcMnZ-xj0168uooyIV7fnDRttjbTdZmIw5daFYD2x549IRZ1eslePTvB7mc-mpuJa5zfvCDdJwqmamO5UHFDgAenmismO
java file
package cn.edu.wtu.junit; public class Calcuator { public double add(double n1, double n2) { return n1 + n1; } }
testcase file
package cn.edu.wtu.junit; import junit.framework.TestCase; public class TestCalcuator extends TestCase { public void testAdd1(){ Calcuator calcuator = new Calcuator(); double result = calcuator.add(1, 2); assertEquals(3,result,1); } public void testAdd2(){ Calcuator calcuator = new Calcuator(); double result = calcuator.add(1, 2); assertEquals(0,result,0); } public void testAdd3(){ } }
junit中:assertEquals(expResult, result,0.0);
expResult 你期望的结果,即你预测程序运行后出现的结果
result 实际运行后得到的结果。
0.0 两个结果间的允许误差
testsuit file
package cn.edu.wtu.junit; import junit.framework.Test; import junit.framework.TestSuite; import junit.textui.TestRunner; public class TestAll extends TestSuite { public static Test getSuite(){ TestSuite test = new TestSuite("test"); test.addTestSuite(TestCalcuator.class); test.addTestSuite(TestCalcuator2.class); return test; } public static void main(String args[]){ TestRunner.run(getSuite()); } }
控制台输出:
..F...F. Time: 0.002 There were 2 failures: 1) testAdd2(cn.edu.wtu.junit.TestCalcuator)junit.framework.AssertionFailedError: expected:<0.0> but was:<2.0> at cn.edu.wtu.junit.TestCalcuator.testAdd2(TestCalcuator.java:15) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at cn.edu.wtu.junit.TestAll.main(TestAll.java:18) 2) testAdd2(cn.edu.wtu.junit.TestCalcuator2)junit.framework.AssertionFailedError: expected:<0.0> but was:<2.0> at cn.edu.wtu.junit.TestCalcuator2.testAdd2(TestCalcuator2.java:15) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at cn.edu.wtu.junit.TestAll.main(TestAll.java:18) FAILURES!!! Tests run: 6, Failures: 2, Errors: 0
android test
首先看下非instrumentation框架测试
代码:
AndroidTestCase文件
package aexp.junit; import android.content.ContentResolver; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.provider.Contacts; import android.util.Log; import android.test.AndroidTestCase; public class ContactTest extends AndroidTestCase { static final String LOG_TAG = "ContactTest"; static final String TESTUSER_NAME = "Test User"; static final String TESTUSER_NOTES = "Test note"; ContentResolver contentResolver; Uri newPerson; public void setUp() { contentResolver = getContext().getContentResolver(); ContentValues person = new ContentValues(); person.put(Contacts.People.NAME, TESTUSER_NAME ); person.put(Contacts.People.NOTES, TESTUSER_NOTES ); newPerson = contentResolver.insert( Contacts.People.CONTENT_URI,person); } public void testInsertContact() { Log.d( LOG_TAG, "testInsertContact" ); assertNotNull( newPerson ); } public void testQueryContact() { Log.d( LOG_TAG, "testQueryContact" ); String columns[] = { Contacts.People.NAME, Contacts.People.NOTES }; Cursor c = contentResolver.query( Contacts.People.CONTENT_URI, columns, Contacts.People.NAME+"=?", new String[] { TESTUSER_NAME }, null ); assertNotNull( c ); int hits = 0; while( c.moveToNext() ) { int nameColumnIndex = c.getColumnIndex( Contacts.People.NAME ); int notesColumnIndex = c.getColumnIndex( Contacts.People.NOTES ); String name = c.getString( nameColumnIndex ); String notes = c.getString( notesColumnIndex ); Log.d( LOG_TAG,"retrieved name: "+name ); Log.d( LOG_TAG,"retrieved notes: "+notes ); assertEquals( TESTUSER_NAME, name ); assertEquals( TESTUSER_NOTES, notes ); ++hits; } assertEquals( hits,1 ); c.close(); } @Override public void tearDown() { contentResolver.delete( newPerson, null, null ); } }
AndroidTestCase文件
package aexp.junit; import android.test.AndroidTestCase; import android.util.Log; public class MathTest extends AndroidTestCase { protected int i1; protected int i2; static final String LOG_TAG = "MathTest"; @Override public void setUp() { i1 = 2; i2 = 3; } public void testAdd() { Log.d( LOG_TAG, "testAdd" ); assertTrue( LOG_TAG+"1", ( ( i1 + i2 ) == 5 ) ); } public void testAndroidTestCaseSetupProperly() { super.testAndroidTestCaseSetupProperly(); Log.d( LOG_TAG, "testAndroidTestCaseSetupProperly" ); } }
testcase文件
package aexp.junit; import junit.framework.TestCase; import junit.framework.TestSuite; import junit.framework.Assert; public class SomeTest extends TestCase{ public void testSomething() throws Throwable { Assert.assertTrue(1 + 1 == 2); } public void testSomethingElse() throws Throwable { Assert.assertTrue(1 + 1 == 3); } }
suite 文件
package aexp.junit; import junit.framework.TestSuite; public class ExampleSuite extends TestSuite { public ExampleSuite() { addTestSuite( MathTest.class ); addTestSuite( ContactTest.class ); addTestSuite(SomeTest.class); } }
新建andoridTest工程后 menifest中自动生成如下
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="aexp.junit.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="aexp.junit.test" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <uses-library android:name="android.test.runner" /> </application> </manifest>
讲解:
setup 初始化
teardown 程序执行结束后 回收变量
instrumentation框架:
package com.example.testandroidtestactivity.test; import android.app.Instrumentation; import android.test.ActivityInstrumentationTestCase2; import android.test.UiThreadTest; import android.widget.TextView; import com.example.testandroidtestactivity.MainActivity; public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> { public MainActivityTest(Class<MainActivity> activityClass) { super(activityClass); } @SuppressWarnings("deprecation") public MainActivityTest() { super("cn.edu.wtu.junit", MainActivity.class); } private Instrumentation mInstrument; private MainActivity mActivity; private TextView tv; @Override protected void setUp() throws Exception { super.setUp(); mInstrument = getInstrumentation(); // 启动被测试的Activity mActivity = getActivity(); tv = (TextView) mActivity.findViewById(com.example.testandroidtestactivity.R.id.tv); } public void testUITextView(){ mInstrument.runOnMainSync(new Runnable() { public void run() { tv.setText("test text!!"); } }); } @Override protected void tearDown() throws Exception { // TODO Auto-generated method stub super.tearDown(); } @Override protected void runTest() throws Throwable { // TODO Auto-generated method stub super.runTest(); } }