What is Microsoft’s MSTest Framework? Why is it used?
MSTest is a Unit Testing framework in Visual Studio that allows you to test .NET Apps. The MSTest Test Framework can be installed as a set of NuGet Packages & is a very effective way to automate tests. You can save the test functions in their own test classes & execute them manually in test runner to have them ready for CICD Automation.
How Does MSTest Relate to CICD?
MSTest is part of Continuous Integration. After a developer would push code to the Dev Environment, MSTest would then execute all of its tests to make sure there are no bugs. With MSTest you can identify failed unit tests to spot bugs faster & make sure none of the tests fail before moving the code to Dev, Staging, & Production.
MSTest Methods can be run manually but the real value is having them run automatically before pushing code to any master branch & then having MSTest run before pushing to master branch and after pushing to master branch.
This process would be automated using various scripts & tools such as Azure DevOps, & Ansible as part of the CICD Pipeline setup.
Elements of MSTest
At a high level, we can think of MSTest to be made of these elements below which describe the various functions and features of MSTest V2.
– Attributes in MSTest
Provided as part of the MSTest Framework to mark methods in code as test methods.
– Asserts in MSTest
Methods that allow us to check if an expected value is received from the production code. IE) Asserts can check to see if basic int, double, string values are there, & also check arrays & list collections.
– Test Runners in MSTest
Test Runners will run through code to identify marked methods with MSTest Attributes & test those marked methods as tests & execute code in those test methods. They will then report pass/fail outcomes of the test which can also be seen in the Test Explorer of VS IDE.
– Test Categories
MSTest lets you specify categories for your unit tests. These categories can be in different classes & can still be filtered in the Test Explorer
– Test Execution Lifecycle Hooks
MSTest lets you hook into the Test Execution lifecycle. This lets you run additional code before test methods execute &/or clean up code after test methods finish executing.
– Data Driven Tests
We can use MSTest to create Data-Driven tests where we can pass multiple data sets through parameters for the same test.
Test Data can come from Inside the code
Test Data Can Come From External Source
Test data can come from a .csv file, a database, or even an API.
– Extensibility in MSTest
MSTest lets you extend the framework by allowing you to write custom assert methods, custom attributes, custom test data sources
– Nuget Packages for MSTest Framework
- MSTest.TestFramework
- MSTest Core Framework:
- Attributes,
- Assert Methods,
- Data Driven Tests
- Extensibility Points
- MSTest Core Framework:
- MSTest.TestAdapter
- Discover MSTest-based test methods: [TestMethod]
- Execute tests
- Microsoft.NET.Test.Sdk