Junit

什么是 JUnit ?
JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(
regression testing framework)。Junit测试是程序员测试,即所谓
白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完
成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可
以用Junit进行自动测试了。

JUnit的4大功能?
管理测试用例TestSuite
定义测试代码TestCase
定义测试环境
检验测试结果

测试类别?
CI (静态方法测试TU、动态方法测试TI、系统测试TS)

测试数据?
真实数据库数据、内存数据库H2

怎么写测试?
关注类本身的功能、测试不同输入结果、可重复的测试、测试与代码
本身同样重要,需要代码审查

JUnit测试中的一些问题?
Junit测试不是解决所有软件测试中的问题,对于框架中的一些问题需要第三方的类库去解决,比如HTTPUnit、JWebUnit、XMLUnit、GroboUtils多线程测试

Junit4

Test Annotation :
@Before
使用了该元数据的方法在每个测试方法执行之前都要执行一次
@BeforeClass
在所有方法执行之前执行
@After
使用了该元数据的方法在每个测试方法执行之后要执行一次
@AfterClass
在所有方法执行之后执行
@Ignore
该元数据标记的测试方法在测试中会被忽略
@Test
测试方法,顺序未知
@Test(expected=Exception.class) 
通过@Test元数据中的expected属性。expected属性的值是一个异
常的类型
@Test(timeout=xxx): 
该元数据传入了一个时间(毫秒)给测试方法,
 如果测试方法在制定的时间之内没有运行完,则测试也失败

Junit3

需要继承一些类,比如TestCase

测试方法命名必须以小写的test开始

在setUp()方法中,主要是实现一些在测试工作前的资源及环境设置等的初始化设置;而针对方法的测试用例中,需要用户自己编写,一般是以“test+方法名”;

tearDown()在每个测试方法之后运行,用来撤消其初始化的测试环境。


执行当前所有的test case

import junit.framework.Test;
import junit.framework.TestSuite;
import android.test.suitebuilder.TestSuiteBuilder;

public class AllTests2 extends TestSuite{
public static Test suite() {
return new TestSuiteBuilder(AllTests2.class).includeAllPackagesUnderHere().build();
}
}

被测函数

public class SampleCaculator {
public int add(int augend , int addend) {
return augend + addend ;
}
public int subtration(int minuend , int subtrahend) {
return minuend - subtrahend;
}
}


顺序执行Junit测试case

exapmle1:

test suite:

import junit.framework.TestSuite;

public class AllTests3 extends TestSuite{

public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}

public static TestSuite suite() {
TestSuite suite= new TestSuite("Framework Tests");
suite.addTest(test.AllTests.suite());
suite.addTest(test.XMLTest5.suite());
return suite;
}
}

example2:

import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTests extends TestSuite{
public static Test suite() {
TestSuite suite = new TestSuite("TestSuite Test");
suite.addTestSuite(XMLTest.class);
suite.addTestSuite(XMLTest2.class);
return suite;
}
}

测试的 test case

test case1:

import junit.framework.Assert;
import junit.framework.TestCase;

public class XMLTest2 extends TestCase {
protected int i3 = 3;

public void setUp(){
i3 = 5;
}

public void testValue(){
Assert.assertEquals(i3, 5);
}

public void tearDown(){
i3=0;
}


}

test case2:

import junit.framework.TestCase;
import junit.framework.TestSuite;

public class XMLTest5 extends TestCase {
private int fValue1;
private int fValue2;
private int unused;

//@Override
protected void setUp() {
fValue1= 2;
fValue2= 3;
}

public static TestSuite suite() {
return new TestSuite(XMLTest5.class);
}

public void testAdd() {
double result= fValue1 + fValue2;
// forced failure result == 5
assertTrue(result != 6);
}

//@Test(expected=Exception.class)
public void testDivideByZero() throws Exception{
int zero= 0;
int result = zero/8;
//int result= 8/zero;
unused= result; // avoid warning for not using result
assertSame(unused, result);
}
public void testEquals() {
assertEquals(12, 12);
assertEquals(12L, 12L);
assertEquals(new Long(12), new Long(12));
}

}

test case3:

顺序执行当前suitexia的所有测试

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class XMLTest6 extends TestCase {
private static int fValue1;
private static int fValue2;
private static int unused;

//@Override
protected void setUp() {
fValue1= 2;
fValue2= 3;
}

public static TestSuite testSuite() throws Exception {
TestSuite suite= new TestSuite("Test execute according list");
suite.addTest((junit.framework.Test) testDivideByZero());
suite.addTest((junit.framework.Test) testEquals());
suite.addTest((junit.framework.Test) testAdd());
return suite;
}

public static Test testAdd() {
double result= fValue1 + fValue2;
// forced failure result == 5
assertTrue(result != 6);
System.out.println("Test testAdd()");
return null;
}

//@Test(expected=Exception.class)
public static Test testDivideByZero() throws Exception{
int zero= 0;
int result = zero/8;
//int result= 8/zero;
unused= result; // avoid warning for not using result
assertSame(unused, result);
System.out.println("Test testDivideByZero()");
return null;
}

public static Test testEquals() {
assertEquals(12, 12);
assertEquals(12L, 12L);
assertEquals(new Long(12), new Long(12));
System.out.println("Test testEquals()");
return null;
}

}

Android and JUnit

JUnit in Andriod :
只有junit3被支持,
配置测试:
Android测试类需要继承android.test.AndroidTestCase
配置AndroidManifest.xml文件
1)     <uses-libraryandroid:name="android.test.runner" />  说明:与<activity>位置同级
2) <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.testandriod" android:label="Tests for My App" />
说明:与<application>标签同级,targetPackage属性与上面mainfest的package属性内容相同即可 -->


AndroidManifest.xml 配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testandriod"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<uses-library android:name="android.test.runner" />
<activity android:name=".JunitTestActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name="com.example.testandriod.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

<instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.testandriod" android:label="Tests for My App" />

</manifest>

主函数

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

测试test case

import junit.framework.Assert;

import com.example.testandriod.MainActivity;
import com.example.testandriod.R;

import android.test.ActivityInstrumentationTestCase2;
import android.widget.TextView;

public class XMLTest4 extends ActivityInstrumentationTestCase2<MainActivity> {

private TextView result;

public XMLTest4() {
super("test", MainActivity.class);
}

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

MainActivity mainActivity = getActivity();

result = (TextView) mainActivity.findViewById(R.id.action_settings);
}

public void testResult(){
Assert.assertNull(result);
}

}


www.junit.org
http://developer.android.com/sdk/index.html
http://tech.it168.com/a2010/1027/1118/000001118903_all.shtml
http://developer.samsung.com/android/technical-docs/Basics-of-JUnit-in-Android
http://hatsukiakio.blogspot.hk/2009/05/android-15-junit.html
http://my.oschina.net/liux/blog/52469
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值