Simple Unit Testing

This is a simple program that demonstrates some unit tests. Depending on your opinion or requirements unit testing is essential in testing code. I’m starting to learn the in’s and outs of unit testing standards, testing to pass, and testing to fail. This is a very simple gradebook console program. It doesn’t really do anything useful except demonstrate concepts. In this case unit testing.

Over the next few posts I’ll be using this same batch of code in different versions. I might not go over all the ways I’m doing things if it’s not in scope of what the blog post is about. If you want to see all of the source you can visit the source downloads page. using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

{
{
// Fields

// Constructors
{
// Setting the fields with values.
LowestGrade = float.MaxValue;       // Setting the lowest grade to a really high number to check against.
}
}
}

This simple class has fields to hold the average, highest, and lowest grade in a grade book. When instantiated the highest grade and lowest grade are set. The highest grade set to a value of zero. The lowest grade is set to a maximum value initally. This way it can be compared to an actual value that is gauranteed to be lower. I’m leaving the calculation of the average grade up to the GradeBook class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

{
{

public string Name;

{
_grades = new List<float>();
}

{
// Adding some validation logic before adding the grade to the collection.
if (grade >= 0 && grade <= 100)
}

{
// Creating an instance variable of the class to return.

// Now to find the average, highest, and lowest grade in the list.
float sum = 0f;
{
// Calculating the highest and lowest grade using built-in .NET methods.
// Comparing the current grade in the loop to the highest grade. If greater then assign the new value.

// Comparing the current grade in the loop to the lowest grade. If lower then assign the new value.

// Adding numbers to the sum variable.
}

// Calculating the average grade.
stats.AverageGrade = sum / _grades.Count();     // Normally this would be encased in a try/catch. Possible divide by zero exception.
return stats;
}
}
}

This class holds the grade book list, name, computes the statistics on the grades in the gradebook, and adds grades to the grade book.

Program.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

{
class Program
{
static void Main(string[] args)
{
// Demonstrating reference types. Basically a pointer to the objects place in memory.

// Calling a method that will compute statistics and return me a value.
// Then that value will be stored in a class variable.
GradeStatistics stats = book.ComputeStatictics();
}
}
}

I’ve kept this class pretty simple thru the demonstration of instantiation and method calls. Creating the gradebook, adding grades to the gradebook, and computing the statistics on the grades in the gradebook.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

{
[TestClass]
{
[TestMethod]
{

GradeStatistics stats = book.ComputeStatictics();

// Checking to see if this test reports back as passed/true. That this is the highest grade.
}
[TestMethod]
{

GradeStatistics stats = book.ComputeStatictics();

// Checking to see if this test reports back as failed/false. That this is not the highest grade.
}

[TestMethod]
{

GradeStatistics stats = book.ComputeStatictics();

// Checking to see if this test reports back as passed/true. That this is the highest grade.
}

[TestMethod]
{

GradeStatistics stats = book.ComputeStatictics();

// Checking to see if this test reports back as passed/true. That this is the highest grade.
}

[TestMethod]
public void PassByValuePass()
{
book.Name = "Not Set";
SetName(book);

// Checking to see if this test reports back as passed/true. That the book name was set to this value.
Assert.AreEqual("Name Set", book.Name);
}

[TestMethod]
public void PassByValueFail()
{
book.Name = "Not Set";
SetName(book);

// Checking to see if the test reports back as failed/false. The book name is not set to this value.
Assert.AreEqual("This can't be right", book.Name);
}

// Private method used to demostrate passing by value in a unit test.
private void SetName(GradeBook book)
{
book.Name = "Name Set";
}
}
}

This is unit test class. Here I’m testing for passing and failing of various items. Examples include testing to make sure highest grade is calculated correctly pass/fail, lowest grade pass/fail, and a pass by value and reference pass/fail.