• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • Android Junit單元測試4部曲

    發表于:2014-10-29來源:uml.org.cn作者:不詳點擊數: 標簽:單元測試
    我們曾經和大家探討過全面剖析Java ME單元測試理念,其實在Android上實現JUnit單元測試也不是很困難,主要是在配置文件和測試環境上將花費很長時間,下面從四步簡單講一下在Android上實

      我們曾經和大家探討過全面剖析Java ME單元測試理念,其實在Android上實現JUnit單元測試也不是很困難,主要是在配置文件和測試環境上將花費很長時間,下面從四步簡單講一下在Android上實現Junit單元測試。

      第一步:新建一個TestCase,記得要繼承androidTestCase,才能有getContext()來獲取當前的上下文變量,這在Android測試中很重要的,因為很多的Android api都需要context。

      Java代碼

    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) == 5));
    }
    public void testDec() {
    assertTrue("testDec failed", ((i2 - i1) == 1));
    }
    @Override
    protected void tearDown() throws Exception {
    super.tearDown();
    }
    @Override
    public void testAndroidTestCaseSetupProperly() {
    super.testAndroidTestCaseSetupProperly();
    //Log.d( LOG_TAG, "testAndroidTestCaseSetupProperly" );
    }
    }

      第二步:新建一個TestSuit,這個就繼承Junit的TestSuite就可以了,注意這里是用的addTestSuite方法,一開始使用addTest方法就是不能成功。

      Java代碼

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

      第三步:新建一個Activity,用來啟動單元測試,并顯示測試結果。系統的AndroidTestRunner竟然什么連個UI界面也沒有實現,這里只是最簡單的實現了一個。

      Java代碼

    public class TestActivity 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.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 = 0;
    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;
    }
    }
    }

    原文轉自:http://www.uml.org.cn/Test/201308013.asp

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>