logo

C# Unit Testing

Unit testing improves programming productivity and improves object oriented class abstraction. classes should be loosely coupled and their methods should do exact what they describe. Unit testing help isolate the functionality of the class. classes and derived class functionality can be access by type casting. Virtual and override methods will have predictable behavior that can be verified in unit testing. Unit testing should be built as you go. Refactoring classes and methods will affect the unit tests, but often don't take long to correct. Unit testing promotes better abstraction reducing complexity and makes possible more reusable components. The automated unit testing can be run every night after checkin and a report generated for correction the next day. Legacy systems, code and design can be refactored incrementally. The real value of unit testing is design feedback. Unit testing reduces fear and increases understanding of the object interface and interaction behaviors. I often look at the unit test to see how to use the application. Every developer is responsible for his code and the code quality of others. github is an excel resource of collaberating with other developers to build quality code. unit tests are a standard expectation of the github repository. Unit test should on test one path. Visual Studio Manager has an unit test manager providing automatic test runs or debug test runs. You can see quickly the quality of your solution by running your unit tests.
BICIP

Boundaries are where most of the bugs reside
a. inconsistent input values
b. badly formatted data
c. missing values (empty string or null value)
d. values that exceed the range expectations
e. duplicates
f. things that arrive out of order

Check inverse Order (Can you get back to the original state)c
a. double x=Math.SquareRoot(4.0)
   Assert.That(4.0, is.EqualTo(x*x).Within(0.0001);

Cross Check the method by different means
1. There is more than one way to get a value.  Compare the different methods and check for tolerance values.

Force Error Conditions


Assert.AreEqual(expected, actual[,string message]);
Assert.Less(x,y);
Assert.Greater(x,y);
Assert.LessOrEqual(x,y);
Assert.GreaterOrEqual(x,y);
Assert.IsNull(object[,string message]);
Assert.IsNotNull(object[,string message]);
Assert.AreSame(expected, actual[,string message]);
Assert.IsTrue(bool condition[,string message]);
Assert.That(actual, Is.EqualTo(expected));
Assert.That(actual, Is.Not.EqualTo(expected));
Assert.That(actual, Is.AtMost(expected));
Assert.That(actual, Is.Null);
Assert.That(actual, Is.Not.Null);
Assert.That(actual, Is.AtLeast(expected));
Assert.That(actual. Is.InstanceOfType(expected));

https://msdn.microsoft.com/en-us/library/ms182530.aspx
Kinds of Assert

s