visual studio 2010 单元测试{整合NDbUnit}
时间:2010-08-28 来源:.net's
开场白
表结构的准备
Table: Customer CustomerId: int, auto-increment PK Firstname: varchar50, nullable Lastname: varchar50, nullable
CREATE TABLE [dbo].[Customer]( [CustomerId] [int] IDENTITY(1,1) NOT NULL, [Firstname] [varchar](50) NULL, [Lastname] [varchar](50) NULL, CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED ( [CustomerId] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
创建强类型数据集
- 添加Database.xsd数据集
在单元测试的项目中右击添加项目,添加一个Database.xsd
- 在visual studio的Server Explorer中打开测试数据库
- 将表Customer拖到DataSet设计窗口中,并删除其中的DataAdapter适配器(因不需要)
- 保存Database.xsd
添加单元测试所需文件
创建一个单元测试项目, 双击解决方案下的单元测试配置文件Local.testsettings,
在部署项目中添加需要的文件,这样当运行单元测试的时候,将会所添加的文件拷贝到当前解决方案下的文件夹TestResults的子目录Out中,
即单元测试运行的应用程序域目录。
创建测试数据的XML文件
<?xml version="1.0" standalone="yes"?> <Database xmlns="http://tempuri.org/Database.xsd"> <Customer> <CustomerId>5</CustomerId> <Firstname>joe</Firstname> <Lastname>yang</Lastname> </Customer> <Customer> <CustomerId>6</CustomerId> <Firstname>steve</Firstname> <Lastname>gu</Lastname> </Customer> <Customer> <CustomerId>7</CustomerId> <Firstname>steve</Firstname> <Lastname>gu</Lastname> </Customer> </Database>
我们使用NDbUnit自带的函数来生成该文件,调用如下接口:
_connectionString = GetConnectionString(); NDbUnit.Core.INDbUnitTest _mySqlDatabase = new NDbUnit.Core.SqlClient.SqlDbUnitTest(_connectionString); _mySqlDatabase.ReadXmlSchema(@"Database.xsd"); //为了方便,直接从数据库中抓取数据来测试 System.Data.DataSet ds = _mySqlDatabase.GetDataSetFromDb(); ds.WriteXml("TestData.xml");
在测试类初始化的时候,我们将调用以上方法。
创建单元测试类
- 通知NDbunit我们所使用的schema以及数据文件
[TestMethod] public void TestMethod1() { string connectionString = "server=localhost;user=dbuser;password=dbpassword;initial catalog=MyDatabase;"; NDbUnit.Core.INDbUnitTest mySqlDatabase = new NDbUnit.Core.SqlClient.SqlDbUnitTest(connectionString); mySqlDatabase.ReadXmlSchema(@"Database.xsd"); mySqlDatabase.ReadXml(@"Testdata.xml"); }
- 运行依赖于数据库的测试
[TestMethod] public void TestMethod1() { string connectionString = "server=localhost;user=dbuser;password=dbpassword;initial catalog=MyDatabase;"; NDbUnit.Core.INDbUnitTest mySqlDatabase = new NDbUnit.Core.SqlClient.SqlDbUnitTest(connectionString); mySqlDatabase.ReadXmlSchema(@"Database.xsd"); mySqlDatabase.ReadXml(@"Testdata.xml"); mySqlDatabase.PerformDbOperation(NDbUnit.Core.DbOperationFlag.CleanInsertIdentity); CustomerRepository repository = new CustomerRepository(); Assert.AreEqual(2, repository.GetAllCustomers().Count); }
- 清楚数据库测试痕迹
// Use TestCleanup to run code after each test has run [TestCleanup()] public void MyTestCleanup() { _mySqlDatabase.PerformDbOperation(NDbUnit.Core.DbOperationFlag.CleanInsertIdentity); }
完整测试类代码如下:
namespace DataAccessLayer.Test { /// <summary> ///This is a test class for NhibernateDataProviderTest and is intended ///to contain all NhibernateDataProviderTest Unit Tests ///</summary> [TestClass()] public class NhibernateDataProviderTest { private static string _connectionString; private static NDbUnit.Core.INDbUnitTest _mySqlDatabase; private TestContext testContextInstance; /// <summary> ///Gets or sets the test context which provides ///information about and functionality for the current test run. ///</summary> public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } public static string GetConnectionString() { string conn = System.Configuration.ConfigurationManager.ConnectionStrings["DataAccessLayer.Test.Properties.Settings.DineDiscussExampleConnectionString"].ConnectionString; return conn; } #region Additional test attributes // //You can use the following additional attributes as you write your tests: // //Use ClassInitialize to run code before running the first test in the class [ClassInitialize()] public static void MyClassInitialize(TestContext testContext) { //when the fixture is setup, we configure our NDbUnit class instance and save it in the fixture _connectionString = GetConnectionString(); _mySqlDatabase = new NDbUnit.Core.SqlClient.SqlDbUnitTest(_connectionString); _mySqlDatabase.ReadXmlSchema(@"Database.xsd"); System.Data.DataSet ds = _mySqlDatabase.GetDataSetFromDb(); ds.WriteXml("TestData.xml"); _mySqlDatabase.ReadXmlSchema(@"Database.xsd"); _mySqlDatabase.ReadXml(@"Testdata.xml"); } // //Use TestCleanup to run code after each test has run [TestCleanup()] public void MyTestCleanup() { _mySqlDatabase.PerformDbOperation(NDbUnit.Core.DbOperationFlag.CleanInsertIdentity); } // #endregion [TestMethod()] public void TestMethod1() { //invoke the test; with the setup moved out of here, the test is simpler and cleaner! CustomerRepository repository = new CustomerRepository(); Assert.AreEqual(2, repository.GetAllCustomers().Count); } } }
题外话
如果使用Nunit或MbUnit, 则可以整合Proteus,或Microdesk.Utility.UnitTest使用。
参考:NDbUnit Quick-start examples and walk-through
相关阅读 更多 +