code_junkie

Unit Tests for comparing text files in NUnit

Nov 11th, 2011
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. class ReportGenerator
  2. {
  3. string Generate(string inputPathA, string inputPathB)
  4. {
  5. //do stuff
  6. }
  7. }
  8.  
  9. [TextFixture]
  10. public class ReportGeneratorTests
  11. {
  12. static Diff(string pathToExpectedResult, string pathToActualResult)
  13. {
  14. using (StreamReader rs1 = File.OpenText(pathToExpectedResult))
  15. {
  16. using (StreamReader rs2 = File.OpenText(pathToActualResult))
  17. {
  18. string actualContents = rs2.ReadToEnd();
  19. string expectedContents = rs1.ReadToEnd();
  20.  
  21. //this works, but the output could be a LOT more useful.
  22. Assert.AreEqual(expectedContents, actualContents);
  23. }
  24. }
  25. }
  26.  
  27. static TestGenerate(string pathToInputA, string pathToInputB, string pathToExpectedResult)
  28. {
  29. ReportGenerator obj = new ReportGenerator();
  30. string pathToResult = obj.Generate(pathToInputA, pathToInputB);
  31. Diff(pathToExpectedResult, pathToResult);
  32. }
  33.  
  34. [Test]
  35. public void TestX()
  36. {
  37. TestGenerate("x1.xml", "x2.xml", "x-expected.txt");
  38. }
  39.  
  40. [Test]
  41. public void TestY()
  42. {
  43. TestGenerate("y1.xml", "y2.xml", "y-expected.txt");
  44. }
  45.  
  46. //etc...
  47. }
  48.  
  49. using NUnit.Framework.Extensions;
  50.  
  51. [RowTest]
  52. [Row("x1.xml", "x2.xml", "x-expected.xml")]
  53. [Row("y1.xml", "y2.xml", "y-expected.xml")]
  54. public void TestGenerate(string pathToInputA, string pathToInputB, string pathToExpectedResult)
  55. {
  56. ReportGenerator obj = new ReportGenerator();
  57. string pathToResult = obj.Generate(pathToInputA, pathToInputB);
  58. Diff(pathToExpectedResult, pathToResult);
  59. }
  60.  
  61. [Test] public void TestX() { DoTest("X"); }
  62. [Test] public void TestY() { DoTest("Y"); }
Add Comment
Please, Sign In to add comment