【发布时间】:2020-01-02 03:38:05
【问题描述】:
我有一个方法,它接受一个文件作为输入,然后根据这个文件返回 N 个输出。
我想通过以下方式测试这个方法:假设我们有 M 个文件要测试。对于每个文件,我想在测试程序(或单独的文件)中添加一行,由文件路径和 N 个预期输出组成。这些数据应该会产生 N*M 个单独的测试,每对文件和预期输出一个。
有没有好的方法来实现这一点?我希望每次测试运行时对每个文件的解析不超过一次。
下面是一个做我想要的例子。如您所见,我必须为每个文件添加单独的测试类。我希望找到一个解决方案,我可以只添加带有测试数据的行(例如testData.Add(("thirdfile", 4), (348, 312));)来测试新文件。
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
namespace ConsoleApp
{
class Program
{
static void Main(string[] args)
{
}
}
public static class FileParser
{
private static int n = 0;
public static void Init(int parameter)
{
n = parameter;
}
public static (int output1, int output2) ParseFile(string filename)
{
return (filename[0] * n, filename[1] * n);
}
}
public class Tests
{
private Dictionary<(string, int), (int, int)> testData;
public Tests()
{
testData = new Dictionary<(string, int), (int, int)>();
testData.Add(("somefile", 3), (345, 333));
testData.Add(("anotherfile", 4), (291, 330));
testData.Add(("thirdfile", 4), (348, 312));
}
public void TestOutput1((int, int) result, string filename, int parameter)
{
Assert.AreEqual(testData[(filename, parameter)].Item1, result.Item1);
}
public void TestOutput2((int, int) result, string filename, int parameter)
{
Assert.AreEqual(testData[(filename, parameter)].Item2, result.Item2);
}
}
[TestClass]
public class Somefile
{
protected static (int, int) fileParseResult;
[ClassInitialize]
public static void ClassInit(TestContext context)
{
FileParser.Init(3);
fileParseResult = FileParser.ParseFile("somefile");
}
[TestMethod]
public void SomefileOutput1() { var tests = new Tests(); tests.TestOutput1(fileParseResult, "somefile", 3); }
[TestMethod]
public void SomefileOutput2() { var tests = new Tests(); tests.TestOutput2(fileParseResult, "somefile", 3); }
}
[TestClass]
public class Anotherfile
{
protected static (int, int) fileParseResult;
[ClassInitialize]
public static void ClassInit(TestContext context)
{
FileParser.Init(3);
fileParseResult = FileParser.ParseFile("anotherfile");
}
[TestMethod]
public void AnotherfileOutput1() { var tests = new Tests(); tests.TestOutput1(fileParseResult, "anotherfile", 4); }
[TestMethod]
public void AnotherfileOutput2() { var tests = new Tests(); tests.TestOutput2(fileParseResult, "anotherfile", 4); }
}
[TestClass]
public class Thirdfile
{
protected static (int, int) fileParseResult;
[ClassInitialize]
public static void ClassInit(TestContext context)
{
FileParser.Init(3);
fileParseResult = FileParser.ParseFile("thirdfile");
}
[TestMethod]
public void ThirdfileOutput1() { var tests = new Tests(); tests.TestOutput1(fileParseResult, "thirdfile", 4); }
[TestMethod]
public void ThirdfileOutput2() { var tests = new Tests(); tests.TestOutput2(fileParseResult, "thirdfile", 4); }
}
}
【问题讨论】:
-
您标记了
xunit.net,但[TestMethod]不是来自Microsoft 的 测试框架吗?我认为 xUnit 有[Fact]属性? - 具体的测试框架可能对于可能的实现至关重要。 -
@Jeroen 是的,没错。我希望能够使用 xUnit,但我对两者都持开放态度。
-
“如果涉及文件系统,测试就不是单元测试”artima.com/weblogs/viewpost.jsp?thread=126923 但是您可以使用github.com/System-IO-Abstractions/System.IO.Abstractions轻松模拟文件系统
-
为什么不用T4模板生成单元测试代码呢?所有的文件探测和解析魔法都在生成器中,单元测试最终变得干净简单。
-
@dfhwze 我没听说过。我会调查的。
标签: c# unit-testing mstest xunit xunit.net