代码之家  ›  专栏  ›  技术社区  ›  D.R.

将IWrapTestMethod属性应用于整个fixture?

  •  0
  • D.R.  · 技术社区  · 7 年前

    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");
            };
        }
    }
    

    当我将属性应用到测试方法时,命令被正确地调用和执行。我希望能够将该属性放到测试夹具上,并将其自动应用于测试夹具中的所有测试。怎么做?我在文件里找不到合适的。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Charlie    7 年前

    作为一种解决方法,请考虑创建 Action Attribute