IWrapTestMethod
属性:
public class OutputElapsedTimeAttribute : Attribute, IWrapTestMethod
{
public TestCommand Wrap(TestCommand command)
{
return new OutputElapsedTimeCommand(command);
}
}
以及相应的before和aftertest命令:
public class OutputElapsedTimeCommand : BeforeAndAfterTestCommand
{
private Stopwatch _sw;
public OutputElapsedTimeCommand(TestCommand innerCommand) : base(innerCommand)
{
BeforeTest = ctx => { _sw = Stopwatch.StartNew(); };
AfterTest = ctx =>
{
_sw.Stop();
ctx.OutWriter.WriteLineAsync($"Took: {_sw.ElapsedMilliseconds}ms");
};
}
}
当我将属性应用到测试方法时,命令被正确地调用和执行。我希望能够将该属性放到测试夹具上,并将其自动应用于测试夹具中的所有测试。怎么做?我在文件里找不到合适的。