Menu

Tree [48a2e5] master /
 History

HTTPS access


File Date Author Commit
 example 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 test 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 .Makefile.swp 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 .cdtproject 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 .project 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 .stubbyc.c.swp 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 List.h 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 Makefile 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 README 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 cmocker.c 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 cmocker.h 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 cmocker.sh 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 lexer.l 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 log.h 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 parser.tab.c 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 parser.tab.h 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 parser.y 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 preset.c 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 preset.h 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit
 test.c 2020-01-31 Erwin Nijmeijer Erwin Nijmeijer [48a2e5] Initial commit

Read Me

/*  Copyright (C) 2020 Erwin Nijmeijer.  All rights reserved.
 * 
 *   This source code is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Public License as 
 *   published by the Free Software Foundation
 *   The code 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 Public License for more details.
 * 
 *   You should have received a copy of the GNU 
 *   Public License; if not, write to the Free
 *   Software Foundation, Inc., 59 Temple Place,
 *   Suite 330, Boston, MA
 *   02111-1307 USA.  
 *
 */


cmocker is a testharness generator for C.
It is a flexible, quick en easy to use tool allowing developers to test software modules throughly on unit level testing.
cmocker allows you to both execute all the relevant code and also mock pieces to allow you to reach a required level of code coverage during testing. This makes it possible to make use of the actual source code and change the control flow by specifying presets to be used at a certain moment.



STEPS FROM HERE:
1 - build de mock generator and libraries for your OS using "make"
2 - go to the "test" directory and run "make" again
3 - examine the example source code named "driver.c" that is a test driver using modules A and B (which are used and mocked by this example).
4 - run the test "./driver" and examine the logging output on the screen explaining the behaviour of the test harness and the influence of the mocks and presets.

IF YOU WANT TO SETUP YOUR TESTS FOR YOU OWN CODE:
1 - generate a test driver for the module you want to test using the -test option and the header file:
	cmocker.sh -test mymodule.h > test_mymodule.c
2 - generate the mocks of the modules that are used by your module under test (you'll need the header files of these modules)
	./cmocker.sh -mock A.h > A_mocked.c
	./cmocker.sh -mock B.h > B_mocked.c
3 - Build your software and include the mock sources.

THE EASY WAY BY EXAMPLE:
With some assumptions, you can easily get this to work and generate a Makefile to build the required sources and binaries.
Assumptions:
	- The module you want to test has a header file specifying the external interface.
	- The modules you want to mock have header files speciying the external interface.
	- The header files have the same filename as the compilation units (modules) except from their extension
	  (e.g. module A.c has header file A.h)
EXAMPLE:
	STEP 1
	set the next environment variables:
	export PATH=$PATH:<path where cmocker binary and cmocker.sh are located>
	export CMOCKER_INCPATH=<path where cmocker.h is located>
	export CMOCKER_LIBPATH=<path where libcmocker.a is located>

	go to the "example directory"
	generate drivers and mocks and a Makefikle with the next command:
	cmocker.sh -test mymodule.h -mock A.h -mock B.h -make

	STEP 2	
	edit the test_myfunction.c test driver
	uncommend the line declaring variable "dummy" and give it an initial value of -9
	edit the first line that presets the function "foo" and specify "afoo2" instead (which is actually a function in module A.c )
	call myfunction(); four times. 
	The fourth time, the function "afoo2" will return a value of -9

	The relevant lines in the source code test_myfunction.c would now look something like:
	int dummy =-9; 
	/* you can preset the return value of the stubbed function */
	/* The next preset value is returned the fourth time "foo" is called */
	cmocker->preset("afoo2", &dummy, CMOCKER_AFTER, 3 ); 
	myfunction();
	myfunction();
	myfunction();
	myfunction();

	STEP 4
	run Make
	
	STEP 5
	start the test harness :
		./test_myfunction 

	evaluate the results