Welcome to SpellCoder Sign in | Join | Help

Default Constructor Test (Automated)

Overview
This article is about Unit Testing, and as you know how it is important to make (Good) unit testing for your assemblies, and also how most of us mess some basic tests like the default constructor test,
What is the default constructor test?
If you wrote a simple class like this
class DummyClass
{
public DummyClass()
{

}
private int _Age;

public int Age
{
get { return _Age; }
set { _Age = value; }
}

private string _name;

public string Name
{
get { return _name; }
set { _name = value; }
}
private float _length;

public float Length
{
get { return _length; }
set { _length = value; }
}

private Random _random;

public Random RandomID
{
get { return _random; }
set { _random = value; }
}
}
and you want to expose a public constructor that takes to initialization parameters, so it is expected that when the target developer uses this class that after calling the default constructor every public property will have a reasonable default value (Not Null), and this is the whole idea of default constructor test, you are just making sure of this concept, but actually the problem that most of us just mess this type of tests that it is very boring to write down this test for every class you write..

How We Can Solve This
We can add more automation to this type of testing, because we can use some .net features like Reflection and Generics to make it possible to write a generic code that
  1. Explores (reflects) the specified type
  2. Instanciates an object from it using the default constructor
  3. Calls every public property and checks for null values
  4. Returns the result...

So, I decided to write a simple proof of concept code that does that last four jobs.

ConstructorTester Class

public class ConstructorTester<T> where T:class, new()
{
T _currentObject;
Type _type;
public ConstructorTester()
{
_currentObject = new T();
_type = typeof(T);
}

private PropertyInfo[] GetPublicProperties()
{
return _type.GetProperties(BindingFlags.Public | BindingFlags.DeclaredOnly | BindingFlags.Instance);
}

public Dictionary<string, bool> Test()
{
Dictionary<string, bool> result = new Dictionary<string, bool>();
PropertyInfo[] props = GetPublicProperties();
foreach (PropertyInfo info in props)
{
//Skips the Indexers (for now only) if (info.GetIndexParameters().Length > 0)
{
result.Add(info.Name, true);
continue;
}
//Skips primitive types if (!info.PropertyType.IsClass || info.DeclaringType.IsEnum)
{
result.Add(info.Name, true);
continue;
}
//Checks for the string if (info.PropertyType == typeof(string))
{
string strVal = info.GetValue(_currentObject, null) as string;
result.Add(info.Name, String.IsNullOrEmpty(strVal));
continue;
}
//Other types object value = info.GetValue(_currentObject, null);
result.Add(info.Name, value != null);
}
return result;
}
}
So as you can see it is very simple and straight forward reflection code, first makes sure that the provided type has an empty (no parameters) constructor and also make sure that the provided type is a class type using the Generic declaration
public class ConstructorTester<T> where T:class, new()
and this will give a compiler error if the specified type is not applicable,
then inside the class code we instantiate an object from the specified type.
Now all you need is to call the Test() method and it will

   1. Reflect the type and gets all the public properties
   2. Skips
         1. (indexers just for this version)
         2. Properties with primitive types because it already has a default value
         3. Enums
   3. Calls the get property and checks wheather the returned value is null or not
   4. Returns the result in a dictionary that contains the Property name and a boolean that represents the test status...

So to test the DummyClass we will just write this
ConstructorTester<DummyClass> tester = new ConstructorTester<DummyClass>();
Dictionary<string, bool> result = tester.Test();

Future Improvement
This code is not a final version, it is just a proof of concept that can be improved to include more code scenarios and common practices...
The code is attached with this blog post so you can download it and improve it...
Regards
Published Monday, May 08, 2006 3:46 AM by Mohammed Hossam
Filed Under:
Attachment(s): AutoTester.zip

Comments

No Comments

Anonymous comments are disabled