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

两盒软妹~` 提交于 2020-06-09 11:57:25

问题


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.


回答1:


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




回答2:


IMPORTANT

Someone who is using base class can do assembly cleanup there as well

[TestClass]
public class Page : PageContract, IWindowControlAccess
{
[AssemblyCleanup()]
    public static void ApplicationCleanup()
    {
        Cleanup();
    }
}

Two important things here 1. [TestClass] attribute at the base is MANDATORY

2. signature of ApplicationCleanup see its a static method

That's it you are done. I had hard time struggling and debugging it.




回答3:


Perhaps what you're looking for is the TestCleanup attribute?

EDIT: Also, if you want to run something after all tests have been run, the AssemblyCleanupAttribute might be more appropriate.




回答4:


If you just want to clean up after all tests inside a single class have finished, use the ClassCleanup attribute rather than the AssemblyCleanup attribute



来源:https://stackoverflow.com/questions/14775574/is-it-possible-to-run-code-after-all-tests-finish-executing-in-mstest

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!