是否可以在所有测试运行之前执行一次代码? [英] Is it possible to execute code once before all tests run?

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

问题描述

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

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().

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

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.XmlConfigurator assembly 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?

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

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

推荐答案

FWIW,您可以使用

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

[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.

据我所知,这与您可以找到的Main差不多.

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

请注意,装饰AssemblyInitialize的方法必须必须在装饰有TestClass的类中,该类至少包含一个装饰有TestMethod的方法,否则将被执行!

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