approvals/ApprovalTests.Net

`UseApprovalSubdirectory` does not work on derived classes

Closed this issue · 1 comments

Hello! Long time user, first time issue-er!

The Problem

I have two test classes that I want to contain all the same tests but with a slight tweak to each one. Normally the way I'd approach this is I'd have an abstract RootTest class where I write all the test cases with an abstract property/function for the behavior I want to change. Then I'd derive two child classes: ClassA : RootTest and ClassB : RootTest, who each override the property with their desired value.

For normal unit tests this works great, I get 2 tests per test! However my ApprovalTests go haywire because both derived classes want to use the same output file name, RootClass.test_case_name.[received/approved]. I came across the UseApprovalSubdirectory attribute which almost solves my problem! But it seems that only the root abstract class is used for UseApprovalSubdirectory, so I hit the same wall I hit with the names.

As an aside: It sure would be nice if the filenames used the name of the derived class. That would be a great workaround for this problem. I can probably customize this within my own use case (haven't figured out how yet but I'll get there), but it would be really cool if that worked out of the box. (see Appendix at the bottom)

Repro

Random details (that probably don't matter):

  • I'm on C# 8.0, netcoreapp3.1
  • I'm using xunit
  • I got ApprovalTests through NuGet
  • I'm using Rider
using System;
using ApprovalTests;
using ApprovalTests.Namers;
using ApprovalTests.Reporters;
using Xunit;

[UseReporter(typeof(RiderReporter))]
public abstract class HelloTests
{
    [Fact]
    public void say_your_name()
    {
        Approvals.Verify($"Hello I am {Name}");
    }

    public abstract string Name { get; }
}

[UseApprovalSubdirectory("BobsApprovalTests")]
public class BobTests : HelloTests
{
    public override string Name => "Bob";
}

[UseApprovalSubdirectory("AliceApprovalTests")]
public class AliceTests : HelloTests
{
    public override string Name => "Alice";
}

My expectation is I'd get the following files as output:

BobsApprovalTests/HelloTests.say_your_name.received
AliceApprovalTests/HelloTests.say_your_name.received

Instead I get:

./HelloTests.say_your_name.received
./HelloTests.say_your_name.received # oops! same file twice!

Appendix

It would be nice if this worked, maybe that's a separate issue

./AliceTests.say_your_name.received
./BobTests.say_your_name.received

This project is not being actively maintained. Instead consider using Verify. See Migrating from ApprovalTests for more information.