How to Use MSTest in C#: Simple Guide with Example
To use
MSTest in C#, create a test project, add the Microsoft.VisualStudio.TestTools.UnitTesting namespace, and write test methods marked with [TestMethod] inside a class marked with [TestClass]. Run tests using Visual Studio Test Explorer or the dotnet test command.Syntax
Use [TestClass] to mark a class that contains tests. Use [TestMethod] to mark each test method. Inside test methods, use Assert methods to check expected results.
[TestClass]: Identifies the test container class.[TestMethod]: Marks a method as a test.Assert: Provides methods to verify test outcomes.
csharp
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MyTests
{
[TestClass]
public class CalculatorTests
{
[TestMethod]
public void Add_TwoNumbers_ReturnsSum()
{
// Arrange
int a = 5;
int b = 3;
// Act
int result = a + b;
// Assert
Assert.AreEqual(8, result);
}
}
}Example
This example shows a simple test class for a calculator's addition method. It demonstrates how to write a test method that checks if adding two numbers returns the correct sum.
csharp
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CalculatorApp.Tests
{
[TestClass]
public class CalculatorTests
{
[TestMethod]
public void Add_TwoPositiveNumbers_ReturnsCorrectSum()
{
// Arrange
var calculator = new Calculator();
int x = 10;
int y = 20;
// Act
int result = calculator.Add(x, y);
// Assert
Assert.AreEqual(30, result);
}
}
public class Calculator
{
public int Add(int a, int b)
{
return a + b;
}
}
}Output
Test run successful.
Total tests: 1
Passed: 1
Total time: 1.2345 Seconds
Common Pitfalls
Common mistakes when using MSTest include:
- Forgetting to mark the test class with
[TestClass]or test methods with[TestMethod], so tests are not discovered. - Not referencing the MSTest NuGet package, causing missing namespace errors.
- Using
Assertincorrectly, such as comparing wrong values or forgetting to assert results. - Running tests without building the project first, leading to stale results.
csharp
/* Wrong: Missing [TestMethod] attribute, test won't run */ [TestClass] public class SampleTests { public void TestWithoutAttribute() { Assert.IsTrue(true); } } /* Correct: Add [TestMethod] attribute */ [TestClass] public class SampleTests { [TestMethod] public void TestWithAttribute() { Assert.IsTrue(true); } }
Quick Reference
| Attribute/Method | Purpose |
|---|---|
| [TestClass] | Marks a class that contains test methods |
| [TestMethod] | Marks a method as a test to run |
| Assert.AreEqual(expected, actual) | Checks if two values are equal |
| Assert.IsTrue(condition) | Checks if a condition is true |
| Assert.IsFalse(condition) | Checks if a condition is false |
| Assert.ThrowsException | Checks if an exception of type T is thrown |
Key Takeaways
Mark test classes with [TestClass] and test methods with [TestMethod] to enable MSTest discovery.
Use Assert methods to verify expected outcomes inside test methods.
Add the MSTest NuGet package to your project to access MSTest features.
Run tests via Visual Studio Test Explorer or the dotnet test command.
Avoid missing attributes or incorrect asserts to ensure tests run and validate correctly.