2012-10-10 24 views
5

我可以在PDF中使用ApprovalTests嗎?我嘗試使用FileLauncher,但似乎相同的PDF在文件(位)級別略有不同。或者我錯用了它?審批測試和PDF

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

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

} 

回答

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(); 
     } 
    } 
+0

輝煌,感謝盧埃林,也有我的PDF生成器(iTextSharp的),創建了一個修改的日期和文件ID必須增加他們太。 – joeriks

5

我發現了一個命令行工具diff-pdf。比較2個PDF並返回退出代碼0(如果它們相同),如果它們不同則返回1。下載+解壓縮並將其添加到您的PATH。

缺點 - 它必須渲染兩個PDF才能執行差異。如果它們很大,那麼perf命中。

審批(很大程度上基於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可以在視覺上顯示的diff爲好。我爲此編了一個記者,但不要多用它。我認爲如果在初始報告開發後出現迴歸(這是我現在所處的位置),它會派上用場。

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; 
    } 
} 
1

看起來這是內置於ApprovalTests現在。

用法:

Approvals.VerifyPdfFile(pdfFileLocation); 

the source

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

我是新來的ApprovalTests和我所看到的文檔相當稀少(尤其是PDF文件)。你能否解釋你將如何編寫一個單元測試來驗證PDF? github上的示例使它看起來像一個filename.recieved.pdf應該已經創建,但我找不到一個。 – Ben

+1

@Ben看看項目中的[單元測試示例](https://github.com/approvals/ApprovalTests.Net/blob/master/ApprovalTests.Tests/Pdf/PdfTest.cs)。但是你是對的,它似乎沒有爲PDF創建一個.received文件。我在我的測試中創建了自己。 – Mathew

+0

謝謝@Mathew。它現在至少運行比較,但仍然失敗。如果我比較兩個文檔我的自我創建日期是不同的(我想這是由PdfScrubber.ScrubPdf(...)方法),並且也有一些Guid靠近文件的底部不同 – Ben