cbUnit - a unit testing framework for CoolBasic

Announce your Work In Process and finalized projects here.
Post Reply
User avatar
Devoted Member
Posts: 877
Joined: Mon Aug 27, 2007 10:18 pm
Location: Pori

cbUnit - a unit testing framework for CoolBasic

Post by Jare »

Hi all :)

This forum hasn't seen much activity in the last years, and there is probably not much people developing programs with CoolBasic anymore. This fact can make it kind of pointless to use time to create new software development tools for CoolBasic. But I find it quite fascinating to sometimes try to develop new ideas with good old CoolBasic and see how it performs - no matter if it will be used or not. cbUnit is one of those ideas that has actually teached me yet again a new way to use CoolBasic.

cbUnit is a unit testing framework that I've written in a little over week. If you don't know what unit testing is, I suggest you to check the before mentioned Wikipedia link, but just in a few words, it's a way to test specific parts of your application - usually functions - via separate test script files. A common practise is to divide tests into functions so that for each function in your application there will be one unit test function that calls your application's function multiple times with different parameters and checks that the result of the function is always as expected. For example, a function should return a specific value when given specific parameters, or a function should write a file with specific content. Unit test functions can then give detailed error messages if the results are incorrect. Incorrect results usually happens when you later change your application's code somehow. Unit test functions are written to separate files and they will not be included in the application when the aplication is built into an executable file, so they will not clutter up the end product.

Here is a short list of how cbUnit works:
  • cbUnit folder is (typically) installed under your CoolBasic directory (so you'll have it in the same folder with CBEditor.exe).
  • You have a folder for your application project in for example: CoolBasic\Projects\MyApplication.
  • Your project folder has a cbUnit.cmd file. When you double click it, cbUnit will start and scan tests folder under MyApplication.
  • Each test_*.cb file will be compiled by cbUnit (using CBCompile.exe which is copied from your CoolBasic\IDE folder) with some unit testing functions attached to it and run.
  • Each test_*() function in your test files will be called automatically.
  • Your test functions will call various assert*() functions provided by the framework and the framework will log if the assertions succeed or not. If an assertion fails, a customisable error message will be written to the log. You can configure via settings if you want to end further processing right after first failure, or continue even if failures happen and collect multiple possible error messages at one go.
  • When all tests are run, cbUnit closes and opens up the log file in your favorite text editor.
  • You can configure cbUnit to use Force Variable Declaration during compiling - and I highly recommend it.
  • If you have a syntax error in your code, you will see CBCompiler's error popup window, but in addition to this, cbUnit writes the error message to the log file too.
Here is an example of what a test file may look like. Note that it's not cbUnit's repository. That link I'll write to the bottom of this post. And that example does not use all the available assert*() functions.

This is not a mature tool, and I won't make any promises about its future development, but I think it's in a quite nice shape and it's a fun tool to play around with :) - at least if you are interested to use time to write useful tests for your projects. This is quite a nice way to avoid filling up your application's codebase with dull debug lines here and there. Well, I can't promise that you will avoid all of it, but maybe some :).

I am still not very experienced with writing unit tests, although I've known the concept for maybe a couple of years. I've always felt that I have introduced myself to unit tests far too late, considering that I have written software as a profession. I guess that building a unit testing framework - even though just a small and simple one - is quite an interesting way to study the field more.

You need to compile cbUnit.exe yourself, but you do it just once with a cmd-file. Test files are not compiled & run with CBEditor (although you can write them with it), but cbUnit.exe will compile them for you, so it's easy to make changes to your test files and just execute one program that handles compiling and running the tests.

Definetly not everything is documented completely, but cbUnit comes with an installation guide. Please let me know if there's anything that's unclear, or if you have any development ideas! You can write them either here or in GitHub issues.

To get it installed, see cbUnit on GitHub
Post Reply