Global test initialize method for MSTest

烂漫一生 提交于 2019-11-30 10:42:57

问题


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


回答1:


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.




回答2:


Just to underscore what @driis and @Malice said in the accepted answer, here's what your global test initializer class should look like:

namespace ThanksDriis
{
    [TestClass]
    class GlobalTestInitializer
    {
        [AssemblyInitialize()]
        public static void MyTestInitialize(TestContext testContext)
        {
            // The test framework will call this method once -BEFORE- each test run.
        }

        [AssemblyCleanup]
        public static void TearDown() 
        {
            // The test framework will call this method once -AFTER- each test run.
        }
    }
}


来源:https://stackoverflow.com/questions/1427443/global-test-initialize-method-for-mstest

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