BYTE.com > Features > 2003
C# Features Useful for Testing
By Dr. Thomas Plum
October 13, 2003
(C# Features Useful for Testing
: Page 1 of 1 )
Nowadays, writing test suites is the main business of my company, Plum Hall. First came C, then C++, and then Java. The structure of the Java test suite is significantly different, because visibility of names should follow class inheritance, whereas the C and C++ suites used the global namespace rather freely. (Test code isn't always intended to follow the same guidelines as product code.)
Writing a test suite for C# was in various ways similar to writing a test suite for Java, but there are several interesting ways that C# provides useful features from its C/C++ heritage—features that permit more straightforward structuring of the testing.
In particular:
1. C# has a preprocessor; Java doesn't. But compared with the "anything-goes" preprocessor of C and C++, the C# preprocessor is very carefully restricted. It supports #define in the code or via command-line option, but requires that all defining be complete by the first line of real code. In other words, the entire file of real code must be compiled under exactly the same set of definitions. And the only thing you can do with those preprocessor definitions is #if-#elif-#else; but that's all that's really needed anyway.
For example, a test suite can skip cases if some language feature is not yet implemented. During the years of evolution of C++ and C99 we had to use "not-yet-implemented" workarounds in hundreds of code contexts:
#if LACKING_FEATURE_X
#define SKIP123A45
#endif
?
#if !SKIP123A45
[positive test for case 123A45]
#else
[print 123A45 skipped]
#endif
In this example, some particular test named "123A45" is being skipped if "feature X" is missing.
The C# preprocessor also has the #error feature; test suites can use this feature to implement the "not-yet-implemented" workaround in negative tests:
#if !SKIP123A46
[negative test for case 123A46]
#else
#error 123A46 skipped
#endif
2. C# has enums, and Java doesn't. But compared with C and C++, the C# enums require fully-qualified names, just like member names of classes.
Page 1 of 1
BYTE.com > Features > 2003
|