tags:

views:

297

answers:

5

This might be an interesting question. I need to test that if I can successfully upload and fetch the PDF file. This works for the text based files but I just wanted to check for PDF. For this unit test to run I need a PDF file. There are couple of options. I can create a dummy PDF file and store it some folder and read that file and save the file to the system. But now, my unit test is dependent on the PDF file. So, anyone who runs the unit test must have the PDF file which is kinda bad.

Another way for me is to create a PDF file. This is not a big deal as I can simply create a dummy file with the .pdf extension OR I can even use some PDF third party tool to create PDF file.

Another way also is to embed the PDF document as an embedded resource and then extract that from the assembly.

What do you think is the best way to handle this issue?

+1  A: 

I normally add a real file along side tests that need external content. This way you're testing with a real file, and can easily replace it for different types of content testing.

GavinCattell
+5  A: 

Save a PDF file with your tests in a resources directory. Your tests should be as simple as possible, and creating a file is just one more point that could fail.

Bill the Lizard
+1  A: 

I think it's better to deal with the "real" objects as much as possible. Introducing "mock" (in this case it is not the exact term, though) objects can help only if handling the test data set is unfeasible. I don't think that putting a test file in your version control system is a big deal, so better go with it rather than writing lots of code which may lead to other bugs and testing.

Use a PDF very close to the expected average file, too.

Manrico Corazzi
A: 

My concern is that if I place a file in a different directory.. let's say resources under unit tests then don't I need the complete path to the file to access it. I am running my tests manually. Also, when I move to a different machine and place my solution in a folder with different name then the path to the file gets messed up.

Unless there is some way that I can access the project's folder from within my application (there should be).

azamsharp
Don't you use source control? Just put the file into that and everyone gets the same layout all the time. What's the problem with that?
André
You can check out the projects to different directories or drives. I had a similar issue, I just made my test reference a path like this:_path = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName + "path";
nportelli
+1  A: 

Adding a pdf file (or a dummy file with pdf extension) to the resources is the way to go. You should be able to access it by relative path (e.g. ....\bla\foo.pdf) from your test unit.

And do not try to create a valid pdf file just in order to test if you have read or write access. The KISS principle applies...

Treb