[英]How to use Junit for testing long arrays?
我剛剛開始進行單元測試(junit)。 我看了一些來自Roy Osherove的視頻,在那里他談到了好的做法。 由於我對這個主題很新,我覺得完全迷失了。 下面是我的代碼的一個簡短示例,然后是我到目前為止測試的方式以及如何創建適合的junit測試。 從本質上講,我正在制作一張帶有52張牌的牌組。 當我創建一個新的“甲板”對象時,我想測試甲板是否包含所有52張牌。
我的代碼中可能有一些錯誤,但它更多的是關於邏輯(最后看問題)。
想象一下,我想創建一個共同的卡片組,其中包括4個套裝和13個等級。 我們還假設我創建了一個“card”類和一個“deck”類,它包含一個實例變量,這是一個由52張卡組成的數組。
“卡”類:
public class card {
private String[] suits = {"h", "s", "c", "d"};
private String[] ranks = {"2","3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"};
private String suit, rank;
public card (int rank, int suit) {
this.suit = this.suits[suit];
this.rank = this.ranks[rank];
}
public String getRank () {
return rank;
}
public String getSuit () {
return suit;
}
}
“甲板”類:
public class deck {
private card[] deckCards = new card[52];
public deck() {
int i = 0;
for(int suit = 0; suit<4; suit++) {
for(int rank=0; rank<13; rank++) {
this.deckCards[i] = new card(rank,suit);
i = i+1;
}
}
}
public card[] getDeckCards() {
return deckCards;
}
}
那么,如果我想創建一個junit測試,我的測試看起來就像是對了嗎?
public class Main {
public static void main(String[] args) {
deck deck = new deck();
int counter = 0;
for (int i = 0; i<52; i++) {
if (deck.deckCards[i] != null) counter++;
}
System.out.println("Cards in deck: " + counter);
for (int i = 0; i<52; i++) {
if (deck.deckCards[i] != null) System.out.println(deck.deckCards[i].getRank() + deck.deckCards[i].getSuit());
}
}
}
結果我會得到這樣的東西:
現在我可以看看所有卡片,我會看到一切都像我想要的那樣。 顯然,這不是你測試的方式..
現在,如果我想創建一個Junit測試,我會做以下事情:
@Test
public void testDeck() {
// create the expected value; which is 52 cards
card[] expected = {new card(0,0),
new card(1,0),
new card(2,0),
... ,
new card(12,0),
new card(0,1),
... ,
new card(12,1),
new card(0,2),
... ,
new card(12,2),
new card(0,3),
... ,
new card(12,3)}
// instantiate the deck
deck carddeck = new deck();
card[] actuals = carddeck.getCardDeck();
//assert
assertArrayEquals(expected, actuals);
}
現在,我有幾個問題:
非常感謝幫助我:)
我會問自己的問題是:
deck
的構造函數),那么可能出現什么問題? 在您的示例中,我將測試以下期望:
Set
,看看是否有內52卡)。 junit中的理論運動員可以更好地完成這項工作。 理論運行器將自動生成測試數據的各種組合。
使用TestedOn注釋。
@RunWith(Theories) public class DeckTest { @Theory public void testCard() { @TestedOn(0, 1, 2, 3) int suit @TestedOn(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) int rank) { Card card = new Card(rank, suit); Deck cardDeck = new Deck(); Card[] cards = cardDeck.getDeckCards(); assertThat(cards, hasItem(card)); // you should static import assertThat and hasItem } }
使用DataPoints和FromDataPoints批注
@RunWith(Theories) public class DeckTest { @DataPoints("suits") public final static int[] SUITS = {0, 1, 2, 3}; @DataPoints("rnaks") public final static int[] RANKS = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; @Theory public void testCard( @FromDataPoints("suits") int suit, @FromDataPoints("ranks") int rank) { Card card = new Card(rank, suit); Deck cardDeck = new Deck(); Card[] cards = cardDeck.getDeckCards(); assertThat(cards, hasItem(card)); } }
你知道你想要這種方法的52張不同的牌,你知道只有52張可能的牌。 因此,您可以創建一個equals()方法來比較套裝和等級 - 當然,測試驅動該方法 - 然后您對getCardDeck的測試可以將數組中的所有卡放入一個集合中。 如果你的設置包含52張牌,你的方法是有效的,因為只有52種可能性,而一套只能容納任意一張卡的一個副本,所以如果你的52元素陣列有任何重復,那么這個集合會更小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.