views:

57

answers:

2

I'm late to the party with regards to using unit testing... trying to figure best practices and the such. My question is, given a class which is responsible for generating a PDF (or Doc/Html/Xml/etc.), how would I go about testing the final output file is correct? I figure a text based file (xml), I could just see if the strings match, but what about a binary file (pdf)? Should I just check against a MD5 hash? Should I even be testing this?

Thanks in advance.

+1  A: 

Would it be a problem if it didn't work ? If yes, then yes you should be testing it.

Now to the next question of how, would a binary file compare utility work for comparing expected and actual pdfs ? If yes, I'd use that.

Gishu
+2  A: 

I use pdfbox to extract text from generated PDF and check if it cointains the data it should. this doesnt check if data is in the correct place, but I dont go that deep with pdf testing. You need think how deep you want to go, the deeper you go the more time you will spend fixing the tests after a change(i never had a bug that text was in the wrong place and maybe thats why i dont test for it).

Another way would be to use the same PDF library (you use to write it) to read it or use someting like iText if you generate PDF from template using some framework.

01