SSE657+Project+2+TDD

= = =Project 2 - Test Driven Development (TDD)= Test-Driven Development is philosophy that gets the programmer to develop test for the program before ever writing the code. In this section of the project I have created Unit Test for each class of the Sudoku Game.

My Soapbox
I strongly disagree with this approach for myself because this is not the way my brain works. Maybe for a new programmer in college who is first learning to design programs, this method may be beneficial. I agree that testing is often left as an afterthought but is a critical portion of any program. At my current job, we write Unit Test after each segment (or project inside a solution) is thought to be finished. If code changes inside of a segment, then the Unit Test are updated according. This method works well for us and our customers.

SudokuGameTest Project
I added a Visual Studio Test Project inside of the current solution for the Sudoku Game. Visual Studio creates accessors that allow test to be run on another project inside of a solution, without holding to the conventional declarations for methods, variables, and properties (i.e. private, internal, protected). Below is picture of the solution.



SudokuTest
The Sudoku Class is the main class for the program which handles and calls all other classes. The SudokuTest class tests each method and property of the Sudoku class. Notice the private method SignInAsPlayer method. This method is created inside of the SudokuTest Class that gets used by most of the Unit Test inside SudokuTest. This was done to keep from retyping the same code and also (and more importantly) to keep the same sign in steps in every test. This allows the individual test to actually test what they are intended to test inside of also testing the Sign In progress every time, since the Sign In progress is tested in the SignInTest Class. code format="csharp" using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Sudoku_Game;

namespace SudokuGameTest {   ///     /// Summary description for UnitTest ///    [TestClass] public class SudokuTest {       public SudokuTest {

}

private TestContext testContextInstance;

///        ///Gets or sets the test context which provides ///information about and functionality for the current test run. ///        public TestContext TestContext {           get {               return testContextInstance; }           set {               testContextInstance = value; }       }

private Sudoku SignInAsPlayer {           Sudoku sud = new Sudoku(false); SignIn sign = new SignIn(sud); sign.SignInAsPlayer("tim", "tim"); return sud; }

[TestMethod] [DeploymentItem("Sudoku Game.exe")] public void UserIDTest {           Sudoku sud = SignInAsPlayer; Assert.AreEqual(sud.UserID, "1"); }

[TestMethod] [DeploymentItem("Sudoku Game.exe")] public void CurrentUserPlayerTest {

Sudoku sud = SignInAsPlayer; Assert.AreEqual(sud.CurrentUser, UserStatus.Player); }

[TestMethod] [DeploymentItem("Sudoku Game.exe")] public void CurrentUserGuestTest {

Sudoku sud = new Sudoku(false); SignIn sign = new SignIn(sud); sign.SignInAsGuest; Assert.AreEqual(sud.CurrentUser, UserStatus.Guest); }   } }

code

SignInTest
The SignInTest class tests the Sign In procedure for the program which is called at the launch of the program. There is only one test in this class but inside the one test, two separate test are being perform on the two ways that a user can sign into the program.

code format="csharp" using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Sudoku_Game; using System.Windows.Forms;

namespace SudokuGameTest {

///    /// Summary description for SignInTest ///    [TestClass] public class SignInTest {       public SignInTest {           //            // TODO: Add constructor logic here //       }

private TestContext testContextInstance;

///        ///Gets or sets the test context which provides ///information about and functionality for the current test run. ///        public TestContext TestContext {           get {               return testContextInstance; }           set {               testContextInstance = value; }       }

[TestMethod] public void LogInTest {           Sudoku sudoku = new Sudoku(false); Assert.IsNotNull(sudoku); SignIn signIn = new SignIn(sudoku); Assert.IsNotNull(signIn); signIn.SignInAsGuest; Assert.AreEqual(sudoku.CurrentUser, UserStatus.Guest); signIn = new SignIn(sudoku); Assert.IsNotNull(signIn); signIn.SignInAsPlayer("tim", "tim"); Assert.AreEqual(sudoku.UserID, "1"); Assert.AreEqual(sudoku.CurrentUser, UserStatus.Player); }   } }

code

GridTest
The GridTest is largest class of the testing because the Grid class holds the main functionality of the game once a user is signed in and picks a game to play. Since most of the Grid class' members are private the use of accessors can easily be seen inside this class. Without the Grid_Accessor that is created by Visual Studio, these test would never compile. When trying to use a private member, I use: code format="csharp" Grid_Accessor grid = new Grid_Accessor; code instead of the normal way: code format="csharp" Grid grid = new Grid; code Below is the GridTest Class code. code format="csharp" using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Sudoku_Game;

