TestFixture属性
这个属性标识一个包含测试和可选的建立或删除方法的类。
SetUp属性
这个属性用在TestFixture中,用以提供一组通用的、在每个测试方法调用之前执行的函数。
Test属性
这个属性标识在一个TestFixture 类中的一个方法作为一个测试。
Assertions
assertion是一个函数或宏,它核实测试下单元的动作(或状态)。assertion失败会抛出一个异常,中断当前测试的执行。
下面是我们的测试内容。
[TestFixture] public class DBTest { private DataTable dt1; private DataTable dt2; private String logFilePath = "C:\\Windows\\Temp\\PnLDiff.txt"; [SetUp] public void Init() { String comparedDate = "2009-10-16"; String connstr1 = "Data Source=localhost;Initial Catalog=TestDB;Integrated Security=true"; String selectQry1 = "SELECT [AccountNumber],[NetSales],[Costs],[NetIncome] " + "FROM [testDB].[dbo].[OldPnLReport] " + "WHERE vol.[ModifiedDate] >= @comparedDate;"; TestDataTable tdt1 = new TestDataTable(connstr1, selectQry1, CommandType.Text); tdt1.SetInputParameter("@comparedDate", comparedDate); dt1 = tdt1.Table; String connstr2 = "Data Source=localhost;Initial Catalog=TestDB;Integrated Security=true"; String selectQry2 = "SELECT [AccountNumber],([FurnitureSales] + [ServiceSales]) as [NetSales],[Costs],[NetIncome] " + "FROM [testDB].[dbo].[NewPnLReport] " + "WHERE [date] >= @comparedDate;"; TestDataTable tdt2 = new TestDataTable(connstr2, selectQry2, CommandType.Text); tdt2.SetInputParameter("@comparedDate", comparedDate); dt2 = tdt2.Table; } [Test] public void ResultSetEqual_Test() { Assert.IsTrue(ResultSetComparer.AreIdenticalResultSets(dt1, dt2, logFilePath )); } } |
让我们运行nunit-console命令来测试我们的用例。
cd "C:\Program Files (x86)\NUnit 2.5.2\bin\net-2.0"
nunit-console.exe "D:\DatabaseUnitTesting\DBUnitTestDemo\DBUnitTestDemo\bin\Release\DBUnitTestDemo.dll" /nologo /nodots |