android 单位测试

Andorid下使用Junit测试框架,是步入正规的Androdid开发的必经之路,在Junit中可以得到组件,可以模拟发送事件,检测程序处理的正确性,对于软件开发者来说,必须写unite test!

源码:

package com.acitivity.test;

import android.app.Activity;
import android.os.Bundle;

public class MainTestActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	public int add(int a, int b) {
		return a + b;
	}

	public int substract(int a, int b) {
		return a - b;
	}
}


测试源码:

package com.acitivity.test.test;

import com.acitivity.test.MainTestActivity;
import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

public class TestMainActivity extends
		ActivityInstrumentationTestCase2<MainTestActivity> {

	public TestMainActivity() {
		super("com.acitivity.test", MainTestActivity.class);
	}
	protected void setUp() throws Exception {
		Log.d("liuwei", "************************");
		super.setUp();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testSum() {
		this.assertEquals(3, ((MainTestActivity) getActivity()).add(1, 2));
	}
}


我把这两个代码,分别放在两个工程里。

你可能感兴趣的:(android,框架,软件测试,OS,JUnit)