March
23

Using Files In Unit Tests

Posted In: TDD by rauchy

One of the ten commandments in Unit Testing states “Thou shalt not access files during unit tests” (check it out here).
I can’t agree to that entirely, as I have been involved in a system whose main functionality dealt with files. In order to make that system unit tested, we had to permit file access for our unit tests. But these days I am working on a project which heavily relies on text analysis, and for this I find CommentReader extremely comfortable.
In a sentence, CommentReader allows you to use text from your test’s comment block inside your unit test. For example:

/// <input>
/// This is my input 
/// </input>
[ Test ] 
public void YourTestName() 
{ 
     string input = CommentReader.GetElement( “input” ); 
     // input == “This is my input” 
     ... 
} 

This way you can make sure your test resources goes along with your test (if you make sure to enable Xml comments, of course) and no other test.
I think its a very elegant solution, as it immediately states what the test’s intentions are. You don’t have to open Xml documents for that any more.

http://www.bestbrains.dk/dansk.aspx/Artikler/Using_Files_In_Unit_Tests

Something to say?

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.