TE

VB.NET Testing

VB.NET testing conventions with MSTest and xUnit

Details

Language / Topic
vbnetVisual Basic .NET
Category
Testing

Rules

balanced
- Use MSTest or xUnit for unit testing — place test projects in a `Tests/` solution folder named `ProjectName.Tests`.
- Follow the naming convention `MethodName_Scenario_ExpectedResult` for test method names: `Sub CalculateTax_WithZeroIncome_ReturnsZero()`.
- Use the Arrange-Act-Assert pattern inside every test method with clearly separated sections marked by blank lines.
- Use `<TestInitialize>` and `<TestCleanup>` (MSTest) or `Sub New()` with `IDisposable` (xUnit) for shared setup and teardown.
- Use `<DataRow>` attribute with `<DataTestMethod>` for parameterized tests: `<DataRow(0, 0)>`, `<DataRow(10, 0.15)>` to test multiple scenarios.
- Use Moq or NSubstitute for mocking — create mock objects via `Dim mockService As Mock(Of IUserService) = New Mock(Of IUserService)()` and verify interactions.
- Test async methods by marking the test `Async` and using `Await`: `Async Function TestFetchUser() As Task`.