For this week's labs and assignments, we'll start creating the classes to implement the Nine Card Golf game. Following good practice, we're going to work on the game data and logic first, without worrying about the graphical user interface elements yet. We're also using Unit Testing, an industry standard practice for writing correct, maintainable code. In this assignment, you will:
Card
and Deck
classes for the gameDownload the sample unit test project from lab here. Unzip and open the solution (.sln) file in the UnitTesting2 folder. (VS2015, should convert to 2017 no trouble.)
Unit tests are simple functions that test other classes and methods. As we walked through in lab, Visual Studio has tools to manage those tests rather than just having them in main
the way you have in the past. Creating unit tests in VS creates a second project in your solution, with the test code for each class and method in your main project.
Your Solution Explorer should look like the picture here. UnitTesting2
is my main project, and UnitTesting2Tests
is the one that was generated.
In Program.cs
I have a class called Test
, so in the testing project there is a unit test file called TestTests.cs
.
Test
has a method called Output()
, so in TestTest.cs
there is a method called OutputTest()
.
In order to run and manage your tests, you open the Test Exporer (Test Viewer in VS2008) from the Tests menu. You can see in the picture that it lists my tests and has the option to Run All.
You can follow a Microsoft tutorial on setting up unit tests here.
Create a new console project for your Card
and Deck
classes. In Visual Studio 2017, you must choose a .Net Framework project rather than .Net Core. Otherwise you won't have access to the unit test tools.
As we discussed, these classes should support the following data and methods:
Card
Value
: the card number (A,2,3,K,etc), stored as int
, char
or an Enum
Suit
: the card suit (Spades, Hearts, etc), stored as char
or an Enum
Deck
List<>
of Cards
Deck()
: constructor, to create a standard 52-card deck plus jokersShuffle()
: randomly rearrange the cards in the deckDraw()
: remove and return the top card in the deckFor this assignment, you must implement both classes and create unit tests for all the Deck
methods that prove that they work. As we discussed, you don't have to check every single card in the deck after creating and shuffling, just enough to be reasonably confident.
For testing purposes, you may want to add a Peek
method that returns any card in the deck by position (without removing it) and a Add
method to let you put cards back on top of the deck.
As with the GUI assignment, you'll need to commit the whole solution here. That means both project subdirectories! Please delete the obj
and bin
directories from both. VS2017 additionally creates frameworks subdirectories which should also not be committed (if you can't find them, don't worry about it this time).
https://cssvn.utrgv.edu/svn_etomai/201810_3328/assn04_testing/<username>