.net 是否可以在所有测试运行之前执行一次代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2382552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:01:59  来源:igfitidea点击:

Is it possible to execute code once before all tests run?

.netmstest

提问by George Mauer

Basically I would like to tell MSTest to execute a bit of code before launching into a series of test runs, essentially what I would like to do is the same thing as sticking some code in Main().

基本上,我想告诉 MSTest 在启动一系列测试运行之前执行一些代码,基本上我想做的与将一些代码粘贴到Main().

The reason I would like to do this is that I would like to do some logging with log4net during my integration test runs. I cannot just use the log4net.Config.XmlConfiguratorassembly attribute since by the time it reads it in my test assembly it has already called LoggerManager. The documentation recommends configuring log4net explicitly at the code entry point - but where is that in my tests?

我想这样做的原因是我想在我的集成测试运行期间用 log4net 做一些日志记录。我不能只使用log4net.Config.XmlConfigurator程序集属性,因为当它在我的测试程序集中读取它时,它已经调用了LoggerManager. 该文档建议在代码入口点明确配置 log4net - 但在我的测试中它在哪里?

I need to be able to run my tests in TestDriven.NET and MSTest runner.

我需要能够在 TestDriven.NET 和 MSTest runner 中运行我的测试。

回答by Mark Seemann

FWIW, you can use the AssemblyInitializeattribute to run code before all unit tests in an assembly executes:

FWIW,您可以使用AssemblyInitialize属性在程序集中的所有单元测试执行之前运行代码:

[TestClass]
public class SetupAssemblyInitializer
{
    [AssemblyInitialize]
    public static void AssemblyInit(TestContext context)
    {
        // Initalization code goes here
    }
}

If you have more than one unit test assembly, I'm not aware of anything that encompasses more than one assembly.

如果您有多个单元测试程序集,我不知道包含多个程序集的任何内容。

As far as I'm aware, this is as close as you can get to a Main equivalent.

据我所知,这与 Main 等效项尽可能接近。

Note that the AssemblyInitialize-decorated method mustbe in a TestClass-decorated class which contains at least one TestMethod-decorated method, otherwise it will notbe executed!

注意AssemblyInitialize-decorated 方法必须TestClass包含至少一个TestMethod-decorated 方法的-decorated 类中,否则不会被执行!

回答by Konamiman

For completion, these are the "run code before" options for MSTest:

为了完成,这些是 MSTest 的“运行代码之前”选项:

  • Use [AssemblyInitialize]to run code once per assembly, before any test in that assembly runs.
  • Use [ClassInitialize]to run code once per class, before any test in the class where the method is defined.
  • Use [TestInitialize]to run code before each and every test in the class where the method is defined.
  • 用于[AssemblyInitialize]在每个程序集中运行一次代码,然后在该程序集中运行任何测试。
  • 用于[ClassInitialize]在定义方法的类中进行任何测试之前,为每个类运行一次代码。
  • 用于[TestInitialize]在定义方法的类中的每个测试之前运行代码。

回答by Maestro1024

I see this in the MS Test header.

我在 MS Test 标头中看到了这一点。

// Use ClassInitialize to run code before running the first test in the class
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext) { }

This would run before the tests in one class.

这将在一个班级的测试之前运行。

Sounds like you want to run something before all of the tests.

听起来你想在所有测试之前运行一些东西。

There is also the setup script option in the test run config.

测试运行配置中还有设置脚本选项。