...
Note: I'm scheduled to do a ClarionLive Webinar on Friday, December 18, 2009 covering many of the points discussed in this article. Please join me, and if you miss the webinar you can always download the recording.
The Invoice app
The specific code I'm interested in is in the UpdateDetail procedure in the Invoice app. It's a routine that's used to calculate various values for each line item that's added to an invoice. As I'm sure you'll agree, this is critical business logic. And as I showed in Part 3, it's buggy code.
...
So far, so good. But how do you translate this logic into a class that can be tested and reused?
Class design and test driven development
Back when I first started writing object-oriented code, I often found the most difficult part of the job was imagining what the class would look like. Which methods should the class have? What properties? Would I need one class, or multiple classes?
...
But even before I start writing any code I have to back up a bit further, because the one thing still missing from this picture is a unit testing framework.
A unit testing framework
A unit testing framework is a software tool that makes it easy to unit test your code. But what's a unit test?
...
But .NET unit testing tools benefit from some .NET language features, particularly reflection. A .NET unit test framework can inspect an assembly, locate test methods inside test classess, run those test methods and report on the results. That's a lot more difficult to accomplish in Win32.
Unit testing in Clarion Win32
I wasn't aware of any third party unit testing tools for Clarion, so I went ahead and wrote one. Actually this tool is in two parts: an executable application (called CTest) that will search a DLL looking for unit tests to run, and a procedure template you use to create test procedures, in your test DLL, that are compatible with CTest.
Figure 1 shows the CTest application with a test DLL loaded and several tests having been run (this screen shot is from Part 2). As you can see, CTest is just a single window application that lists the available tests, lets you run those tests, and shows you the results.
Figure 1. The CTest test runner
The Test_
prefix is necessary for CTest to identify the procedure as a test procedure (this way you can mix test and non-test procedures in the DLL).
...
Having said that, even after you become proficient in creating unit tests you almost certainly will spend more time up front creating your code than you did writing embed code. It takes time to write good tests. But there's also a huge payback, in fewer bugs, more confidence and better reusability.
Creating a test DLL
I'm using Clarion 7 in the following explanation, mainly because it lets me create a single solution containing both the original application as well as my test DLL.
...
If you're using your own solution, right-click on the solution in the Solution Explorer and choose Add | New Application or Project. Choose a type Application (not DLL) from the dialog (Figure 2).
Figure 2. Adding a test DLL to the solution
In the Application Properties dialog set the Destination Type to DLL (Figure 3).
Figure 3. Creating the test DLL
You don't need the Application Wizard since you'll be creating all your procedures manually.
You do however need to add a global extension to the DLL to enable the unit test support code. On the Global Extensions tab click on Insert, and choose Class Clarion Test | TestSupportIncludes (Figure 4).
Figure 4. Adding the TestSupportIncludes global extension
You're now ready to create your first test procedure, and I'll cover that topic next time.
...