In this article, we will explain to you in detail about unit testing using MSTest in ASP.NET core.
In the programming world, unit testing is a software testing method by which individual constituents of source code are evaluated to regulate whether they are relevant for use. A unit is commonly an entire interface, such as a class, but could be an individual procedure.
There are three different test frameworks that are assisted by the unit test with ASP.NET core: MSTest, xUnit, and NUnit, which enable us to test our code in a compatible way.
The MSTest framework reinforces unit testing in Visual Studio by using the classes and members in Microsoft.VisualStudio.TestTools.UnitTesting namespace when you are coding unit tests. You can also utilize them when you are concentrating on a unit test that originated from code.
What is MSTest?
- MSTest is one type of framework that provides the facility to test the code without using any third-party tool. It helps in writing effective unit tests using MSTest framework to test software applications.
- MSTest is a number-one open-source test framework that is shipped along with the Visual Studio IDE. It is also referred to as the Unit Testing Framework. However, MSTest is the same within the developer community.
- MSTest is used to run tests. This command has a lot of options that you can use to customize your test run various NET applications.
- The fact that it copies files also causes fairly common file conflicts which because test runs to fail silently our integration tests, to be non-destructive, mount, and unmount a test database.
- This is combined test-first development where you can write a test Just before you write enough production and code to fulfill that Test. You can rewrite the code to pass the test. We have two Main frameworks to write the Unit Test that is MS Test and NUnit.
- MSTest V2 is now available as open-source and if you are using an older version, so remove the “Microsoft.visualstudio. quality tools. unit test framework” Add Ref. new Ref of “MSTest.TestFramework” and “MSTest.Test Adapter” from NuGet Package Manager.
How to Run MSTest Scripts with Selenium?
We will use the following Selenium test automation scenario for the MSTest:
- First of all, Navigate to the URL https://lambdatest.github.io/sample-todo-app/
- Then select the first two checkboxes
- Send ‘Adding item to the list’ to the textbox with id = sample to text
- Lastly click the Add Button and verify whether the text has been added or not
Example of MSTest in ASP.Net Step-by-Step
We will hereby perform the simple program for understanding the concept of MSTest Framework.
- For testing code in ASP.Net Core, initially we will create a new project. So, for creating a new ASP.Net Application open Visual Studio 2019. After that, select the menu option File -> New -> select New Project Click on Ok.

Fig. 01
- First of all, the new project creation window pops up, select the ASP.Net Core C# Application and then click on the Next button. you will get the same screen shown below.
- Create a simple “Hello World” program. Here you can also create another project whatever would you like to test in this Framework this is just a demo for beginners
using System; namespace HelloWorldCore1 { public class Program { public static void Main () { Console.WriteLine("Hello World!"); } } }
- After Creating the Simple Hello word Program go to the solution explorer and right-click on it. Add New Project, at that time select the MSTest Project that is displayed using below screen.
- Then if you want to apply the appropriate name so apply name otherwise just click on the create button. Add a reference from the test project to the project that which you want to test.

Fig. 02
- After completing all these steps, your project structure will be visible as per the below screen. Here you can check if the reference is added or not.
Note: You should add the Reference. Follow the mentioned step to add a reference in the test project.
- Right click on a test project and go to the add menu and select add project reference.
When you are able to see the above screen, select the checkbox and press ok.
- Add Code in UnitTest1 Method. Add two Reference this class. Just an example, for a Hello WordCore project, you might use the following code. There is HelloWorldCore1. Program.
using Microsoft.VisualStudio. TestTools.UnitTesting;
using System; using System.IO; namespace UnitTestProject1 { [TestClass] public class UnitTest1 { private const string Expected = "Hello World!"; [TestMethod] public void TestMethod1() { using (var sw = new StringWriter()) { Console.SetOut(sw); HelloWorldCore1.Program.Main(); var result = sw.ToString().Trim(); Assert.AreEqual(Expected, result); } } } }
This is for the NUnit project, you can use the following code.
using NUnit.Framework; using System.IO; using System; namespace HelloWorldTests { public class Tests { private const string Expected = "Hello World!"; [SetUp] public void Setup() { } [Test] public void TestMethod1() { using (var sw = new StringWriter()) { Console.SetOut(sw); HelloWorldCore1.Program.Main(); var result = sw.ToString().Trim(); Assert.AreEqual(Expected, result); } } } }
Run unit tests
After completing the program, run all test methods. Follow steps below:
1. Go to the Test menu and select run all tests. Then, you will get the below display.
2. If all is right, arrow is green and Unit Test Is Complete without any Bugs. If any one Right Arrow is not Green and display in reading color, then error has occurred in over code. So go to the error-related code, fix the bug, and then run project.
3. Now Run Over Main Project that was created. Press F5 or else click on This sing
Then you will get the below output by running the test project successfully with the help of MSTest Framework.
Conclusion
In this blog, we learned how to integrate a test project with a .NET Core application with the help of test Framework. We have discussed how to create and execute a sample unit test project using MStest in.NET Core. A unit test is a code that assists us in referring the expected behavior of the other code in isolation. This is a perfect idea to test the application code before it goes to the quality assurance (QA) team.