Creating a Just for the Sake on Unit Test File

This may be an interesting question. I need to check this if I can successfully download and extract the PDF file. This works for text files, but I just wanted to check the PDF. For this unit test to run, I need a PDF file. There are several options. I can create a dummy PDF file and save it in some folder and read this file and save the file in the system. But now my unit test depends on the PDF file. So anyone who runs unit test should have a PDF file which is bad.

Another way for me is to create a PDF file. This is not a big deal, as I can just create a dummy file with a .pdf extension. Or I can even use a third-party PDF tool to create a PDF file.

Another way is also to insert the PDF as an embedded resource and then extract it from the assembly.

What do you think is the best way to deal with this problem?

+4
source share
5 answers

Save the PDF file with your tests in the resource directory. Your tests should be as simple as possible, and file creation is another point that may fail.

+7
source

I usually add a real file for side tests that need external content. Thus, you are testing a real file and can easily replace it with various types of content testing.

+1
source

, "" . "" ( , ) , . , - , , , .

PDF .

+1

PDF ( pdf) - . (,....\bla\foo.pdf) .

And do not try to create a valid PDF file to check if you have read or write access. The KISS principle applies ...

+1
source

I take care that if I put the file in another directory, say, resources in unit tests, then I don’t need the full path to the file to access it. I run my tests manually. In addition, when I switch to another machine and place my solution in a folder with a different name, the file path becomes corrupted.

If there is any way to access the project folder from my application (should be).

0
source

All Articles