Unit testing with MSTest

Structure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using Microsoft.VisualStudio.TestTools.UnitTesting;

[TestClass]
public class MyClassTests
{

[TestMethod]
public void UnitOfWork_InitialCondition_ExpectedResult()
{
// Arrange
// Act
// Assert
Assert.AreEqual(expected, actual);
}
}

Other Asserts

1
Assert.IsTrue

Exception

ThrowsException

1
2
3
4
5
// Act
var exception = Assert.ThrowsException<ArgumentException>(() => classUnderTest.UnitOfWork("foo", "bar"));

// Assert
Assert.AreEqual(expected, exception.Message);

ExpectedException

1
2
3
4
[ExpectedException(typeof(MyException))]
[TestMethod]
public void UnitOfWork_InitialCondition_ExpectedResult()
{ ... }

References