Unit Tests for comparing text files in NUnit

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)
    {
        //do stuff
    }
}

[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();                  

                 //this works, but the output could be a LOT more useful.
                 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");
     }

     //etc...
}

Update

I am not interested in testing diff functionality. I just want to use it to get more readable crashes.

+5
source share
5 answers

For multiple tests with different data, use the NUnit RowTest extension:

using NUnit.Framework.Extensions;

[RowTest]
[Row("x1.xml", "x2.xml", "x-expected.xml")]
[Row("y1.xml", "y2.xml", "y-expected.xml")]
public void TestGenerate(string pathToInputA, string pathToInputB, string pathToExpectedResult)
 {
      ReportGenerator obj = new ReportGenerator();
      string pathToResult = obj.Generate(pathToInputA, pathToInputB);
      Diff(pathToExpectedResult, pathToResult);
 }
+5
source

You are probably requesting testing against gold data. I donโ€™t know if there is a specific term for such testing accepted around the world, but this is how we do it.

. "void DoTest (string fileName)", , "string Transform (string text)", fileName.gold , , , . , . , , . ":" ":", , .

, Transform, , , :

[Test] public void TestX() { DoTest("X"); }
[Test] public void TestY() { DoTest("Y"); }

, . , . , , .. , , .

, , . , , . , diff . , , , , "".

+2

.AreEqual , . , ...

32 12 - "x", "y"

,

32 12,
A = t x st
B = t e sts

, , , , . , / , , !

0

, , unit test, . 2 xml diff, xml ( ) diff, . , . a1.xml, b1.xml, diff1.txt; a2.xml, b2.xml, diff2.txt; a3.xml, b3.xml, diff3.txt .., , .

, .

0

, , XmlReader . , XPath , .

PS: But in fact, it was always easy for me to read the entire file in a line and compare two lines. For reports, just make sure the test failed. Then, when I do debugging, I usually parse the files using Araxis Merge to see exactly where I am having problems.

0
source

All Articles