hBifTs

山自高兮水自深!當塵霧消散,唯事實留傳.荣辱不惊, 看庭前花开花落; 去留随意, 望天上云展云舒.

导航

Automating Unit Testing With a Base Class

Posted on 2004-07-07 14:00  hbiftsaa  阅读(959)  评论(0)    收藏  举报
在网上看到了别人写的一个示例:
怎么通过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. 

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(
19999), null);
          
break;
       
case "System.Decimal":
          random 
= new Random(Environment.TickCount);
          property.SetValue(bizObject, Convert.ToDecimal(random.Next(
19999)), null);
          
break;
       
case "System.DateTime":
          Random dayRandom 
= new Random(Environment.TickCount);
          Random monthRandom 
= new Random(Environment.TickCount);
          property.SetValue(bizObject, DateTime.Parse(monthRandom.Next(
112+ "/" + 
                                                                          dayRandom.Next(
128+ "/" + 
                                                                          DateTime.Now.Year), 
null);
          
break;
       
case "System.Boolean":
          random 
= new Random(Environment.TickCount);
          property.SetValue(bizObject, Convert.ToBoolean(random.Next(
01)), 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 getset; }   
   
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.");
     }

   }

}


思路很好,值得学习啊:)
推荐看看~