C# 将数据传递给nunit测试用例

C# 将数据传递给nunit测试用例,c#,nunit,C#,Nunit,如何将数据从testrunner传递到unittest 例如,主机的输出路径或接口配置?您可能已经在这一点上执行了不同的路径,但我想我会分享这一点。在NUnit的2.5后版本中,实现了通过外部源在测试环境中驱动测试用例的功能。我用CSV文件做了一个简单示例的演示 CSV包含我的两个测试输入和预期结果。第一个是1,1,2,依此类推 代码 using System; using System.Collections.Generic; using System.Linq; using System.I

如何将数据从testrunner传递到unittest


例如,主机的
输出路径
接口配置

您可能已经在这一点上执行了不同的路径,但我想我会分享这一点。在NUnit的2.5后版本中,实现了通过外部源在测试环境中驱动测试用例的功能。我用CSV文件做了一个简单示例的演示

CSV包含我的两个测试输入和预期结果。第一个是1,1,2,依此类推

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
namespace NunitDemo
{
    public class AddTwoNumbers
    {
        private int _first;
        private int _second;

        public int AddTheNumbers(int first, int second)
        {
            _first = first;
            _second = second;

            return first + second;
        }
    }

    [TestFixture]
    public class AddTwoNumbersTest 
    {

        [Test, TestCaseSource("GetMyTestData")]
        public void AddTheNumbers_TestShell(int first, int second, int expectedOutput)
        {
            AddTwoNumbers addTwo = new AddTwoNumbers();
            int actualValue = addTwo.AddTheNumbers(first, second);

            Assert.AreEqual(expectedOutput, actualValue, 
                string.Format("AddTheNumbers_TestShell failed first: {0}, second {1}", first,second));
        }

        private IEnumerable<int[]> GetMyTestData()
        {
            using (var csv = new StreamReader("test-data.csv"))
            {
                string line;
                while ((line = csv.ReadLine()) != null)
                {
                    string[] values = line.Split(',');
                    int first = int.Parse(values[0]);
                    int second = int.Parse(values[1]);
                    int expectedOutput = int.Parse(values[2]);
                    yield return new[] { first, second, expectedOutput };
                }
            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.IO;
使用系统文本;
使用System.Threading.Tasks;
使用NUnit.Framework;
名称空间NunitDemo
{
公共类addTwoNumber
{
私人INTU优先;
私人国际秒;
公共整数地址编号(整数第一,整数第二)
{
_第一=第一;
_秒=秒;
返回第一个+第二个;
}
}
[测试夹具]
公共类addTwoNumberTest
{
[测试,TestCaseSource(“GetMyTestData”)]
public void AddTheNumbers\u TestShell(int first,int second,int expectedOutput)
{
AddTwoNumbers addTwo=新的AddTwoNumbers();
int actualValue=addTwo.AddTheNumbers(第一个,第二个);
Assert.AreEqual(预期输出、实际值、,
Format(“AddTheNumbers_TestShell第一个失败:{0},第二个{1}”,第一个,第二个));
}
私有IEnumerable GetMyTestData()
{
使用(var csv=newstreamreader(“test data.csv”))
{
弦线;
而((line=csv.ReadLine())!=null)
{
string[]value=line.Split(',');
int first=int.Parse(值[0]);
int second=int.Parse(值[1]);
int expectedOutput=int.Parse(值[2]);
收益-返回新[]{first,second,expectedOutput};
}
}
}
}
}
然后,当您使用NUnit UI运行它时,它看起来像(出于示例目的,我包括了一个故障:


如果你这样做,你可能没有做一个好的单元测试。单元测试应该是非常独立的。我们没有使用
nunit
来测试主机上的软件。我们正在验证微控制器平台。因此,例如,我们需要计算机上的可用接口来连接微控制器。没有多少di与您运行集成测试的方式不同。例如,对数据库运行一些db代码。在某个地方,您有一个带有名称、用户、密码等的配置文件。这可能会很痛苦,但它不是单元测试。是的,您是对的。但nunit是一个强大的测试用例运行程序。因此,我们希望将其用于自定义测试。传递任意数据将d我们非常好-例如当前的
位置
使用机器人进行测试…很好的例子,谢谢。我可以建议将TestCaseSource(“GetMyTestData”)更改为使用nameof-TestCaseSource(nameof(GetMyTestData)),并且该方法需要是静态的吗