5

I'm using ApprovalTests.Net. I see that I can specify various reports. I would like to automatically approve the tests when I run the unit tests. I need to do this only temporarily, or when the code goes through major changes. This is especially useful when creating data-driven ApprovalTests using ApprovalResults.ForScenario.

Is there a way to do that?

zumalifeguard
  • 8,648
  • 5
  • 43
  • 56

3 Answers3

7

Create an reporter but rather than implementing IApprovalFailureReporter, implement IReporterWithApprovalPower. IReporterWithApprovalPower has an additional method, ApprovedWhenReported where you can do the work of approving the test.

Here's an example reporter that will automatically copy the received file to the approved file:

// Install-package ApprovalTests 
// Install-package ObjectApproval
public class AutoApprover : IReporterWithApprovalPower
{
    public static readonly AutoApprover INSTANCE = new AutoApprover();

    private string approved;
    private string received;

    public void Report(string approved, string received)
    {
        this.approved = approved;
        this.received = received;
        Trace.WriteLine(string.Format(@"Will auto-copy ""{0}"" to ""{1}""", received, approved));
    }

    public bool ApprovedWhenReported()
    {
        if (!File.Exists(this.received)) return false;
        File.Delete(this.approved);
        if (File.Exists(this.approved)) return false;
        File.Copy(this.received, this.approved);
        if (!File.Exists(this.approved)) return false;

        return true;
    }
}

You can then use it in your test class or method the way you specify any approver:

[UseReporter(typeof(AutoApprover))] 
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
zumalifeguard
  • 8,648
  • 5
  • 43
  • 56
  • 1
    This is now a first class feature to Verify: https://github.com/approvals/ApprovalTests.Net/issues/154#issuecomment-572862715 – zumalifeguard Jan 14 '20 at 17:23
  • A note of caution: When auto-approving, it's import for the tests to still fail if the approved and received differ. Otherwise, you never find out that the test failed. For me, when I tried this reporter, the tests still passed, even if the outputs had changed and the auto-approving was triggered... – Clare Macrae Mar 06 '21 at 21:54
2

This strikes me as a very bad idea. If you use the ClipboardReporter, or AllFailingTestsClipboardReporter you can approve them with a single paste to the command line, but to auto-approve without checking or understanding why they changed seem to defeat all advantages of testing.

Why would you want to do this?

Simon
  • 33,714
  • 21
  • 133
  • 202
llewellyn falco
  • 2,281
  • 16
  • 13
  • 2
    You would never want to do this without understanding what is going on. But let's say you've done a bunch of refactoring, and the unit tests have moved around.. you have thousands of them. You know the logic of the unit tests haven't changed. They're just in different class-names and namespace. Rather than go through a 1000 files and figure out where each goes, you can just delete all the approval.txt files and regenerate them. If refactoring tools that move methods and classes around understood about these extra files, that wouldn't be necessary to do. – zumalifeguard Jun 07 '16 at 00:40
  • 3
    I use this type of Reporter to approve everything too. My workflow involves changing production code and running the tests, automatically approving everything. Then before I commit I check the diff in code AND .approved files with a `git diff HEAD --ignore-space-at-eol --ignore-space-change` (aliased for brevity). Pretty quick when you have a battery of .approved files. – Trey Mack Jun 15 '16 at 02:07
  • Another use-case where AutoApprover could be useful: cleaning up old 'approved' files no longer needed for a test. 1. Assert build and all tests are green 2. Delete all .approved files from sourcecontrol 3. Set AutoApprover as Reporter 4. Re-run all tests, AutoApprover recreates missing files 5. Really delete form source control the remaining files – janv8000 Jul 05 '22 at 14:07
0

ApprovalTests now supports DiffEngineTray. DiffEngineTray sits in the Windows tray and monitors all pending approvals. It can be use to bulk approve, open diff tools, etc

Simon
  • 33,714
  • 21
  • 133
  • 202