中执行完所有测试后运行

中执行完所有测试后运行

本文介绍了是否可以在 MStest 中执行完所有测试后运行代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写编码的 ui 测试,如果应用程序尚未打开,我将其打开.然后,如果其中一个失败,我关闭应用程序,问题是我在多个项目中有多个测试,有没有办法在所有测试执行完毕后关闭应用程序?testSettings 文件中可能有内容吗?

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?

如果这有帮助的话,我所有的测试类都来自一个 codeduiTestBase,这就是我设置我所拥有的设置的方式.

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.

推荐答案

是的,这是可能的.为此,您可以使用 AssemblyCleanup 属性:

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

标识包含要在所有测试之后使用的代码的方法程序集已运行并释放程序集获得的资源.

以下是根据执行时间排列的所有 MSTest 方法的概述:

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);
        }
    }
}

请参阅:MSTest-Methods

这篇关于是否可以在 MStest 中执行完所有测试后运行代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 04:25