Está en la página 1de 3

Unit Testing C#

Unit test shown below are done with Microsoft unit test framework. Unit test is done to make
sure properties, attributes and actions work as expected. A Professional IDE for developers
needs to have unit test support since unit testing is a requirement in quality software
development. Visual studio being a professional IDE, has capabilities for unit testing including
the test explorer Microsoft cited to run unit tests efficiently. Moreover, Visual studio has
capabilities in simplifying unit test creation. There are test cases and annotations already so
Visual Studio is aware if a class is intended for unit testing or not. Microsoft recommended that
test methods are made as soon as the method was created to define and check expectations.
First make sure there is/are class/classes to test on. Afterwards, add a test project in the
solution. The test project is where test classes can be created and defined.

Next, make sure the test project has reference of the project that has the classes to be tested. This step
ensures that the test classes (or the classes with test methods) can call the classes to be tested.


You can rename the auto generated test class or add your own Test class. This is also a good time to
have the test explorer.


There is a class below that has mathematical functions. The other class is a test class that determines if
the results from the mathematical functions are correct. Pardon the awful naming convention.
Mathematical function caller
public class MathOperator
{
public int Fibonacci(int number)
{
if (number <= 1)
return number;
return Fibonacci(number - 1) + Fibonacci(number - 2);
}

public int Summation(int maxNumber)
{
if (maxNumber <= 1)
return maxNumber;
return maxNumber + Summation(maxNumber - 1);
}

}

Test class
[TestClass]
public class MathFuncTest
{
[TestMethod]
public void TestFibonacci()
{
MathOperator toBeTested = new MathOperator();
Assert.AreEqual(toBeTested.Fibonacci(2), 1);
}

[TestMethod]
public void TestFibonacci_F3()
{
MathOperator toBeTested = new MathOperator();
Assert.AreEqual(toBeTested.Fibonacci(3), 2);
}

[TestMethod]
public void TestFibonacci_F8()
{
MathOperator toBeTested = new MathOperator();
Assert.AreEqual(toBeTested.Fibonacci(8), 21);
}

[TestMethod]
public void TestSummation_n100()
{
MathOperator toBeTested = new MathOperator();
Assert.AreEqual(toBeTested.Summation(100), 5050);
}
}

As seen above, the test class contains an annotation [TestClass] indicating that the class is made for
testing purposes and not to be part of the build. The [TestMethod] annotation indicates that the
method is to be called when tests are commanded to be run. Running tests can be done from the test
explorer where the user can click Run all. Tests can either pass or fail. The results will be visible on the
Test explorer. Pass is given when the assertion has the expected result. A fail is given when he assertion
did not have the expected result or when an unanticipated exception occurred. Furthermore, a right
click on the test class name allows the user to run tests in the class by clicking the Run Tests option or
using the keyboard shortcut specified.

También podría gustarte