在 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
}
简要说明这里发生了什么:
此代码使用IUseFixture接口来确保全局 初始化/拆卸功能只调用一次。为了这 版本,您不会从测试类扩展基类,但是 实现IUseFixture接口,其中T指的是你的夹具 类
一切似乎都很好,但是当我运行测试时,我得到一个错误(下面)。关于如何解决这个问题的任何想法?
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
答案 0 :(得分:1)
documentation表示您可以将类型 Get_Calc_And_Save_Data( self, closure: {(error:NSError?) -> Void in
if error==nil {
print("OPERATION OK")
} else {
print("OPERATION ERROR: \(error)")
}
})
的参数添加到测试类的构造函数中。我没有看到任何说明你可以将它作为参数添加到测试夹具类的构造函数...
此输出通过ITestOutputHelper
没有意义,因为该机制的重点是允许输出与特定测试相关联。您的设置/拆卸是全局的,而不是按测试。
您需要找到另一种方法来输出这些诊断信息。