the*_*ric 6 c# unit-testing xunit.net approval-tests
当我尝试使用带有[Theory]属性的单元测试批准时,它说:
System.Exception: System.Exception : Approvals is not set up to use your test framework.
It currently supports [NUnit, MsTest, MbUnit, xUnit.net]
To add one use ApprovalTests.StackTraceParsers.StackTraceParser.AddParser() method to add implementation of ApprovalTests.StackTraceParsers.IStackTraceParser with support for your testing framework.
To learn how to implement one see http://blog.approvaltests.com/2012/01/creating-namers.html
at ApprovalTests.StackTraceParsers.StackTraceParser.Parse(StackTrace stackTrace)
at ApprovalTests.Namers.UnitTestFrameworkNamer..ctor()
at ApprovalTests.Approvals.GetDefaultNamer()
at ApprovalTests.Approvals.Verify(IApprovalWriter writer)
at ApprovalTests.Approvals.Verify(Object text)
Run Code Online (Sandbox Code Playgroud)
似乎它只识别[Fact]属性.我试图关注stacktrace中的链接, 但没有关于如何将自己的名称/解析器插入批准的内容.
有没有我可以添加自己的名字/解析器的入口点?它本身似乎微不足道,唯一的问题是如何使用它:
public class TheoryNamer : AttributeStackTraceParser
{
protected override string GetAttributeType()
{
return typeof(TheoryAttribute).FullName;
}
public override string ForTestingFramework
{
get { return "xUnit Extensions"; }
}
}
Run Code Online (Sandbox Code Playgroud)
这个答案和问题有几个部分.
1)如何添加
添加很简单(如果有点粗糙)提到的方法应该是静态的,但它仍然可以工作.
要添加一个使用ApprovalTests.StackTraceParsers.StackTraceParser.AddParser()方法来添加ApprovalTests.StackTraceParsers.IStackTraceParser的实现,并支持您的测试框架.
所以你需要做一个
new StackTraceParser().AddParser(new TheoryNamer());
Run Code Online (Sandbox Code Playgroud)
我为此道歉,在下一版本中它将是静态的(第21节)
2)命名者
命名者假设为每个批准/接收的文件生成唯一的名称.这通常是在方法的名称上完成的,但是这里的名称不是唯一的,因为基于理论的测试将是数据驱动的,因此对同一方法有多次调用.
Naming: classname.methodname(optional: .additionalInformation).received.extension
Run Code Online (Sandbox Code Playgroud)
因此,您可能必须在其自身的方法中包含其他信息
public class StringTests1
{
[Theory,
InlineData("goodnight moon"),
InlineData("hello world")]
public void Contains(string input)
{
NamerFactory.AdditionalInformation = input; // <- important
Approvals.Verify(transform(input));
}
}
Run Code Online (Sandbox Code Playgroud)
3)在审批测试中处理数据驱动的测试
说实话,在大多数情况下,批准测试中的数据驱动方法不是方法装饰器中的参数.它通常通过带有lambda变换的VerifyAll.例如,上面可能看起来像
[Fact]
public void UpperCase()
{
var inputs = new[]{"goodnight moon","hello world"};
Approvals.VerifyAll(inputs, i => "{0} => {1}".FormatWith(i, i.ToUpperInvariant()));
}
Run Code Online (Sandbox Code Playgroud)
哪个会创建收到的文件:
goodnight moon => GOODNIGHT MOON
hello world => HELLO WORLD
Run Code Online (Sandbox Code Playgroud)