在网上看到了别人写的一个示例:
怎么通过BaseClass来对数据库函数进行自动化单元测试:
其相关文章链接如下:
Steve Eichert
public void LoadProperties(BusinessObject bizObject) {
System.Reflection.PropertyInfo[] properties = BusinessObjectType.GetProperties();
foreach(PropertyInfo property in properties) {
if(property.CanWrite) {
SetDynamicPropertyValue(bizObject, property);
}
}
}


/// <summary>
/// Set the property of an <c>BusinessObject</c> to a random (dynamic) value.
/// </summary>
/// <param name="bizObject">The <c>BusinessObject</c> to set the value of.</param>
/// <param name="property">The <c>PropertyInfo</c> to set the value of.</param>
private void SetDynamicPropertyValue(BusinessObject bizObject, PropertyInfo property) {
Random random;
string propertyKey = bizObject.GetType().Name + "-" + property.Name;
if(_allowableValues[propertyKey] != null) {
object[] values = (object[]) _allowableValues[propertyKey];
random = new Random(Environment.TickCount);
property.SetValue(bizObject, values.GetValue(random.Next(values.Length)), null);
} else {
ArrayList usedValues = (ArrayList)_uniqueValues[propertyKey];
switch(property.PropertyType.ToString()) {
case "System.String":
property.SetValue(bizObject, GetRandomString(usedValues), null);
break;}
case "System.Int32":
case "System.Double":
random = new Random(Environment.TickCount);
property.SetValue(bizObject, random.Next(1, 9999), null);
break;
case "System.Decimal":
random = new Random(Environment.TickCount);
property.SetValue(bizObject, Convert.ToDecimal(random.Next(1, 9999)), null);
break;
case "System.DateTime":
Random dayRandom = new Random(Environment.TickCount);
Random monthRandom = new Random(Environment.TickCount);
property.SetValue(bizObject, DateTime.Parse(monthRandom.Next(1, 12) + "/" +
dayRandom.Next(1, 28) + "/" +
DateTime.Now.Year), null);
break;
case "System.Boolean":
random = new Random(Environment.TickCount);
property.SetValue(bizObject, Convert.ToBoolean(random.Next(0, 1)), null);
break;
default:
if(property.PropertyType.BaseType != null && property.PropertyType.BaseType.ToString() == "System.Enum") {
Array values = Enum.GetValues(property.PropertyType);
random = new Random(Environment.TickCount);
property.SetValue(bizObject, values.GetValue(random.Next(values.Length)), null);
}
break;
}
}
}


public abstract class BusinessObject {
public BusinessObject() {}
abstract public int ID { get; set; }
abstract public void Load(string key);
abstract public bool Save();
abstract public bool Delete();
}

protected abstract Type BusinessObjectType { get; }

/// <summary>
/// Test that the business object can be saved.
/// </summary>
[Test]
public virtual void CanBeSaved() {
Save(BusinessObjectType);
}

/// <summary>
/// Test if the BusinessObject can be deleted.
/// </summary>
[Test]
public virtual void CanBeDeleted() {
BusinessObject bizObject = Save(BusinessObjectType);
Assert.IsTrue(bizObject .Delete(), "The object could not be deleted.");
}


/// <summary>
/// Helper method for saving and business object.
/// </summary>
/// <param name="bizObjectType">The type of business object to save.</param>
protected BusinessObject Save(Type bizObjectType) {
BusinessObject bizObject = (BusinessObject) Activator.CreateInstance(BusinessObjectType);
LoadProperties(bizObject);
SaveAsIs(bizObject);
return bizObject;
}


/// <summary>
/// Helper method for saving and business object in its current state.
/// </summary>
/// <param name="bizObject">The <c>BusinessObject</c> to save.</param>
private BusinessObject SaveAsIs(BusinessObject bizObject) {
Assert.IsTrue(bizObject.Save(), "The " + bizObject.GetType().Name + " could not be saved.");
return bizObject;
}


