Skip to content

lunarum/SCUnit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SCUnit

Simple C Unittest framework

There are a lot of unittest frameworks, but I wanted a very simple one for my own projects (written in C). I've looked at a couple of other frameworks, most notably the C Unit Testing Framework (see http://cunit.sourceforge.net/index.html written by Anil Kumar, John Pye and Martin Gerhardy). But I didn't find the right (simple!) one for me, so decided to write my own :).

The SCUnit framework is build around two "classes":

  • SCU_TestSuite
  • SCU_TestCase
A unit test comprises of one or more SCU_TestSuite structs which are composed of one or more SCU_TestCase structs. You create a SCU_TestSuite with the SCU_TestSuite_create() function, which takes a name, a StartUp function and a TearDown functions as argument. Then you add TestCases with the function SCU_TestCase_create(). This function is similar to de TestSuite create function in that it has the same argument and then some; a reference to the Suite tis new TestCase is added and ofcourse a TestCase function.

Note that TestSuites and TestCases are C structs and are linked to the next TestSuite / TestCase in a circular list. Each new item is added at the end, so the creation order is preserved. All TestSuites are in one list, with a global pointer SCU_lastTestSuite referencing the last in the list (therefore looping over all items is starting at the next and then check if the current pointer matches the last to decide if all items are processed). All TestCases are ofcourse part of a circular list per TestSuite, which also references the last TestCase.

To actually run then unittest session is nothing more than calling SCU_executeAllTests(), although you could run seperate TestSuites or even separate TestCases bij calling their respective execute functions. One thing to note about running is the global runmode SCU_runMode;

  • SCU_RUN_MODE_SILENT (no output),
  • SCU_RUN_MODE_NORMAL (default, only result line per Suite) and
  • SCU_RUN_MODE_VERBOSE (print everything of interrest).

The result of every test is either a SCU_SUCCESS if everything went allright and all tests where succesfull, a SCU_FAILED if one or more tests failed or a negative error number, being some kind of SCU_FATAL_ERROR()

Test case functions must use the SCU_ASSERT(EQUATION, VALUE) macro for assertions. First parameter (EQUATIONS) is the assertion which must hold true. The second parameter (VALUE) is the value (variable) which is asserted. All values are cast to unsigned long and printed (if run mode is SCU_RUN_MODE_VERBOSE)


Build environment

I use CodeLite (see https://codelite.org/) as the development environment. After installation, use CodeLite to download and install the GNU C compiler suite (gcc - Mingw). After installing open the .workspace file in the root of the sources. SCUnit is the library project, SCUnitTest a simple test example.


Copyright

Copyright 2017 Remco van Maanen


SCUnit is free software:

you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

SCUnit is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. (see file COPYING) If not, see .

About

Simple C Unittest framework

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published