软件测试 pdf 是否正确创建 [英] Software testing a pdf is created correctly

查看:31
本文介绍了软件测试 pdf 是否正确创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Laravel 中编写了一个 REST 应用程序.它接受 Json 负载并使用此数据创建格式化的 pdf.

I have written a REST application in laravel. It accepts a Json payload and creates a formatted pdf using this data.

是否可以编写一个测试来检查 pdf 是否已正确生成?

Is it possible to write a test that checks the pdf has been generated correctly?

理想情况下,我想知道 pdf 没有损坏,即.将在 pdf 阅读器中打开.

Ideally I'd like to know the pdf is not corrupted, ie. Will open in a pdf reader.

另外,以某种方式检查pdf的内容也会很好.例如...它是否包含客户名称?

Also it would be good to somehow check the content of the pdf. For example... does it contain the customers name?

谢谢.

推荐答案

是的,这是可能的,因为相同的 JSON 输入总是生成相同的 PDF.

Yes this is possible, given that the same JSON input always generates the same PDF.

您不会真正检查 PDF 文件.PDF 是一种复杂的格式,基于 PostScript 和一些黑魔法.

You wouldn't really check the PDF file. PDF is a complex format, based on PostScript and some dark magic.

您可以做的是生成一次示例"PDF,然后编写一个单元测试,使用相同的输入数据生成一个 PDF 文件,然后将其与您的示例进行比较.

What you can do is generate a "sample" PDF once, then write a unit test that uses the same input data to generate a PDF file, then compare this to your sample.

这看起来像(只是一些示例代码):

This would look something like (just some example code):

$myPdf = $pdfGenerator->generatePdf();
$samplePdf = file_get_contents('/some/example/file.pdf');

// with PHPunit
$this->assertEquals(0, strcmp($myPdf, $samplePdf));

这有点脏,但它确实有效……如果您的 PDF 或 JSON 实现中的某些内容发生更改,单元测试会让您意识到这一点.

That's a bit dirty, but it does the job … if something in your PDF or JSON implementation changes, the unit test will make you aware of it.

但是,重要的是您的 PDF 生成器不会插入任何动态"数据,例如日期戳.在这种情况下,PDF 文件显然永远不会完全相同.

It is important, however, that your PDF generator does not insert any "dynamic" data, such as date stamps. In that case, the PDF files could obviously never be identical.

这篇关于软件测试 pdf 是否正确创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