JUnit是一个Java语言的单元测试框架。它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个。 JUnit有它自己的JUnit扩展生态圈。多数Java的开发环境都已经集成了JUnit作为单元测试的工具。
JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了。
回归测试是指修改了旧代码后,重新进行测试以确认修改没有引入新的错误或导致其他代码产生错误。自动回归测试将大幅降低系统测试、维护升级等阶段的成本。回归测试作为软件生命周期的一个组成部分,在整个软件测试过程中占有很大的工作量比重,软件开发的各个阶段都会进行多次回归测试。在渐进和快速迭代开发中,新版本的连续发布使回归测试进行的更加频繁,而在极端编程方法中,更是要求每天都进行若干次回归测试。因此,通过选择正确的回归测试策略来改进回归测试的效率和有效性是非常有意义的。
Android Testing
android里面的的单元测试本质上和junit差不多,先来看一下例子:android sdk sample snake的测试(导入的时候test报sdk version 最低是8现在是1 错误,直接project->Android Tools->clear link marks)
首先是menifest.xml文件
1 在application tag中指明我们需要android.test库包,构建测试用例的时候需要用到它
2 <instrumentation> 标签 android:name 、android:targetPackage android:lable
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.snake.tests">
<!-- We add an application tag here just so that we can indicate that
this package needs to link against the android.test library,
which is needed when building test cases. -->
<application>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.android.snake"
android:label="Snake sample tests">
</instrumentation>
</manifest>
package com.example.android.snake;
import android.test.ActivityInstrumentationTestCase2;
/**
* Make sure that the main launcher activity opens up properly, which will be
* verified by {@link #testActivityTestCaseSetUpProperly}.
*/
public class SnakeTest extends ActivityInstrumentationTestCase2<Snake> {
/**
* Creates an {@link ActivityInstrumentationTestCase2} for the {@link Snake} activity.
*/
public SnakeTest() {
super(Snake.class);
}
/**
* Verifies that the activity under test can be launched.
*/
public void testActivityTestCaseSetUpProperly() {
assertNotNull("activity should be launched successfully", getActivity());
}
}
参考:http://blog.csdn.net/wangpeng047/article/details/9628449