I have a class that processes 2 xml files and creates a text file.
I would like to write a bunch of unit / integration tests that can individually pass or fail for this class that do the following:
- For input A and B, generate output.
- Compare the contents of the generated file with the contents of the expected output
- When the actual content differs from the expected content, the display fails and some useful information about the differences appears.
Below is a class prototype along with my first hit on unit tests.
Is there a template that I should use for this kind of testing, or are people inclined to write zillions from TestX () functions?
Is there a better way to convince the difference between text files from NUnit? Should I embed a diff algorithm with a text file?
class ReportGenerator
{
string Generate(string inputPathA, string inputPathB)
{
}
}
[TextFixture]
public class ReportGeneratorTests
{
static Diff(string pathToExpectedResult, string pathToActualResult)
{
using (StreamReader rs1 = File.OpenText(pathToExpectedResult))
{
using (StreamReader rs2 = File.OpenText(pathToActualResult))
{
string actualContents = rs2.ReadToEnd();
string expectedContents = rs1.ReadToEnd();
Assert.AreEqual(expectedContents, actualContents);
}
}
}
static TestGenerate(string pathToInputA, string pathToInputB, string pathToExpectedResult)
{
ReportGenerator obj = new ReportGenerator();
string pathToResult = obj.Generate(pathToInputA, pathToInputB);
Diff(pathToExpectedResult, pathToResult);
}
[Test]
public void TestX()
{
TestGenerate("x1.xml", "x2.xml", "x-expected.txt");
}
[Test]
public void TestY()
{
TestGenerate("y1.xml", "y2.xml", "y-expected.txt");
}
}
Update
I am not interested in testing diff functionality. I just want to use it to get more readable crashes.
source
share