單元測(cè)試
Unit Testing,是指對(duì)軟件中的最小可測(cè)試單元進(jìn)行檢查和驗(yàn)證。
誤解
- 編寫單元測(cè)試沒(méi)有用并且浪費(fèi)大量的開(kāi)發(fā)時(shí)間,延遲開(kāi)發(fā)進(jìn)度
- 從沒(méi)寫過(guò),不會(huì)寫,不影響產(chǎn)品功能
實(shí)際
好的測(cè)試能避免開(kāi)發(fā)中遇到的80%以上奇奇怪怪的問(wèn)題
促進(jìn)編寫出模塊化、松耦合高內(nèi)聚的優(yōu)質(zhì)代碼,減少代碼重構(gòu)
測(cè)試框架
AndroidJUnitRunner:兼容JUnit 4測(cè)試運(yùn)行器
Espresso:UI測(cè)試框架;適合在單個(gè)應(yīng)用的功能UI測(cè)試
UI Automator:UI測(cè)試框架;適用于跨應(yīng)用的功能UI測(cè)試及安裝應(yīng)用
AndroidJunitRunner
Enviroment搭建
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
}
Tips關(guān)鍵
InstrumentationRegistry.getInstrumentation()返回當(dāng)前正在運(yùn)行的Instrumentation
InstrumentationRegistry.getContext()返回此Instrumentation軟件包的上下文。
InstrumentationRegistry.getTargetContext()返回目標(biāo)應(yīng)用的應(yīng)用上下文。
InstrumentationRegistry.getArguments()返回傳遞給此Instrumentation的參數(shù)Bundle。
當(dāng)測(cè)試使用JUnit4時(shí),需要注解@RunWith(AndroidJUnit4.class)
@Before:測(cè)試方法每次執(zhí)行Test方法之前都會(huì)執(zhí)行的方法注解,該注解替代了JUnit 3中的setUp()方法。
@Test:測(cè)試方法體注解
@After:測(cè)試方法每次執(zhí)行完一個(gè)Test方法后都會(huì)執(zhí)行的方法注解,該注解替代了JUnit 3中的tearDown()方法。
@Rule: 簡(jiǎn)單來(lái)說(shuō),是為各個(gè)測(cè)試方法提供一些支持。具體來(lái)說(shuō),比如我需要測(cè)試一個(gè)Activity,那么我可以在@Rule注解下面采用一個(gè)ActivityTestRule,該類提供了對(duì)相應(yīng)Activity的功能測(cè)試的支持。該類可以在@Before和@Test標(biāo)識(shí)的方法執(zhí)行之前確保將Activity運(yùn)行起來(lái),并且在所有@Test和@After方法執(zhí)行結(jié)束之后將Activity殺死。在整個(gè)測(cè)試期間,每個(gè)測(cè)試方法都可以直接對(duì)相應(yīng)Activity進(jìn)行修改和訪問(wèn)。
@BeforeClass: 為測(cè)試類標(biāo)識(shí)一個(gè)static方法,在測(cè)試之前只執(zhí)行一次。
@AfterClass: 為測(cè)試類標(biāo)識(shí)一個(gè)static方法,在所有測(cè)試方法結(jié)束之后只執(zhí)行一次。
@Test(timeout=<milliseconds>): 為測(cè)試方法設(shè)定超時(shí)時(shí)間。
@RequiresDevice:物理設(shè)備上運(yùn)行。
@SdkSupress:限定最低SDK版本。例如@SDKSupress(minSdkVersion=18)。
@SmallTest,@MediumTest和@LargeTest:測(cè)試分級(jí)。
Demo示例
不管是繼承AndroidTestCase還是ActivityInstrumentationTestCase2,在Android中都顯示方法已經(jīng)過(guò)時(shí),因此我們什么都不繼承,比如
package com.ziv.zutils;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.LargeTest;
import android.support.test.filters.MediumTest;
import android.support.test.filters.RequiresDevice;
import android.support.test.filters.SdkSuppress;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a >Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Before
public void testBefore() throws Exception {
LogUtil.e("JUnit","testBefore");
}
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
LogUtil.e("JUnit","testTest");
assertEquals("com.ziv.zutils", appContext.getPackageName());
}
@Test
public void testTest() throws Exception {
LogUtil.e("JUnit","testTest");
}
@Test
@SmallTest
public void testTestSmallTest() throws Exception {
LogUtil.e("JUnit","testTestSmallTest");
}
@SmallTest
public void testSmallTest() throws Exception {
LogUtil.e("JUnit","testSmallTest");
}
@MediumTest
public void testMediumTest() throws Exception {
LogUtil.e("JUnit","testMediumTest");
}
@LargeTest
public void testLargeTest() throws Exception {
LogUtil.e("JUnit","testLargeTest");
}
@RequiresDevice
public void testRequiresDevice() throws Exception {
LogUtil.e("JUnit","testRequiresDevice");
}
@SdkSuppress(minSdkVersion = 19)
public void testSdkSuppress() throws Exception {
LogUtil.e("JUnit","testSdkSuppress");
}
@After
public void testAfter() throws Exception {
LogUtil.e("JUnit","testAfter");
}
}
使用adb命令進(jìn)行測(cè)試,分成10個(gè)碎片,僅執(zhí)行第二片測(cè)試,請(qǐng)使用:
adb shell am instrument -w -e numShards 10 -e shardIndex 2
Espresso
Enviroment搭建
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
另外還需要hamcrest的庫(kù),用來(lái)和Espresso配合使用
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
Tips關(guān)鍵
Run-->Record Espresso Test
- onView()找元素
- perform()操作元素
- check()檢查結(jié)果
Demo示例
API參考:developer.android.com/reference/android/support/test/package-summary.html
測(cè)試參考:http://developer.android.com/training/testing/ui-testing/espresso-testing.html
UI Automator
Enviroment搭建
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
Tips關(guān)鍵
UI Automator僅支持Android 4.3(API Level 18)及以上版本。
使用流程
- 獲得一個(gè)UiDevice對(duì)象,代表我們正在執(zhí)行測(cè)試的設(shè)備。該對(duì)象可以通過(guò)一個(gè)getInstance()方法獲取,入?yún)橐粋€(gè)Instrumentation對(duì)象:
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); - 通過(guò)findObject()方法獲取到一個(gè)UiObject對(duì)象,代表我們需要執(zhí)行測(cè)試的UI組件。
- 對(duì)該UI組件執(zhí)行一系列操作。
- 檢查操作的結(jié)果是否符合預(yù)期。
Demo示例
API參考:http://developer.android.com/reference/android/support/test/package-summary.html
實(shí)例:http://developer.android.com/training/testing/ui-testing/uiautomator-testing.html
谷歌安卓測(cè)試實(shí)例介紹
AndroidJunitRunnerSample
實(shí)例下載地址:https://github.com/googlesamples/android-testing/tree/master/runner/AndroidJunitRunnerSample
高效單元測(cè)試
代碼的可讀性
- 使用更易懂的API,如
//代碼一
String msg = “hello,World”;
assertTrue(msg.indexOf(“World”)!=-1);
//代碼二
String msg = “hello,World”;
assertThat(msg.contains(“World”),equals(true));
明顯代碼二的邏輯更符合人類思想,更容易理解
- 避免使用較底層的方式,比如位運(yùn)算符
//代碼一
assertTrue(Platform.IS_32_BIT ^ Platform.IS_64_BIT);
//代碼二
assertTrue("Not 32 or 64-bit platform?", Platform.IS_32_BIT || Platform.IS_32_BIT);
assertFalse("can't be 32 and 64-bit at the same time.",Platform.IS_32_BIT && Platform.IS_32_BIT);
當(dāng)99%的人類可以在3s內(nèi)判斷出代碼一是在做什么的時(shí)候,我們就可以使用代碼一,而不是邏輯更清楚的代碼二了
代碼的可維護(hù)性
- 不要在測(cè)試代碼中運(yùn)用防御性策略
Data data = project.getData();
//代碼一
assertNotNull(data);// 沒(méi)有任何實(shí)際意義
assertEquals(4,data.count());
//代碼二
assertNotNull(data);// 可測(cè)試出data.getSummary()是否為空的情況
assertEquals(4,data.getSummary().getTotal())
- 減少重復(fù)性復(fù)制粘貼的代碼
//代碼一
public class TemplateTest(){
@Test
public void emptyTemplate() throws Exception{
String template=“”;
assertEquals(template,new Template(template).getType());
}
@Test
public void plainTemplate() throws Exception{
String template=“plaintext”;
assertEquals(template,new Template(template).getType());
}
}
//代碼二
public class TemplateTest(){
@Test
public void emptyTemplate() throws Exception{
assertTemplateType(“”);
}
@Test
public void plainTemplate() throws Exception{
assertTemplateType(“plaintext”);
}
private void assertTemplateType(String template){
assertEquals(template,newTemplate(template).getType())
}
}
- 避免由于條件邏輯而造成的測(cè)試遺漏,存在條件邏輯時(shí)要在最后加上 fail()方法,強(qiáng)制測(cè)試失敗
//代碼一
public class DictionaryTest{
@Test
public void testDictionary() throws Exception{
Dictionary dict = new Dictionary();
dict.add(“A”,new Long(3));
dict.add(“B”,”21”);
for(Iterator e = dict.iterator();e.hasNext()){
Map.Entry entry = (Map.Entry) e.next();
if(“A”.equals(entry.getKey()))
asserEquals(3L,entry.getValue());
if(“B”.equals(entry.getKey()))
assertEquals(“21”),entry.getValue();
}
}
}
//代碼二
public class DictionaryTest{
@Test
public void testDictionary() throws Exception{
Dictionary dict = new Dictionary();
dict.add(“A”,new Long(3));
dict.add(“B”,”21”);
assertContain(dict.iterator(),”A”,3L);
assertContain(dict.iterator(),”B”,21);
}
private void assertContain(Iterator i,Object key,Object value){
while(i.hasNext()){
Map.Entry entry = (Map.Entry)i.next();
if(key.equals(entry.getKey())){
assertEquals(value,entry.getValue());
return;
}
}
fail("Iterator didn't contain "+ key);
}
}
代碼一當(dāng)Iterator為空時(shí),測(cè)試并不會(huì)失敗,這并不符合我們單元測(cè)試的目的
- 避免使用sleep方法浪費(fèi)大量的測(cè)試時(shí)間
counterAccessFromMultipleThreads 用來(lái)測(cè)試一個(gè)多線程計(jì)數(shù)器,開(kāi)啟10個(gè)線程,每個(gè)線程調(diào)用計(jì)數(shù)器1000次,sleep(500),是為了讓主線程等待開(kāi)啟的10個(gè)線程執(zhí)行完畢
那么問(wèn)題來(lái)了,如果在10毫秒內(nèi)所有線程都執(zhí)行完畢,豈不白白浪費(fèi)了490毫秒?又或者在等待500毫秒后仍有線程沒(méi)有執(zhí)行完畢,那該怎么辦?
@Test
public class counterAccessFromMultipleThreads{
final Counter counter = new Counter();
final int callsPerThread = 1000;//每個(gè)線程調(diào)用計(jì)數(shù)器1000次
final Set<Long> values = new HashSet<Long>();
Runnable runnable = new Runnable(){
public void run(){
for(int i=0;i<callsPerThread;i++){
values.add(counter.getAndIncrement());
}
}
};
int threads = 10;//開(kāi)啟10個(gè)線程
for(int i=0;i<threads;i++){
new Thread(runnable).start();
}
Thread.sleep(500);
int exceptedNoOfValues = threads * callsPerThread;
assertEquals(exceptedNoOfValues ,values.size());
}
改進(jìn)后的測(cè)試方法:
public class counterAccessFromMultipleThreads{
final Counter counter = new Counter();
final int callsPerThread = 1000;
final int numberOfthreads = 10;
final CountDownLatch allThreadsComplete = new CountDownLatch(numberOfthreads);
final Set<Long> values = new HashSet<Long>();
Runnable runnable = new Runnable(){
public void run(){
for(int i=0;i<callsPerThread;i++){
values.add(counter.getAndIncrement());
}
allThreadsComplete.countDown();
}
};
for(int i=0;i<numberOfthreads;i++){
new Thread(runnable).start();
}
allThreadsComplete.await();
// allThreadsComplete.await(10,TimeUnit.SECONDS);
int exceptedNoOfValues = threads * callsPerThread;
assertEquals(exceptedNoOfValues ,values.size());
}
等待所有線程結(jié)束后再繼續(xù)執(zhí)行,有更好的辦法,java.util.concurrent 包中的CountDownLatch類完全可以勝任這項(xiàng)工作。
調(diào)用await方法開(kāi)始阻塞,直到所有的線程都通知完成,然后繼續(xù)執(zhí)行主線程代碼。也可以設(shè)置超時(shí)時(shí)間,allThreadsComplete.await(10,TimeUnit.SECONDS); 如果10秒鐘內(nèi)子線程仍未執(zhí)行結(jié)束,也會(huì)繼續(xù)執(zhí)行主線程。
- 避免歧義注釋
/**
* 功能描述: 發(fā)送郵件<br>
* 〈功能詳細(xì)描述〉
* @return
* @see [相關(guān)類/方法](可選)
* @since [產(chǎn)品/模塊版本](可選)
*/
public void sendShortMessage() {
//todo
}
// 代碼二
public void sendEmail() {
//todo
}
sendShortMessage不是說(shuō)發(fā)送短信么?注釋又寫發(fā)送郵件…這樣的注釋真不如不要寫了…
- 避免永不失敗的測(cè)試
//代碼一
@Test
public void includeForMissingResourceFails()
try{
new Environment().include("somethingthatdoesnotexist");
}catch(IOException e){
assertThat(e.getMesssage(),contians(“FileNotExist”));
}
//代碼二
public void includeForMissingResourceFails()
try{
new Environment().include(“FileNotEixst”);
// 除非拋出期望的異常,否則測(cè)試失敗
fail();
}catch(IOException e){
assertThat(e.getMesssage(),contians(“FileNotExist”));
}
代碼一中當(dāng)程序發(fā)生異常時(shí),異常被catch捕獲,測(cè)試通過(guò)。程序沒(méi)有發(fā)生異常時(shí),程序正常執(zhí)行完畢,測(cè)試也是通過(guò)的,發(fā)現(xiàn)不了問(wèn)題
遵守的原則
- 少用繼承多用組合,繼承更大程度上是為了多態(tài)而非復(fù)用代碼
- 單元測(cè)試應(yīng)該模塊化,每個(gè)模塊小而專注,減少反饋鏈
- 單一職責(zé),如果一個(gè)單元測(cè)試方法失敗了,那么導(dǎo)致它失敗的原因只有一個(gè)
- 加載外部文件時(shí)使用相對(duì)路徑而不是絕對(duì)路徑
- 見(jiàn)名知意的定義常量,而不是魔法數(shù)字
- 完整的方法注釋,說(shuō)明測(cè)試的內(nèi)容,使用的方法,避免注釋歧義等
測(cè)試驅(qū)動(dòng)開(kāi)發(fā)

參考資料:
https://my.oschina.net/u/1433482/blog/602003
https://segmentfault.com/a/1190000004338384
http://www.cnblogs.com/jarman/p/5272761.html