运行 在构造函数和析构函数中输出一次初始化和拆卸代码
run initialisation and teardown code once while outputting in the constructor and destructor
在 xUnit 中,我需要 运行 一些代码 once,在执行任何测试之前,以及之后的一些代码所有测试都已完成。虽然这thread explains how to do it quite well, I want to do some printing inside the constructor and destructor, like shown in the code below, and that's the tricky part. Since Console.Writeline
won't work, I looked for a workaround, which I found in this 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 说你可以在 test class 的构造函数中添加类型 ITestOutputHelper
的参数。我没有看到任何内容表明您可以将它作为参数添加到 test fixture class...
的构造函数中
此输出通过 ITestOutputHelper
没有意义,因为该机制的全部意义在于允许输出与特定测试相关联。您的 setup/teardown 是全局的,而不是每次测试。
您需要找到另一种方式来输出这些诊断信息。
在 xUnit 中,我需要 运行 一些代码 once,在执行任何测试之前,以及之后的一些代码所有测试都已完成。虽然这thread explains how to do it quite well, I want to do some printing inside the constructor and destructor, like shown in the code below, and that's the tricky part. Since Console.Writeline
won't work, I looked for a workaround, which I found in this 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 说你可以在 test class 的构造函数中添加类型 ITestOutputHelper
的参数。我没有看到任何内容表明您可以将它作为参数添加到 test fixture class...
此输出通过 ITestOutputHelper
没有意义,因为该机制的全部意义在于允许输出与特定测试相关联。您的 setup/teardown 是全局的,而不是每次测试。
您需要找到另一种方式来输出这些诊断信息。