namespace SudokuGameTest {

///    ///This is a test class for GridTest and is intended ///to contain all GridTest Unit Tests ///    [TestClass] public class GridTest {

private TestContext testContextInstance;

///        ///Gets or sets the test context which provides ///information about and functionality for the current test run. ///        public TestContext TestContext {           get {               return testContextInstance; }           set {               testContextInstance = value; }       }

///        ///A test for Cells ///        [TestMethod] [DeploymentItem("Sudoku Game.exe")] public void CellsTest {           Grid_Accessor grid = new Grid_Accessor; List cells = grid.Cells; Assert.AreEqual(81, cells.Count);

}

///        ///A test for GetCellsInSubSquare ///        [TestMethod] [DeploymentItem("Sudoku Game.exe")] public void GetCellsInSubSquareTest {           Grid_Accessor grid = new Grid_Accessor; int subSquare = 1; List sub = grid.GetCellsInSubSquare(subSquare); foreach (Cell cell in sub) Assert.AreEqual(cell.SubSquare, subSquare); }

///        ///A test for GetCellsInRow ///        [TestMethod] [DeploymentItem("Sudoku Game.exe")] public void GetCellsInRowTest {           Grid_Accessor grid = new Grid_Accessor; int row = 4; List actual = grid.GetCellsInRow(row); foreach(Cell cell in actual) Assert.AreEqual(cell.Row, row); }

///        ///A test for GetCellsInColumn ///        [TestMethod] [DeploymentItem("Sudoku Game.exe")] public void GetCellsInColumnTest {           Grid_Accessor grid = new Grid_Accessor; int col = 3; List actual = grid.GetCellsInColumn(col); foreach (Cell cell in actual) Assert.AreEqual(cell.Column, col); }

///        ///A test for GetCellAt ///        [TestMethod] [DeploymentItem("Sudoku Game.exe")] public void GetCellAtTest {           Grid_Accessor target = new Grid_Accessor; int row = 7; int col = 8; Cell actual = target.GetCellAt(row, col); Assert.AreEqual(row, actual.Row); Assert.AreEqual(col, actual.Column); }

///        ///A test for GetAvailableNumbers ///        [TestMethod] [DeploymentItem("Sudoku Game.exe")] public void GetAvailableNumbersTest {           Grid_Accessor target = new Grid_Accessor; //Fill in Grid for (int i = 0; i < 8; i++) target.GetCellAt(1, i).Value = i+1; Cell cell = target.GetCellAt(1,8); List actual = target.GetAvailableNumbers(cell); Assert.AreEqual(1, actual.Count); Assert.AreEqual(9, actual[0]); }   } }

code

CellTest
A Cell in the Sudoku Game is the most basic member of the game. It contains information on each individual cell. Again, the Cell_Accessor is used instead of just using Cell when creating an object.

code format="csharp" using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using Sudoku_Game;

namespace SudokuGameTest {

///    ///This is a test class for CellTest and is intended ///to contain all CellTest Unit Tests ///    [TestClass] public class CellTest {

private TestContext testContextInstance;

///        ///Gets or sets the test context which provides ///information about and functionality for the current test run. ///        public TestContext TestContext {           get {               return testContextInstance; }           set {               testContextInstance = value; }       }

///        ///A test for Value ///        [TestMethod] [DeploymentItem("Sudoku Game.exe")] public void ValueTest {           int expected = 4; Cell_Accessor target = new Cell_Accessor(1, 1, expected); int actual; actual = target.Value; Assert.AreEqual(expected, actual); }

///        ///A test for SubSquare ///        [TestMethod] [DeploymentItem("Sudoku Game.exe")] public void SubSquareTest {           Cell_Accessor target = new Cell_Accessor(1, 1); int expected = 1; int actual; actual = target.SubSquare; Assert.AreEqual(expected, actual); }

///        ///A test for Row ///        [TestMethod] [DeploymentItem("Sudoku Game.exe")] public void RowTest {           Cell_Accessor target = new Cell_Accessor(2, 1); int expected = 2; int actual; actual = target.Row; Assert.AreEqual(expected, actual); }

///        ///A test for ReadOnly ///        [TestMethod] [DeploymentItem("Sudoku Game.exe")] public void ReadOnlyTest {           Cell_Accessor target = new Cell_Accessor(1, 1); bool expected = true; bool actual; target.readOnly = expected; actual = target.ReadOnly; Assert.AreEqual(expected, actual); }

///        ///A test for Column ///        [TestMethod] [DeploymentItem("Sudoku Game.exe")] public void ColumnTest {           Cell_Accessor target = new Cell_Accessor(1, 6); int expected = 6; int actual; actual = target.Column; Assert.AreEqual(expected, actual); }

///        ///A test for calculateSubSquare ///        [TestMethod] [("Sudoku Game.exe")] public void calculateSubSquareTest {           Cell_Accessor target = new Cell_Accessor(9, 9); int expected = 9; int actual; actual = target.SubSquare; Assert.AreEqual(expected, actual); }   } }

code

Test Results
Below is a screen capture of all the Unit Tests passing.