如何为UnitTest项目初始化log4net ?

时间:2021-12-17 22:03:01

I have a Visual Studio unit test project for testing an ASP.NET MVC project.

我有一个Visual Studio单元测试项目来测试一个ASP。净MVC项目。

Adding the assembly-level log4net.Config.XmlConfigurator attribute to AssemblyInfo.cs doesn't work and other people on SO have found they have to use a direct call to log4net.Config.XmlConfigurator.Configure();

添加程序log4net.Config。XmlConfigurator来AssemblyInfo属性。cs不起作用,其他人不得不直接调用log4net.Config.XmlConfigurator.Configure();

The question is, how can this be done for a unit test? The answer to use Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitialize attribute on a class method doesn't work.

问题是,如何在单元测试中做到这一点呢?使用Microsoft.VisualStudio.TestTools.UnitTesting的答案。类方法上的assembly yinitialize属性不起作用。

For me, this code results in an InvalidOperationException logged in the output window and the test session bombs early.

对我来说,这段代码导致在输出窗口中登录一个InvalidOperationException,并导致测试会话炸弹在早期出现。

[TestClass]
public sealed class Startup
{
    [AssemblyInitialize]
    public void Configure()
    {
        System.Diagnostics.Debug.Write("Microsoft.VisualStudio.TestTools.UnitTesting.AssemblyInitialize");
    }
}

Reading the documentation, MSDN says not to use AssemblyInitialize on test projects for ASP.NET since they may be called more than once.

MSDN在阅读文档时表示,不要在ASP的测试项目上使用程序集初始化。因为它们可能被调用不止一次。

So how can this be done so that log4net is configured before any tests are run?

那么如何在运行任何测试之前配置log4net呢?

1 个解决方案

#1


22  

The answer was that I was mis-using AssemblyInitialize.

答案是我滥用了汇编初始化。

Once I'd set the debugger to halt on first chance exceptions I was able to read that my method was not static and I'd not added a parameter accepting a TestContext to it.

一旦我设置了调试器来停止第一次异常,我就可以读取我的方法不是静态的,也不会添加一个接受TestContext的参数。

A pretty crappy use of an attribute if the method has to be a certain way, if you ask me. Not very discoverable.

如果方法必须是特定的,如果你问我的话,这是一个很糟糕的属性用法。不是很可发现。

Anyway, this works:

不管怎样,这工作原理:

[TestClass]
public static class Startup
{
    [AssemblyInitialize]
    public static void Configure(TestContext tc)
    {
        log4net.Config.XmlConfigurator.Configure();
    }
}

Regarding the advice not to use this for ASP.NET test, sod it. It might get run more than once but that doesn't matter.

关于建议不要在ASP中使用这个。网络测试,sod。它可能会运行不止一次,但这并不重要。

#1


22  

The answer was that I was mis-using AssemblyInitialize.

答案是我滥用了汇编初始化。

Once I'd set the debugger to halt on first chance exceptions I was able to read that my method was not static and I'd not added a parameter accepting a TestContext to it.

一旦我设置了调试器来停止第一次异常,我就可以读取我的方法不是静态的,也不会添加一个接受TestContext的参数。

A pretty crappy use of an attribute if the method has to be a certain way, if you ask me. Not very discoverable.

如果方法必须是特定的,如果你问我的话,这是一个很糟糕的属性用法。不是很可发现。

Anyway, this works:

不管怎样,这工作原理:

[TestClass]
public static class Startup
{
    [AssemblyInitialize]
    public static void Configure(TestContext tc)
    {
        log4net.Config.XmlConfigurator.Configure();
    }
}

Regarding the advice not to use this for ASP.NET test, sod it. It might get run more than once but that doesn't matter.

关于建议不要在ASP中使用这个。网络测试,sod。它可能会运行不止一次,但这并不重要。