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?
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))]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With