public void CanBeRead() {
Customer savedCustomer = new Customer();
//
set properties
Assert.IsTrue(savedCustomer.Save());
// read the customer
Customer readCustomer = new Customer();
readCustomer.Load(savedCustomer.CustomerID);
// check properties of the loaded object against the saved object
Assert.AreEqual(savedCustomer.ContactName, readCustomer.ContactName, "ContactName properties are not equal.");
Assert.AreEqual(savedCustomer.ContactTitle, readCustomer.ContactTitle, "ContactTitle properties are not equal.");
Assert.AreEqual(savedCustomer.Address, readCustomer.Address, "Address properties are not equal.");
// check remaining properties as necessary
}
[Test]
public virtual void CanBeLoaded() {
BusinessObject savedBizObject = Save(BusinessObjectType);
BusinessObject bizObject = ((BusinessObject) Activator.CreateInstance(BusinessObjectType);
bizObject.Load(savedBizObject.ID);
CompareBusinessObjects(savedBizObject, bizObject);
}

public void CompareBusinessObjects(BusinessObject bizObject, BusinessObject comparedBizObject) {
// Get all properties and loop through them to ensure the values of the saved
// business object are the same as the freshly loaded object.
System.Reflection.PropertyInfo[] properties = BusinessObjectType.GetProperties();
foreach(PropertyInfo property in properties) {
if(property.CanWrite && !_ignoredProperties.Contains(bizObjectType.Name + "-" + property.Name))
Assert.IsTrue(property.GetValue(bizObject, null).Equals(property.GetValue(comparedBizObject, null)), property.Name + " of the loaded
object is not equal to the value of the saved object.");
}
}
}

思路很好,值得学习啊:)
推荐看看~
怎么通过BaseClass来对数据库函数进行自动化单元测试:
其相关文章链接如下:
Steve Eichert
I've recently written a series of posts on the process of automating the unit testing of CRUD operations on business objects.
- Intro: Automating Unit Tests with a Base Class
- Part 1: Automating Unit Tests with a Base Class
- Part 2: Automating Unit Tests with a Base Class
- Part 2 Follow Up: But those tests suck!?!
- Part 3: Automating Unit Testing with a Base Class
In future posts I'll dive into some of the details which I didn't go into such as how to set the allowable values for a property, how to ignore a property when comparing objects, how to set a property value as unique, as well as how to manage relationships among objects. Look for a zip file containing a running example in the next couple of days.
其主要思想就是通过Reflection对自定义Class中的字段自动赋值,再对数据库进行操作:
public void LoadProperties(BusinessObject bizObject) {
System.Reflection.PropertyInfo[] properties = BusinessObjectType.GetProperties();
foreach(PropertyInfo property in properties) {
if(property.CanWrite) {
SetDynamicPropertyValue(bizObject, property);
}
}
}

/// <summary>
/// Set the property of an <c>BusinessObject</c> to a random (dynamic) value.
/// </summary>
/// <param name="bizObject">The <c>BusinessObject</c> to set the value of.</param>
/// <param name="property">The <c>PropertyInfo</c> to set the value of.</param>
private void SetDynamicPropertyValue(BusinessObject bizObject, PropertyInfo property) {
Random random;
string propertyKey = bizObject.GetType().Name + "-" + property.Name;
if(_allowableValues[propertyKey] != null) {
object[] values = (object[]) _allowableValues[propertyKey];
random = new Random(Environment.TickCount);
property.SetValue(bizObject, values.GetValue(random.Next(values.Length)), null);
} else {
ArrayList usedValues = (ArrayList)_uniqueValues[propertyKey];
switch(property.PropertyType.ToString()) {
case "System.String":
property.SetValue(bizObject, GetRandomString(usedValues), null);
break;}
case "System.Int32":
case "System.Double":
random = new Random(Environment.TickCount);
property.SetValue(bizObject, random.Next(1, 9999), null);
break;
case "System.Decimal":
random = new Random(Environment.TickCount);
property.SetValue(bizObject, Convert.ToDecimal(random.Next(1, 9999)), null);
break;
case "System.DateTime":
Random dayRandom = new Random(Environment.TickCount);
Random monthRandom = new Random(Environment.TickCount);
property.SetValue(bizObject, DateTime.Parse(monthRandom.Next(1, 12) + "/" +
dayRandom.Next(1, 28) + "/" +
DateTime.Now.Year), null);
break;
case "System.Boolean":
random = new Random(Environment.TickCount);
property.SetValue(bizObject, Convert.ToBoolean(random.Next(0, 1)), null);
break;
default:
if(property.PropertyType.BaseType != null && property.PropertyType.BaseType.ToString() == "System.Enum") {
Array values = Enum.GetValues(property.PropertyType);
random = new Random(Environment.TickCount);
property.SetValue(bizObject, values.GetValue(random.Next(values.Length)), null);
}
break;
}
}
}


