Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run code after all tests finish executing in MStest

Tags:

I am writing coded ui tests and I have the application open if it is not already open. Then if one of them fails I close the application the thing is I have multiple tests in multiple projects is there a way to close the application after all of the tests are done executing? Is there maybe something in the testSettings file?

If this helps at all, all of my test classes derive from one codeduiTestBase which is how I set up the settings I do have.

I do not want to have to open and close the application before and after each test runs because it is a big application and it takes too long to load.

like image 881
jgerstle Avatar asked Feb 08 '13 15:02

jgerstle


People also ask

Which attribute can be used to do the clean up work once all the tests are done?

For example, if you have any static or cached data or services, an action attribute can be used to clean them up for each test.

Does MSTest run tests in parallel?

The biggest advantage of MSTest is that it allows parallelization at the method level. As opposed to the other frameworks which only allow parallelization at the class level. So, for example, If you have 100 test methods in 5 classes, MSTest will let you run 100 tests in parallel.

How do I run a MSTest unit test?

To run MSTest unit tests, specify the full path to the MSTest executable (mstest.exe) in the Unit Testing Options dialog. To call this dialog directly from the editor, right-click somewhere in the editor and then click Options.


1 Answers

Yes it is possible. You can use the AssemblyCleanup Attribute for this purpose:

Identifies a method that contains code to be used after all tests in the assembly have run and to free resources obtained by the assembly.

Here is an overview of all MSTest methods arranged according to execution time:

using Microsoft.VisualStudio.TestTools.UnitTesting; using SampleClassLib; using System; using System.Windows.Forms;  namespace TestNamespace {     [TestClass()]     public sealed class DivideClassTest     {         [AssemblyInitialize()]         public static void AssemblyInit(TestContext context)         {             MessageBox.Show("AssemblyInit " + context.TestName);         }          [ClassInitialize()]         public static void ClassInit(TestContext context)         {             MessageBox.Show("ClassInit " + context.TestName);         }          [TestInitialize()]         public void Initialize()         {             MessageBox.Show("TestMethodInit");         }          [TestCleanup()]         public void Cleanup()         {             MessageBox.Show("TestMethodCleanup");         }          [ClassCleanup()]         public static void ClassCleanup()         {             MessageBox.Show("ClassCleanup");         }          [AssemblyCleanup()]         public static void AssemblyCleanup()         {             MessageBox.Show("AssemblyCleanup");         }          [TestMethod()]         [ExpectedException(typeof(System.DivideByZeroException))]         public void DivideMethodTest()         {             DivideClass.DivideMethod(0);         }     } } 

see: MSTest-Methods

like image 199
MUG4N Avatar answered Sep 21 '22 05:09

MUG4N