Search code examples
c#unit-testingapproval-tests

How to place approval file for approvaltests in a folder of their own?


I can't find out how to move the .approved. files to a folder of their own in Approval-tests. I guess the information is there somewhere - I just can't find it.

https://github.com/approvals/ApprovalTests.Net


Solution

  • The current way to do it is by annotating the fixture or at the assembly level with

    [ApprovalTests.Namers.UseApprovalSubdirectory("foldername")]
    

    If you are before version 3.2 you can create a custom namer that will handle this if you want to. The basics are:

    Override the namer for your framework, and override the method SourcePath

    public string SourcePath
    {
        get { return base.SourcePath + @"\yourSubfolder"; }
    }
    

    Then you need to add your new namer to the stack

    StackTraceParser.AddParser(new MyNamer());
    

    Although I would ask why you want the separation of the approval files to a subdirectory of your tests? I'm sure there is a good reason, but I have found it nicer to keep them closer to my actual tests.