c# - 对方法的每个输出进行单独的单元测试

标签 c# unit-testing mstest xunit xunit.net

我有一个方法将一个文件作为输入,然后根据该文件返回 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); }
    }
}

最佳答案

您实际上可以简化这一点,以便对该库的新测试不一定需要对测试库本身进行代码更改。

可以找到数据驱动单元测试的 MS 文档 here .

我见过有人对 csv 文件使用类似的东西,然后当需要新测试时,他们只需在 csv 文件中添加一行。

或者,我个人喜欢 MSTest 中提供的 DataRow 功能。 可以找到示例 MS Doc here .我更喜欢这个选项,尽管新的测试用例确实需要一行新的代码。

它应该减少整体代码量。有点像这样。

[TestClass]
public class FileClass
{
    [TestMethod]
    [DataRow("somefile", 3, 345, 333)]
    [DataRow("anotherfile", 4, 291, 330)]
    public void Output1IsValid(string fileName, int parameter, int resultX, int resultY) 
    { 
        var fileParseResult = FileParser.ParseFile(fileName);
        Assert.AreEqual(fileParseResult.Item1, resultX);         
    }

}

关于c# - 对方法的每个输出进行单独的单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57700368/

相关文章:

c# - MS 测试,试图将我的测试名称插入到测试功能中

c# - 派生类中的无意基类构造函数 "hiding"

c# - 如何写入脚本组件中的变量

c# - Fluent NHibernate - 应用自定义类型的约定

unit-testing - 当已经测试了每个组件方法时测试方法组合

dll - 为什么 MSTest 不复制引用的项目库?

c# - 在gridview中显示百分比值

unit-testing - 单元测试服务返回 'cannot add service class'错误消息

reactjs - 如何避免 React Jest 测试中的 "False positive"覆盖率

c# - 验证在 Task.Run 中调用了 Mock 方法