I'm interested in the opinion, practices and reccommended best practices of using lengthy setup strings in unit tests.
Do you prefer to declare the tests in-line, close to your test, or externalize in a file somewhere?
Note, I'm talking about test assets which are specific to a single unit test, so not neccessarily suitable for living in a setup() method.
I see pros/cons of both -- I'm a fan of keeping things important to your test as close to the test as possible, however a few lines of string setup can quickly become nosiy in a test method.
Eg., I'm writing a quick parser to strip unused css declarations from files. I wanna test that given a specific input string, the correct text is stripped out. My test has become really noisy with all the string concatenation.
public void removesStyleFromText()
{
StyleCleaner styleCleaner = new StyleCleaner();
String source = ".presentInFileOne {\r\n" +
"}\r\n" +
"\r\n" +
".presentInFileTwo {\r\n" +
" bottom-corners-rounded : false;\r\n" +
"}\r\n" +
".notUsed {\r\n" +
"}\r\n" +
"";
String actual = styleCleaner.removeDeclaration(source , "notUsed");
String expected = ".presentInFileOne {\r\n" +
"}\r\n" +
"\r\n" +
".presentInFileTwo {\r\n" +
" bottom-corners-rounded : false;\r\n" +
"}\r\n";
assertEquals(expected , actual);
}
Given this example, I could externalize the actual / expected into external files, but that also makes the test a little unclear as to what it's actually testing for.
Thoughts?
Regards
Marty Pitt