NUnit中的数据驱动测试?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NUnit中的数据驱动测试?相关的知识,希望对你有一定的参考价值。
在MSTest中,您可以执行以下操作:
[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
"testdata.csv", "testdata#csv", DataAccessMethod.Sequential)]
public void TestSomething()
{
double column1 = Convert.ToDouble(TestContext.DataRow["column1"]);
...
Assert.AreEqual(...);
}
NUnit 2.5中的等效代码是什么?
我会看看parameterized tests documentation in NUnit 2.5,看看你是否可以做一些像你在那里做的事情。我不记得NUnit有内置的CSV读取属性来驱动参数化测试。某处可能有社区插件。
我还应该指出,如果您只是寻找非MS单元测试框架库来帮助您,xUnit.net确实具有此功能。看看这个blog post from Ben Hall
我在NUnit中进行了基于csv的数据驱动测试,工作如下:
使用csv reader from code project,包含在一个私有方法中,在您的测试类中返回IEnumerable,然后在测试用例中使用TestCaseSource属性引用它。在项目中包含csv文件,并将“复制到输出目录”设置为“始终复制”。
using System.Collections.Generic;
using System.IO;
using LumenWorks.Framework.IO.Csv;
using NUnit.Framework;
namespace mytests
{
class MegaTests
{
[Test, TestCaseSource("GetTestData")]
public void MyExample_Test(int data1, int data2, int expectedOutput)
{
var methodOutput = MethodUnderTest(data2, data1);
Assert.AreEqual(expectedOutput, methodOutput, string.Format("Method failed for data1: {0}, data2: {1}", data1, data2));
}
private int MethodUnderTest(int data2, int data1)
{
return 42; //todo: real implementation
}
private IEnumerable<int[]> GetTestData()
{
using (var csv = new CsvReader(new StreamReader("test-data.csv"), true))
{
while (csv.ReadNextRecord())
{
int data1 = int.Parse(csv[0]);
int data2 = int.Parse(csv[1]);
int expectedOutput = int.Parse(csv[2]);
yield return new[] { data1, data2, expectedOutput };
}
}
}
}
}
原帖:http://timwise.blogspot.com/2011/05/data-driven-test-in-nunit-with-csv.html
这是另一个与Tim Abell非常相似的例子,但没有使用CSV阅读器的框架并显示测试的细节。请注意,当您使用TestCaseAttribute时,可以省略TestAttribute。
[TestCaseSource("GetDataFromCSV")]
public void TestDataFromCSV(int num1,int num2,int num3)
{
Assert.AreEqual(num1 + num2 ,num3);
}
private IEnumerable<int[]> GetDataFromCSV()
{
CsvReader reader = new CsvReader(path);
while (reader.Next())
{
int column1 = int.Parse(reader[0]);
int column2 = int.Parse(reader[1]);
int column3 = int.Parse(reader[2]);
yield return new int[] { column1, column2, column3 };
}
}
public class CsvReader : IDisposable
{
private string path;
private string[] currentData;
private StreamReader reader;
public CsvReader(string path)
{
if (!File.Exists(path)) throw new InvalidOperationException("path does not exist");
this.path = path;
Initialize();
}
private void Initialize()
{
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
reader = new StreamReader(stream);
}
public bool Next()
{
string current = null;
if ((current = reader.ReadLine()) == null) return false;
currentData = current.Split(',');
return true;
}
public string this[int index]
{
get { return currentData[index]; }
}
public void Dispose()
{
reader.Close();
}
}
CSV数据:
10,200,210 20,190,210 30,180,210 40,170,210 50,160,210 60,150,210 70,140,210 80,130,210 90,120,210 100,110,210
注意:第3列是前两列的总和,这将在单元测试中声明。
结果:
使用TestCaseData对象查找下面的替代方法并设置返回类型(必须使用off-course)
[TestCaseSource("GetDataFromCSV2")]
public int TestDataFromCSV2(int num1, int num2)
{
return num1 + num2;
}
private IEnumerable GetDataFromCSV2()
{
CsvReader reader = new CsvReader(path);
while (reader.Next())
{
int column1 = int.Parse(reader[0]);
int column2 = int.Parse(reader[1]);
int column3 = int.Parse(reader[2]);
yield return new TestCaseData(column1, column2).Returns(column3);
}
}
MS Test提供了一种机制,可以从不同来源获取测试数据。但是NUnit没有开箱即用。
我赞成使用分离数据和代码来进行相当大的测试。当我将两者分开时,我的期望是1.测试数据的可读性2.测试数据应该易于修改3.单元测试应该在本地和构建环境中舒适地运行
下面的JsonSectionReader具有我想要的所有功能。该工具能够从嵌入的json文件中读取数据部分。它还提供了一种非常舒适的反序列化机制。
https://www.nuget.org/packages/WonderTools.JsonSectionReader/
附:我是这个项目的维护者,这个项目的创建是因为我没有找到任何其他工具来解决我想要的程度。
我认为Nunit等效是将方法标记为设置方法,然后将数据加载到要在后续测试中使用的字段中。
你必须或多或少地自己编写代码。
以上是关于NUnit中的数据驱动测试?的主要内容,如果未能解决你的问题,请参考以下文章
Oracle ODP.NET 托管驱动程序在 64 位中的运行速度比在 32 位中慢 50-100%
NUnit 5 Spring MVC 测试 NoSuchBeanDefinitionException 用于子模块中的 Autowired 依赖项