1/23/08

Self-testing class

I am not sure how popular this idea is. Wikipedia does not have much contents in this topic.

Self-testing code - Wikipedia, the free encyclopedia

I have always been thinking about effective ways to write classes that can be tested easily. I've tried NUnit, which works just great. One drawback was that it is not easy to test protected methods.

I've also tried Visual Studio Team System's unit test feature, which also works great. It comes with IDE integration so generating test scheketon is a matter of several mouse clicks. It also supports generating "accessors" that would use Reflection to invoke protected members. However, it is still somehow not of my liking. Besides, I use Visual Studio Developer edition for my personal projects and unit test feature isn't just available to me.

Then, I started tying out "self-testing code".

I am still experimenting but it seems to be effective. Here is how you would do it.


public class Hello
{
static Hello()
{
#if DEBUG
// add code to test features of Hello class
#endif
}

public void Foo() { }
public void Bar() { }
}



Whenever this class is used, the static constructor runs self-testing code, provided that it was compiled as Debug build.

The condition can be tweaked to use configuration settings. For example, app.config's appSettings can have a key-value pair: TestHello=true

That way, we can control the self-test execution independent of build target.

My projects are small, done by few people (my game is worked on by just me and my friend - and another new project is by four of us). Because of the size of the projects, there is not dedicated QA resource - not that I am pushing the responsibility of testing entirely to QA, but my point is that importance of uint testing increases ever more.

For such small development environment, self-testing strategy might be OK.

3 comments:

Anonymous said...

Good post.

Just Friends said...

This topic has fascinated me for quite a while. locksmith in queens I have just began studying it on the Online and discovered your publish to be useful. Thanks

Find Doctor List said...

I have just began studying it on the Online and discovered your publish to be useful. Thanks