How to add support for xunit's Theory attribute in Approvaltests

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 07:56:35

There are a couple parts to this answer & question.

  1. How to add
  2. The Namer
  3. Dealing with data driven tests in approvaltests

1) How to add

Adding is simple (if a bit rough) The method mentioned should have been static, but it works none the less.

To add one use ApprovalTests.StackTraceParsers.StackTraceParser.AddParser() method to add implementation of ApprovalTests.StackTraceParsers.IStackTraceParser with support for your testing framework.

so you'll need to do a

new StackTraceParser().AddParser(new TheoryNamer());

I apologize for this, and it will be static in the next version (v.21)

2) The Namer

The Namer is suppose to generate a unique name for each approved/received file. This is normally done on the name of the method, however the name here will not be unique as a theory based test will be data driven and therefore have multiple calls to the same method.

Naming:  classname.methodname(optional: .additionalInformation).received.extension

As such, you will probably have to include additional information in the method it's self

public class StringTests1
{
    [Theory,
    InlineData("goodnight moon"),
    InlineData("hello world")]
    public void Contains(string input)
    {
        NamerFactory.AdditionalInformation = input;  // <- important
        Approvals.Verify(transform(input));
    }
} 

3) Dealing with data driven tests in approvaltests

To be honest, in most cases, the data driven method of approach in Approval Tests isn't thru parameters in the Method Decorators. It is usually thru the VerifyAll with a lambda transform. For Example the above might look like

[Fact]
public void UpperCase()
{
    var inputs = new[]{"goodnight moon","hello world"};
    Approvals.VerifyAll(inputs, i => "{0} => {1}".FormatWith(i, i.ToUpperInvariant()));
}

Which would create the received file:

goodnight moon => GOODNIGHT MOON
hello world => HELLO WORLD

It's better to inherit TheoryNamer class from XUnitStackTraceParser.
It works perfect!
I think it would be cool to add such class into ApprovalTests.StackTraceParsers namespace :)

public class XUnitTheoryStackTraceParser : XUnitStackTraceParser
{
    public const string TheoryAttribute = "Xunit.Extensions.TheoryAttribute";

    protected override string GetAttributeType()
    {
        return TheoryAttribute;
    }
}

public class ApproveTheoryTest
{
    static ApproveTheoryTest()
    {
        StackTraceParser.AddParser(new XUnitTheoryStackTraceParser());
    }

    [Theory]
    [UseReporter(typeof(DiffReporter))]
    [InlineData("file1.txt")]
    [InlineData("file2.txt")]
    public void approve_file(string fileName)
    {
        NamerFactory.AdditionalInformation = fileName;
        Approvals.Verify("sample text");
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!