简体   繁体   English

使用断言的单元测试用例

[英]Unit Test case using Assert

The scenario I have is to Draw the specified number of cards from the deck and return them in sorted order (first by suit, then by face value). 我要解决的情况是从卡组中抽出指定数量的卡,然后按排序顺序将它们退回(先按西装,再按面值)。

<param name="howMany">how many cards to draw.
<returns>a list of drawn cards, sorted by suit and face value (rank)</returns>

I have coded this Method to achieve it.I have to write an Unit Test code for it. 我已经为实现该方法编写了代码,我必须为此编写一个单元测试代码。 what can be possible unit test for this as It has a list to compare and how to achieve that. 对此有什么可能的单元测试,因为它具有要比较的列表以及如何实现此目的。

public List<Card> DrawSorted(int howMany)
{
    List<Card> drawnCards = _cards.Take(howMany).ToList();
    IEnumerable<Card> sortedCards = from card in drawnCards
                                            orderby card.Order ascending
                                            select card;
    return sortedCards.ToList();
}

Any help is much appreciated 任何帮助深表感谢

You should make tests as simple as possible, and should probably make several tests here: 您应该使测试尽可能简单,并且可能应该在此处进行多个测试:

Some rough ideas: 一些粗略的想法:

  • One to verify that you draw and return the correct number of cards 一个来验证您是否抽签并返回正确数量的卡
  • One to check that if you draw cards that you know are in the correct order to begin with, you will still get them back in the correct order 检查您是否以正确的顺序抽出知道的牌,仍然可以按照正确的顺序取回它们
  • One to check that cards that are NOT in the correct order will be returned in the correct order. 检查卡顺序不正确的人将以正确的顺序退回。

A simple way to check ordering is by just drawing a small umber of cards (maybe even just two?), and comparing those; 一种检查订购的简单方法是,仅抽取一小张卡片(也许只有两张?),然后进行比较。 that will let you keep the test-logic as short and simple as possible, while still being able to give a warning if something is wrong. 这样一来,您就可以使测试逻辑尽可能短而简单,同时在出现问题时仍然能够发出警告。

Of course the better process would have been to specify your most simple requirements in the form of tests to begin with, and then make them more advanced bit by bit, while adding logic to your method as you go. 当然,更好的过程是以测试的形式指定最简单的需求,然后逐步将它们提高一点,同时为您的方法添加逻辑。 You might then have tests named something like this (you can probably guess what they might contain): 然后,您可能会有名为以下名称的测试(您可能猜出它们可能包含的内容):

public void should_return_correct_nr_of_cards(){ ... }

public void should_return_presorted_cards_in_correct_order(){ ... }

public void should_return_unsorted_cards_sorted(){ ... }

The first question you have to ask is "What do I want to test?". 您必须问的第一个问题是“我要测试什么?”。 Are you testing that all the cards are different? 您是否正在测试所有卡都不同? Are you testing what happens when you draw more cards than there are? 您是否正在测试当您抽出更多的牌时会发生什么? Myself I'd test both (and a few other edge case scenarios). 我自己会同时测试这两种情况(以及其他一些极端情况)。

The second thing you have to do is provide some mechanism for mocking the list of cards to draw from. 您要做的第二件事是提供某种机制来模拟可从中抽奖的牌列表。 For a test to be valuable you need to be able set the conditions of the test each time. 为了使测试有价值,您需要每次都能设置测试条件。

My suggestion would be to create your test class like so: 我的建议是像这样创建测试类:

public class Deck
{
  private List<Card> _cards;

  public Deck(List<Card> allCards)
  {
    _cards = allCards;
  }

  public List<Card> DrawSorted(int howMany)
  {
    List<Card> drawnCards = _cards.Take(howMany).ToList();
    IEnumerable<Card> sortedCards = from card in drawnCards
                                            orderby card.Order ascending
                                            select card;
    return sortedCards.ToList();
  }
}

In your test class you could pass in the complete list of cards you want to use for your test. 在您的测试课程中,您可以输入要用于测试的卡片的完整列表。

Depending on the test framework you're using the tests themselves will be slightly different. 根据测试框架的不同,您使用测试本身也会有所不同。 But here's an example: 但这是一个例子:

[Test]
public void CheckCorrectNumberOfCardsAreReturned()
{
  List<Card> allCards = GenerateDeck();
  var deck = new Deck(allCards);
  var drawnCards = deck.DrawSorted(5);
  Assert.AreEqual(5, drawnCards.Count());
}

For your testing to be truly valuable what happens if you try and draw 55 cards? 为了使您的测试真正有价值,如果您尝试抽出55张卡片会发生什么? Should you get 55 or 52 (assuming a standard deck of cards)? 您应该得到55还是52(假设是一副标准的纸牌)?

If I have duplicates in my allCards should I get duplicates when I draw them or should some be removed? 如果我的allCard中有重复项,我在抽签时应该得到重复项还是应该将其删除?

Testing is tied directly into requirements - work out what your method should do and design your tests to ensure your code meets them . 测试直接与需求联系在一起- 确定您的方法应该做什么并设计测试以确保您的代码符合要求

A couple of final points: 最后几点:

  • One Test should test one edge case/piece of behavior, this often results in multiple tests per method 一项测试应该测试一种情况/行为,这通常会导致每种方法进行多次测试
  • A test should have known starting conditions (you can configure your deck to give the test conditions you want) 测试应具有已知的开始条件(您可以配置平台以提供所需的测试条件)
  • A test should be repeatable, be wary of things like random numbers. 测试应该是可重复的,对诸如随机数之类的东西要当心。 Draw a random card from two deck and check they're not the same? 从两个卡组中抽出一张随机卡,并检查它们是否不相同? 1/52 they could be! 他们可能是1/52! These sort of tests are a nightmare to diagnose. 这些测试是诊断的噩梦。

I hope this helps! 我希望这有帮助!

I think that one possible unit test is to check if the result is already sorted after your DrawSorted is called. 我认为一种可能的单元测试是在调用DrawSorted之后检查结果是否已经排序。

I would create other method called IsSorted, returning a bool, and then use this method to test the output. 我将创建另一个称为IsSorted的方法,返回一个布尔值,然后使用此方法测试输出。

Something like: 就像是:

bool expectedAnswer = true;

List<Cards> list = yourClass.DrawSorted(15);
bool result = yourClass.IsSorted(list);

Assert.AreEqual(expectedResult, result);

Or something like this. 或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM