|
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace TestProject1
{
/// <summary>
/// Summary description for UnitTest1
/// </summary>
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
foreach (TestCase testCase in getTestCases())
{
// Create the tested class
var c = new Calculator();
try
{
// invoke the tested method
int result = c.Divide(testCase.Dividend, testCase.Divisor);
// check the result
Assert.AreEqual(testCase.ExpectedResult, result);
Assert.IsNull(testCase.ExpectedExceptionType);
}
catch (Exception ex)
{
// an error has occured
Assert.IsNotNull(testCase.ExpectedExceptionType);
Assert.AreEqual(testCase.ExpectedExceptionType, ex.GetType());
}
}
}
private IEnumerable<TestCase> getTestCases()
{
// both Dividend and diviser are 0
var tc = new TestCase(0, 0, 0, typeof (DivideByZeroException));
tc.Description = "both Dividend and diviser are 0";
yield return tc;
// Dividend is 0, Diviser is > 0
tc = new TestCase(0, 1, 0, null);
tc.Description = "Dividend is 0, Diviser is > 0";
yield return tc;
// Dividend is > 0, Diviser is 0
tc = new TestCase(1, 0, 0, typeof (DivideByZeroException));
tc.Description = "Dividend is > 0, Diviser is 0";
yield return tc;
}
#region Nested type: TestCase
private class TestCase
{
public TestCase(int dividend, int divisor,
int expectedResult, Type expectedExceptionType)
{
Dividend = dividend;
Divisor = divisor;
ExpectedResult = expectedResult;
ExpectedExceptionType = expectedExceptionType;
}
public int Dividend { get; set; }
public int Divisor { get; set; }
public int ExpectedResult { get; set; }
public Type ExpectedExceptionType { get; set; }
public string Description { get; set; }
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.