Ada*_*gen 5 diff nunit unit-testing text-files
我有一个类处理2 xml文件并生成一个文本文件.
我想写一堆单元/集成测试,可以单独传递或失败,以便执行以下操作:
下面是该课程的原型以及我在单元测试中的第一次尝试.
是否有我应该用于此类测试的模式,还是人们倾向于编写数以万亿的TestX()函数?
有没有更好的方法来哄骗NUnit的文本文件差异?我应该嵌入文本文件diff算法吗?
class ReportGenerator
{
string Generate(string inputPathA, string inputPathB)
{
//do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
[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...
}
Run Code Online (Sandbox Code Playgroud)
我对测试diff功能不感兴趣.我只是想用它来产生更多可读的失败.
对于使用不同数据的多个测试,请使用NUnit RowTest扩展:
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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3664 次 |
| 最近记录: |