JUnit in android

android 测试框架是基于JUnit(www.junit.org)的,感兴趣的可以看看JUnit的源码,相信会对做unit test in android 大有帮助的。

本文只是一篇介绍JUnit如何用于android的入门文字,更多是照着网上的敲出来的,很意外的是三星的官网上居然有介绍JUnit的文章,

哈哈,所以拿过来用了。以前也没做过unit test,JUnit也是最近才接触,记录一下,边学边用吧!!

英文还OK的可以直接看原文(click me)吧!!

JUnit的基本思路:

a 创建自己的项目(已有项目直接跳过这步,从b开始)

b 创建android test project.

下面介绍步骤a,b.

a 创建自己的项目。创建普通android项目,其中main.xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button
        android:id="@+id/Pounds"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="Convert to Pounds" />
    <EditText
        android:id="@+id/inputvalueK"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/Pounds"
        android:hint="No. of Kilos" />
    <Button
        android:id="@+id/Kilos"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@id/Pounds"
        android:text="Convert to Kilos" />
    <EditText
        android:id="@+id/inputvalueP"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Pounds"
        android:layout_toLeftOf="@id/Kilos"
        android:hint="No. of Pounds" />
    <TextView
        android:id="@+id/resultview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/Kilos"
        android:text="Result  " />
</RelativeLayout>

该项目的相关activity ,其中Sample的代码

package com.example.samplejunit;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Sample extends Activity implements OnClickListener {
	Button mckilos,mcpounds;
	EditText mkilos,mpounds;
	TextView mresult;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mresult = (TextView)findViewById(R.id.resultview);
		mkilos = (EditText)findViewById(R.id.inputvalueK);
		mpounds = (EditText)findViewById(R.id.inputvalueP);
		mckilos = (Button)findViewById(R.id.Kilos);
		mcpounds = (Button)findViewById(R.id.Pounds);
	
		mckilos.setOnClickListener(this);
		mcpounds.setOnClickListener(this);
	}
	
	
	@Override
	public void onClick(View v)
	{
		switch(v.getId())
		{
			case R.id.Kilos:
			{
				double pounds = Double.parseDouble(mpounds.getText().toString());
				double kilos = pounds*0.4535;
				mresult.setText(new Double(kilos).toString());
			}
			break;
			case R.id.Pounds:
			{
				double kilos = Double.parseDouble(mkilos.getText().toString());
				double pounds = kilos*2.2046;
				mresult.setText(new Double(pounds).toString());
			}
			break;
		}
	}
	

	
}

运行Sample时的截图



b 创建android test project.

大致简单的介绍下在Eclipse中创建android test project。

File → New → Other → Android test Project → Next → New Android Test Project,填写Project Name,随意

→ Select Test Target,选择被测试的项目,比如我的是Sample,→ Next 一路下去 →  创建成功。


之后创建一个Testcase 类 SampleUnit ,继承自ActivityInstrumentationTestCase2<Sample> .


接下来要重写父类的方法 setUp()  tearDown() ,以及其他的一些方法

setUp()   在用例运行之前需要先初始化环境. 该方法总是先被调用。

tearDown()   用于回收资源与垃圾清理

testViews ():  确保 SampleJUnit application 能正确的开始执行。

testKilos2Pounds()  测试kilo 转pound是否正确

testPounds2Kilos()   测试pound 转 kilo是否正确

setUp() 代码 (初始化变量和测试环境)

	private Sample mSample;
<span style="white-space:pre">	</span>private TextView mTextView;
<span style="white-space:pre">	</span>private Button mckilo,mcpound;
<span style="white-space:pre">	</span>private EditText mkilo,mpound;
<span style="white-space:pre">	</span>@Override
	protected void setUp()  throws Exception{
		super.setUp();
		mSample = this.getActivity();
		mTextView = (TextView) mSample.findViewById(com.example.samplejunit.R.id.resultview);
		mckilo = (Button) mSample.findViewById(com.example.samplejunit.R.id.Kilos);
		mcpound = (Button) mSample.findViewById(com.example.samplejunit.R.id.Pounds);
		mkilo = (EditText) mSample.findViewById(com.example.samplejunit.R.id.inputvalueK);
		mpound = (EditText) mSample.findViewById(com.example.samplejunit.R.id.inputvalueP);
	}
	

tearDown() 代码

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

testView() 代码

	@SmallTest 
	public void testViews(){
		assertNotNull(getActivity());
		assertNotNull(mTextView);
		assertNotNull(mckilo);
		assertNotNull(mcpound);
		assertNotNull(mkilo);
		assertNotNull(mpound);
	}

关于 smallTest,MediumTest,LargeTest的解释

Small: this test doesn't interact with anyfile system or network.

Medium: Accesses file systems on box whichis running tests.

Large: Accesses external file systems,networks, etc.


@SmallTest
	public void testKilos2Pounds()
	{
		mkilo.clearComposingText();
		//touchutils--can simulate touch event tapview--simulate touching the center of a view
		TouchUtils.tapView(this, mkilo);
		sendKeys("1");  // sending input to mKilos as 1
		//click on the button
		TouchUtils.clickView(this, mcpound); //simulate touching the center of view and release
		double pounds;
		try {
			pounds = Double.parseDouble(mTextView.getText().toString());
		} catch (NumberFormatException e) {
			// TODO: handle exception
			pounds = -1;
		}
		assertTrue("1 kilo is 2.20462262 pounds", pounds > 2.2 && pounds < 2.3);
	}

@SmallTest
	public void testPounds2Kilos()
	{
		mpound.clearComposingText();
		TouchUtils.tapView(this, mpound);
		sendKeys("1");
		TouchUtils.clickView(this, mckilo);
		double kilos;
		try {
			kilos = Double.parseDouble(mTextView.getText().toString());
		} catch (NumberFormatException e) {
			// TODO: handle exception
			kilos = -1;
		}
		assertTrue("1 pound is 0.45359 kilos",kilos > 0.4 && kilos < 0.5);
	}

代码完成啦,该运行test project啦。 

SampleUnit 右键 → Run as  → Android Junit test.



ok ,JUnit 关于android的简单介绍到此为止了,还有关于JUnit android 的文章可以 click here 。

另外如需查看源码,click here 






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值