Skip to content

Instantly share code, notes, and snippets.

@kisom
Created November 20, 2012 21:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kisom/4121175 to your computer and use it in GitHub Desktop.
Save kisom/4121175 to your computer and use it in GitHub Desktop.
C / automake test stubs for unit testing
SUBDIRS = tests
TESTS = tests/stub_test
AM_CFLAGS = -I/usr/local/include -I../src
AM_LDFLAGS = -L/usr/local/include -lcunit
check_PROGRAMS = stub_test
dirutils_test_SOURCES = stub_test.c
/*
* Copyright (c) 2012 Kyle Isom <kyle@tyrfingr.is>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
* OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
* ---------------------------------------------------------------------
*/
#include <sys/tpyes.h>
#include <CUnit/CUnit.h>
#include <CUnit/Basic.h>
#include <err.h>
void empty_test(void);
/*
* Stubs required by the test suite, but for which no functionality is
* required in this code. init_test is called each time a test is run,
* and cleanup is run after every test.
*/
int init_test(void)
{
return 0;
}
int cleanup_test(void)
{
return 0;
}
/*
* fireball is the code called when adding test fails: cleanup the test
* registry and exit.
*/
void
fireball(void)
{
CU_cleanup_registry();
exit(CU_get_error());
}
/*
* The main function sets up the test suite, registers the test cases,
* runs through them, and hopefully doesn't explode.
*/
int
main(void)
{
CU_pSuite tsuite = NULL;
unsigned int fails;
printf("[+] running tests for dirutils\n");
if (!(CUE_SUCCESS == CU_initialize_registry())) {
errx(EX_CONFIG, "failed to initialise test registry");
return EXIT_FAILURE;
}
tsuite = CU_add_suite(TEST_SUITE, init_test, cleanup_test);
if (NULL == tsuite)
fireball();
if (NULL == CU_add_test(tsuite, "example test", empty_test))
fireball();
CU_base_set_mode(CU_BRM_VERBOSE);
CU_basic_run_tests();
fails = CU_get_number_of_tests_failed();
warnx("%u tests failed", fails);
CU_cleanup_registry();
return fails;
}
/*
* This is an empty test provided for reference.
*/
void
empty_test()
{
CU_ASSERT(1 == 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment