批准测试和PDF

时间:2012-10-10 05:11:29

标签: c# testing approval-tests

我可以将ApprovalTests与PDF一起使用吗?我尝试使用FileLauncher,但似乎相同的PDF在文件(位)级别略有不同。或者我错误地使用了它?

[TestMethod]
[UseReporter(typeof(FileLauncherReporter))]
public void TestPdf()
{
    var createSomePdf = PdfCreate();

    ApprovalTests.Approvals.Verify(new FileInfo(createSomePdf.FileName));

}

3 个答案:

答案 0 :(得分:6)

Pdf很可能是使用时间戳创建的。根据用于创建pdf的方法,您可以模拟创建的时间。但我不得不擦洗它。

以下是我过去常用的代码。

    public static void VerifyPdf(string coverFile)
    {
        ScrubPdf(coverFile);
        Approvals.Verify(new ExistingFileWriter(coverFile));
    }

    private static void ScrubPdf(string coverFile)
    {
        long location;
        using (var pdf = File.OpenRead(coverFile))
        {
            location = Find("/CreationDate (", pdf);

        }
        using (var pdf = File.OpenWrite(coverFile))
        {
            pdf.Seek(location, SeekOrigin.Begin);

            var original = "/CreationDate (D:20110426104115-07'00')";
            var desired = new System.Text.ASCIIEncoding().GetBytes(original);

            pdf.Write(desired, 0, desired.Length);
            pdf.Flush();
        }
    }

答案 1 :(得分:5)

我找到了一个命令行工具diff-pdf。比较2个PDF并返回退出代码0(如果它们相同),如果它们不同则返回1。下载+提取+将其添加到您的路径。

下行 - 它必须渲染两个PDF才能执行差异。如果他们很大,那就打击了。

审批人(严重依据ApprovalTests.Approvers.FileApprover):

public class DiffPdfApprover : IApprovalApprover
{
    public static void Verify(byte[] bytes)
    {
        var writer = new ApprovalTests.Writers.BinaryWriter(bytes, "pdf");
        var namer = ApprovalTests.Approvals.GetDefaultNamer();
        var reporter = ApprovalTests.Approvals.GetReporter();

        ApprovalTests.Core.Approvals.Verify(new DiffPdfApprover(writer, namer), reporter);
    }

    private DiffPdfApprover(IApprovalWriter writer, IApprovalNamer namer)
    {
        this.writer = writer;
        this.namer = namer;
    }

    private readonly IApprovalNamer namer;
    private readonly IApprovalWriter writer;
    private string approved;
    private ApprovalException failure;
    private string received;

    public virtual bool Approve()
    {
        string basename = string.Format(@"{0}\{1}", namer.SourcePath, namer.Name);
        approved = Path.GetFullPath(writer.GetApprovalFilename(basename));
        received = Path.GetFullPath(writer.GetReceivedFilename(basename));
        received = writer.WriteReceivedFile(received);

        failure = Approve(approved, received);
        return failure == null;
    }

    public static ApprovalException Approve(string approved, string received)
    {
        if (!File.Exists(approved))
        {
            return new ApprovalMissingException(received, approved);
        }

        var process = new Process();
        //settings up parameters for the install process
        process.StartInfo.FileName = "diff-pdf";
        process.StartInfo.Arguments = String.Format("\"{0}\" \"{1}\"", received, approved);

        process.Start();

        process.WaitForExit();

        if (process.ExitCode != 0)
        {
            return new ApprovalMismatchException(received, approved);
        }

        return null;
    }

    public void Fail()
    {
        throw failure;
    }

    public void ReportFailure(IApprovalFailureReporter reporter)
    {
        reporter.Report(approved, received);
    }

    public void CleanUpAfterSucess(IApprovalFailureReporter reporter)
    {
        File.Delete(received);
        if (reporter is IApprovalReporterWithCleanUp)
        {
            ((IApprovalReporterWithCleanUp)reporter).CleanUp(approved, received);
        }
    }
}

验证:

DiffPdfApprover.Verify(pdfBytes);

diff-pdf也可以直观地显示差异。我为此推出了一个Reporter,但不要使用它。我认为如果在初始报告开发之后出现回归(这就是我现在所处的位置),它会派上用场。

public class DiffPdfReporter : GenericDiffReporter
{
    private static readonly string Path = FindFullPath("diff-pdf.exe");
    public DiffPdfReporter() : base(Path,
        GetArgs(),
        "Please put diff-pdf.exe in your %PATH%. https://github.com/vslavik/diff-pdf. And restart whatever's running the tests. Everything seems to cache the %PATH%.") { }

    private static string GetArgs()
    {
        return "--view \"{0}\" \"{1}\"";
    }

    private static string FindFullPath(string programInPath)
    {
        foreach (var path in from path in Environment.GetEnvironmentVariable("path").Split(';')
                             select path)
        {
            var fullPath = System.IO.Path.Combine(path, programInPath);
            if (File.Exists(fullPath))
                return fullPath;
        }
        return null;
    }
}

答案 2 :(得分:2)

现在看来这是内置于ApprovalTests。

用法:

Approvals.VerifyPdfFile(pdfFileLocation);

请参阅the source

public static void VerifyPdfFile(string pdfFilePath)
{
    PdfScrubber.ScrubPdf(pdfFilePath);
    Verify(new ExistingFileWriter(pdfFilePath));
}