zumalifeguard
zumalifeguard

Reputation: 9016

How do I automatically approve approval-tests when I run them?

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?

Upvotes: 5

Views: 1552

Answers (3)

Simon
Simon

Reputation: 34840

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

Upvotes: 0

zumalifeguard
zumalifeguard

Reputation: 9016

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))] 

Upvotes: 8

llewellyn falco
llewellyn falco

Reputation: 2351

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?

Upvotes: 2

Related Questions