MbUnit3.0较之2.0在数据驱动测试方面有很大的提高,下文尝试将数据驱动的测试运用于TDD。
问题:
在TDD的过程中,对于持久层数据的单元测试一直是一个很头大的问题,就数据逻辑本身,可能并不负责,但大量可读性较差的数据(几十条记录硬编码到代码中,想必测试代码的维护工作也大得很),对于单元测试的维护造成了很大麻烦。
解决方案:
使用MbUnit3中数据驱动测试的概念,是解决持久层数据单元测试的一个很好方法。
下面举个简单的例子,来对持久层的一个简单地Query功能进行单元测试;
接口定义如下:
public interface IItemRepository { IList<ItemPersistence> Query(ItemQuery query); }
Model定义:
public class ItemPersistence { public Int32? Id { get; set; } public Int32? EntityId { get; set; } public Int32? TypeId { get; set; } public String Type { get; set; } public String Item { get; set; } public float? Amount { get; set; } public String Description { get; set; } public DateTime BillDate { get; set; } public String RecordOperator { get; set; } }
实现类:
public class ItemRepository : IItemRepository { public IList<ItemPersistence> Query(ItemQuery query,out int total) { throw new NotImplementedException(); } }
[Test] [XmlData("//QueryById/item",FilePath = @"Data\Items.xml")] public void TestQueryById( [Bind("id")] Int32? id,[Bind("entityId")] Int32? entityId,[Bind("name")] String name,[Bind("billDate")] String billDate,[Bind("type")] String type,[Bind("amount")] float amount,[Bind("description")] String description,[Bind("operator")] String recordOperator) { var expectedItem = new ItemPersistence { Id = id,EntityId = entityId,Type = type,Item = name,BillDate = DateTime.Parse(billDate),Amount = amount,Description = description,RecordOperator = recordOperator }; // run Int32 rTotal; var rItem = _target.Query(new ItemQuery { Id = expectedItem .Id},out rTotal); // verify Assert.AreEqual(1,rTotal); Assert.AreEqual(expectedItem.Id,rItem[0].Id); Assert.AreEqual(expectedItem.EntityId,rItem[0].EntityId); Assert.AreEqual(expectedItem.BillDate.ToString("yyyy-MM-dd"),rItem[0].BillDate.ToString("yyyy-MM-dd")); Assert.AreEqual(expectedItem.Type,rItem[0].Type); Assert.AreEqual(expectedItem.Item,rItem[0].Item); Assert.AreEqual(expectedItem.Amount,rItem[0].Amount); Assert.AreEqual(expectedItem.Description,rItem[0].Description); Assert.AreEqual(expectedItem.RecordOperator,rItem[0].RecordOperator); }
测试数据:
<?xml version="1.0" encoding="utf-8" ?> <shoppingCart> <QueryById> <item> <id>1</id> <entityId>1</entityId> <billDate>2011-01-01</billDate> <type>MARSHOSAURUS BICENTESIMUS</type> <name>OTHNIELIA REX</name> <amount>16.79</amount> <description>GENT</description> <operator>ALYCE</operator> </item> <item> <id>2</id> <entityId>2</entityId> <billDate>2011-01-02</billDate> <type>MEGALOSAURUS CLOACINUS</type> <name>PECTINODON BAKKERI</name> <amount>52.83</amount> <description>POSTPROCESS</description> <operator>SILVANUS</operator> </item> <item> <id>3</id> <entityId>1</entityId> <billDate>2011-01-03</billDate> <type>IGUANODON SUESSI</type> <name>ARCHAEORNITHOIDES DEINOSAURISCUS</name> <amount>58.76</amount> <description>PILGRIM</description> <operator>THORNTON</operator> </item> <item> <id>4</id> <entityId>2</entityId> <billDate>2011-01-04</billDate> <type>DYSALOTOSAURUS LETTOW-VORBECKI</type> <name>SHANTUNGOSAURUS GIGANTEUS</name> <amount>65.92</amount> <description>EMPTIES</description> <operator>HAYWOOD</operator> </item> <item> <id>5</id> <entityId>2</entityId> <billDate>2011-01-05</billDate> <type>MICROCERATOPS SULCIDENS</type> <name>TALARURUS PLICATOSPINEUS</name> <amount>11.31</amount> <description>WORKBENCH</description> <operator>STTELLA</operator> </item> </QueryById> </shoppingCart>