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

前端 未结 4 743
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 01:28

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 mu

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 01:50

    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

提交回复
热议问题