How to add xunit Theory attribute support to Test Approval

When I try to use statements with my unit test decorated with the [Theory] attribute, it says:

 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) 

It appears that it only recognizes the [Fact] attributes. I tried to execute the link from stacktrace , but there is nothing on how to connect own namer / parser to statements.

Is there any entry point where I can add my own namer / parser? It itself seems trivial, the only question is how to use it:

  public class TheoryNamer : AttributeStackTraceParser { protected override string GetAttributeType() { return typeof(TheoryAttribute).FullName; } public override string ForTestingFramework { get { return "xUnit Extensions"; } } } 
+6
source share
2 answers

There are a couple of parts to this answer and question.

  • How to add
  • The namer
  • Working with data-based validations in approval tests

1) How to add

Adding is simple (if a little rough) The specified method should be static, but it works no less.

To add one use, the ApprovalTests.StackTraceParsers.StackTraceParser.AddParser () method to add an ApprovalTests.StackTraceParsers.IStackTraceParser implementation that supports your test environment.

so you need to do

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

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

2) Namer

Namer is supposed to create a unique name for each approved / accepted file. This is usually done by the name of the method, but the name here will not be unique, since a theory-based test will be data driven and therefore has several calls to the same method.

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

Thus, you probably have to include additional information in the it self method.

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

3) Work with data-based validations in approval tests

Honestly, in most cases, the data-based approach method in approval tests is independent of the parameters in Decorators. This usually happens through VerifyAll with lambda conversion. 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())); } 

To create the resulting file:

 goodnight moon => GOODNIGHT MOON hello world => HELLO WORLD 
+7
source

It is better to inherit the TheoryNamer class from XUnitStackTraceParser .
It works great!
I think it would be great to add such a class to the 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"); } } 
+1
source

Source: https://habr.com/ru/post/923381/


All Articles