gpt4 book ai didi

c# - 在构造函数和析构函数中输出时运行一次初始化和拆卸代码

转载 作者:行者123 更新时间:2023-11-30 23:32:40 28 4
gpt4 key购买 nike

xUnit 中,我需要在执行任何测试之前一次 运行一些代码,并在所有测试完成后运行一些代码。虽然这thread解释了如何做得很好,我想在构造函数和析构函数中进行一些打印,如下面的代码所示,这是棘手的部分。由于 Console.Writeline 不起作用,我寻找了一个解决方法,我在这个 link 中找到了它.

public class TestsFixture : IDisposable
{
protected readonly ITestOutputHelper _output;

public TestsFixture(ITestOutputHelper output)
{
_output = output;

// Do "global" initialization here; Only called once.
_output.WriteLine("global init");
}

public void Dispose()
{
// Do "global" teardown here; Only called once.
_output.WriteLine("global teardown");
}
}

public class HandlerTests : IClassFixture<TestsFixture>
{
// All my tests are here
}

关于这里发生的事情的简要解释:

This code uses the IUseFixture interface to ensure that the global initialization/teardown functionality is only called once. For this version, you don't extend a base class from your test class but implement the IUseFixture interface where T refers to your fixture class

一切似乎都很好,但是当我运行测试时,出现错误(如下)。关于如何解决这个问题的任何想法?

Test Outcome: Failed
Test Duration: 0:00:00,001

Result Message: Class fixture type 'TestsPlatform.TestsFixture' had one or more unresolved constructor arguments: ITestOutputHelper output

最佳答案

documentation表示您可以将类型为 ITestOutputHelper 的参数添加到 test 类的构造函数中。我没有看到任何说明您可以将它作为参数添加到 test fixture 类的构造函数中...

此输出通过 ITestOutputHelper 是没有意义的,因为该机制的全部要点是允许输出与特定测试相关联。您的设置/拆卸是全局的,而不是针对每个测试。

您需要找到另一种方式来输出这些诊断信息。

关于c# - 在构造函数和析构函数中输出时运行一次初始化和拆卸代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34204195/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com