以下是调用代码:
public abstract class BusinessObject {
public BusinessObject() {}
abstract public int ID { get; set; }
abstract public void Load(string key);
abstract public bool Save();
abstract public bool Delete();
}

protected abstract Type BusinessObjectType { get; } 
/// <summary>
/// Test that the business object can be saved.
/// </summary>
[Test]
public virtual void CanBeSaved() {
Save(BusinessObjectType);
}
/// <summary>
/// Test if the BusinessObject can be deleted.
/// </summary>
[Test]
public virtual void CanBeDeleted() {
BusinessObject bizObject = Save(BusinessObjectType);
Assert.IsTrue(bizObject .Delete(), "The object could not be deleted.");
}

/// <summary>
/// Helper method for saving and business object.
/// </summary>
/// <param name="bizObjectType">The type of business object to save.</param>
protected BusinessObject Save(Type bizObjectType) {
BusinessObject bizObject = (BusinessObject) Activator.CreateInstance(BusinessObjectType);
LoadProperties(bizObject);
SaveAsIs(bizObject);
return bizObject;
}

/// <summary>
/// Helper method for saving and business object in its current state.
/// </summary>
/// <param name="bizObject">The <c>BusinessObject</c> to save.</param>
private BusinessObject SaveAsIs(BusinessObject bizObject) {
Assert.IsTrue(bizObject.Save(), "The " + bizObject.GetType().Name + " could not be saved.");
return bizObject;
}

public void CanBeRead() {
Customer savedCustomer = new Customer();
//
set properties
Assert.IsTrue(savedCustomer.Save());
// read the customer
Customer readCustomer = new Customer();
readCustomer.Load(savedCustomer.CustomerID);
// check properties of the loaded object against the saved object
Assert.AreEqual(savedCustomer.ContactName, readCustomer.ContactName, "ContactName properties are not equal.");
Assert.AreEqual(savedCustomer.ContactTitle, readCustomer.ContactTitle, "ContactTitle properties are not equal.");
Assert.AreEqual(savedCustomer.Address, readCustomer.Address, "Address properties are not equal.");
// check remaining properties as necessary
}
[Test]
public virtual void CanBeLoaded() {
BusinessObject savedBizObject = Save(BusinessObjectType);
BusinessObject bizObject = ((BusinessObject) Activator.CreateInstance(BusinessObjectType);
bizObject.Load(savedBizObject.ID);
CompareBusinessObjects(savedBizObject, bizObject);
}
public void CompareBusinessObjects(BusinessObject bizObject, BusinessObject comparedBizObject) {
// Get all properties and loop through them to ensure the values of the saved
// business object are the same as the freshly loaded object.
System.Reflection.PropertyInfo[] properties = BusinessObjectType.GetProperties();
foreach(PropertyInfo property in properties) {
if(property.CanWrite && !_ignoredProperties.Contains(bizObjectType.Name + "-" + property.Name))
Assert.IsTrue(property.GetValue(bizObject, null).Equals(property.GetValue(comparedBizObject, null)), property.Name + " of the loaded
object is not equal to the value of the saved object.");
}
}
}
思路很好,值得学习啊:)
推荐看看~

浙公网安备 33010602011771号