0

I have some methods im trying to test which use a StreamReader dependency that i mock using Rhino mocks. But it is a large xmlFile. In the spirit of unit testing im trying to keep away from using a test file in my test project.

On the other hand copy-pasting 100 lines of xml into my test class seems sort of ugly. What is the best practice when returning large strings using a stub.

1
  • why not just use the test file? so much simpler and much less annoying to deal with Commented Mar 26, 2014 at 16:30

2 Answers 2

2

In the spirit of unit testing im trying to keep away from using a test file in my test project.

In the battle between idealism and pragmatism, sometimes a truce is needed.

The main push for not using test files is threefold:

  1. Your job isn't to test reading files from disk, it's to test the stuff at hand.
  2. Having files tends towards multiple tests using the same file, which in turn tends towards the tests not being isolated.
  3. Reading from disk is pretty much always going to be slower.

Which conflict against three of the core traits of unit tests: fast, isolated, focused.

But you're right. Having giant blobs of xml in your tests isn't terribly maintainable or pretty. Simply placing a small file into your test project isn't a gross violations of these motivations. In C#, loading a file is dead simple and nearly foolproof. You'll need to work to keep the file isolated to keep it from being an inter-test dependency, and you may need to move it back to code if it slows your tests too much.

1

I'm not sure what "spirit of unit testing" you are talking about. But if you really need to address that spirit ...

  1. Have basic tests that use small data sets. You can put string constants in your code for this purpose.

  2. Have not-so-basic tests that actually test the things that need to be tested. For these tests, your driver can feel free to call on all the resources you need to get the job done. The file system is such a resource.

  3. The files which contain the test data become configuration items that are controlled in your software configuration management system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.