Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global test initialize method for MSTest

Tags:

c#

mstest

Quick question, how do I create a method that is run only once before all tests in the solution are run.

like image 893
mglmnc Avatar asked Sep 15 '09 14:09

mglmnc


People also ask

What is TestInitialize MSTest?

The method decorated by [TestInitialize] is called before running each test of the class. The method decorated by [TestCleanup] is called after running each test of the class.

What is the prerequisite to unit testing C# with MSTest and net?

Create the test project The test project requires other packages to create and run unit tests. dotnet new in the previous step added the MSTest SDK, the MSTest test framework, the MSTest runner, and coverlet for code coverage reporting. You can see the entire file in the samples repository on GitHub.

Which among the following is the correct command to install test framework packages in MSTest?

You can download and install the MSTest framework by either of the two methods, as shown below: a. PM (Package Manager) commands from the ''NuGet Package Manager Console” – For executing commands from the NuGet PM console, go to 'Tools' -> 'NuGet Package Manager' -> 'Package Manager Console.


1 Answers

Create a public static method, decorated with the AssemblyInitialize attribute. The test framework will call this Setup method once per test run:

[AssemblyInitialize()] public static void MyTestInitialize(TestContext testContext) {} 

For TearDown its:

[AssemblyCleanup] public static void TearDown()  {} 

EDIT:

Another very important detail: the class to which this method belongs must be decorated with [TestClass]. Otherwise, the initialization method will not run.

like image 74
driis Avatar answered Sep 22 '22 06:09

driis