VS2010单元测试的使用【2】-数据驱动的单元测试

  上一篇详细记录了一下UnitTest的简单使用,测试时需要自己输入测试值,这里继续记录一下数据驱动下的单元测试,可以通过读取配置好的数据,执行测试。

  数据驱动测试,即黑盒测试(Black-box Testing),又称为功能测试,是把测试对象看作一个黑盒子。利用黑盒测试法进行动态测试时,需要测试软件产品的功能,不需测试软件产品的内部结构和处理过程。数据驱动测试注重于测试软件的功能性需求,也即数据驱动测试使软件工程师派生出执行程序所有功能需求的输入条件。【--百度百科】

  其实说白了,就是在执行测试单元的时候,遍历整个数据源的行,一次提取指定的数据,通过Assert断言判断是否全部通过。

  在测试用例类中新增一个测试方法AddTestFromSql,打开Test View窗口  

  在上图的右首可以看到Test View,选中AddTestFromSql,按F4查看它的属性:

Data Connection String 字段可以用于指定数据源:

选择Server:

最后会在AddTestFromSql方法前增加一个[DataSource]的属性

View Code
 [TestMethod()]
[DataSource("System.Data.OleDb","Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\test.mdb",
"",DataAccessMethod.Sequential),DeploymentItem("MyUnitTest\\test.mdb"),TestMethod()]
public void AddTestFromSql()
{
double a = double.Parse(TestContext.DataRow["a"].ToString());
double b = double.Parse(TestContext.DataRow["b"].ToString());
double expected = double.Parse(TestContext.DataRow["expected"].ToString());
double actual;
actual = NumberOperation.Add(a, b);
Assert.AreEqual(expected, actual);
//Assert.Inconclusive("Verify the correctness of this test method.");
}

【msdn:http://msdn.microsoft.com/zh-cn/library/ms182527.aspx

 

posted @ 2011-10-24 11:17  liver.wang  阅读(768)  评论(0编辑  收藏  举报