日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(五)如何写测试类

發布時間:2023/12/20 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (五)如何写测试类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

要討論的類:先UML定義需要的方法?--->?ADT偽代碼,確認操作和設計?---->?考慮特殊情況?--->?實現Java接口(注意寫好注釋)?---->?寫好用來測試的Java語句,更好的理解定義的方法?--->?實現核心方法?----->?測試核心方法?--->?實現其他方法及測試。

分析ArrayBagDemo:

/*** A demostration of the class ArrayBag* @author Administrator**/ public class ArrayBagDemo {public static void main(String[] args) {String[] contentsOfBag = {"A", "A", "B", "A", "C", "A"};// Tests on an empty bagBagInterface<String> aBag = new ArrayBag<>(contentsOfBag.length);System.out.println("Testing an initially empty bag:");testIsEmpty(aBag, true);String[] testStrings1 = {"", "B"};testFrequency(aBag, testStrings1);testContains(aBag, testStrings1);testRemove(aBag, testStrings1);// Adding stringsSystem.out.println("Adding " + contentsOfBag.length + " strings to an initially empty bag "+ "with the capacity to hold more than " + contentsOfBag.length + " strings:");testAdd(aBag, contentsOfBag);// Tests on a bag that is not emptytestIsEmpty(aBag, false);String[] testStrings2 = {"A", "B", "C", "D", "A"};testFrequency(aBag, testStrings2);testContains(aBag, testStrings2);// Removing stringsString[] testStrings3 = {"", "B", "A", "C", "Z"};testRemove(aBag, testStrings3);System.out.println("\nClearing the bag:");aBag.clear();testIsEmpty(aBag, true);displayBag(aBag);}// Tests the method add.public static void testAdd(BagInterface<String> aBag, String[] content) {System.out.println("Adding ");for(int index = 0; index < content.length; index++) {aBag.add(content[index]);System.out.print(content[index] + " ");} // end for System.out.println();displayBag(aBag);} // end testAddprivate static void testRemove(BagInterface<String> aBag, String[] tests) {for (int index = 0; index < tests.length; index++) {String aString = tests[index];if (aString.equals("") || (aString == null)) {// test remove()System.out.println("\nRemoving a string from the bag:");String removedString = aBag.remove();System.out.println("remove() returns " + removedString);}else {// test remove(aString)System.out.println("\nRemoving \"" + aString + "\" from the bag:");boolean result = aBag.remove(aString);System.out.println("remove(\"" + aString + "\") returns " + result);} // end if displayBag(aBag);} // end for } // end testRemove// Tests the method toArray while displaying the bag.private static void displayBag(BagInterface<String> aBag) {System.out.println("The bag contains " + aBag.getCurrentSize() +" string(s), as follows:");Object[] bagArray = aBag.toArray();for (int index = 0; index < bagArray.length; index++) {System.out.print(bagArray[index] + " ");} // end for System.out.println();} // end diaplayBag// Tests the method contains.private static void testContains(BagInterface<String> aBag, String[] tests) {System.out.println("\nTesting the method contains:");for (int index = 0; index < tests.length; index++) {String aString = tests[index];if (!aString.equals("") && (aString != null)) {System.out.println("Does this bag contain " + tests[index] +" ? " + aBag.contains(aString));} // end if} // end for} // end testContains// Tests the method getFrequencyOfprivate static void testFrequency(BagInterface<String> aBag, String[] tests) {System.out.println("\nTesting the method getFrequencyOf:");for (int index = 0; index < tests.length; index++) {String aString = tests[index];if (!aString.equals("") && (aString != null)) { System.out.println("In this bag, the count of " + tests[index] +" is " + aBag.getFrequencyOf(aString));} // end if} // end for} // end testFrequency// Tests the method isEmpty// correctResult indicates what isEmpty should return.private static void testIsEmpty(BagInterface<String> aBag, boolean correctResult) {System.out.println("Testing isEmpty with ");if(correctResult) {System.out.println("an empty bag:");}else {System.out.println("a bag that is not empty:");} // end if System.out.print("isEmpty finds the bag ");if(correctResult && aBag.isEmpty()) {System.out.println("empty: OK.");}else if(correctResult) {System.out.println("not empty, but it is empty: ERROR.");}else if(!correctResult && aBag.isEmpty()) {System.out.println("empty, but it is not empty: ERROR.");}else {System.out.println("not empty: OK.");} // if else System.out.println();} // end testIsEmpty } View Code

1)判空isEmpty(): Boolean

設計函數testIsEmpty,將包和是否真正為空的事實作為參數,利用isEmpty函數和事實對bag做判斷。

2)測試getFrequencyOf

利用包和字符串數組做參數,判斷數組中的元素在包中出現的次數并輸出

3)測試contains函數

參數為包和數組,判斷包中是否包含數組中的元素并輸出

4)測試remove

將包和測試數組作為參數,如果當前數組元素是空串或是null,則測試remove(),否則用remove(T),最后再輸出包中所有元素

5)測試add

同樣將包和測試數組作為參數,依次將數組中元素添加進包中,再輸出包中所有元素

6)都要使用的輸出包中元素的函數displayBag

7)main函數中:

  首先測試空包:判空、測試數組中元素的個數、是否包含數組中元素、移除元素 ---> 向空包中添加元素,測試add函數?---> 當包不為空時,繼續測試:判空、元素個數、是否包含等函數?---> 從包中移除元素,測試remove函數?----> 將包清空,通過判空可以測試clear()

8)總結

  各個測試方法中均為將函數執行后輸出包中元素,對于判空的測試需要使用事實條件繼續對函數返回值進行詳細判斷說明。main函數測試時,先從空包開始測試除add以外的函數,再向包添加元素測試add,隨后在包不為空的情況下繼續測試所有函數,最后情況,通過判空測試clear函數。

轉載于:https://www.cnblogs.com/datamining-bio/p/9630373.html

總結

以上是生活随笔為你收集整理的(五)如何写测试类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。