Last active
June 23, 2021 11:39
-
-
Save tnymlr/29ec50d059af52b22091 to your computer and use it in GitHub Desktop.
Autotools/Automake unit testing (make check) via CUnit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- Autoconf -*- | |
# Process this file with autoconf to produce a configure script. | |
AC_PREREQ([2.69]) | |
AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS]) | |
AC_CONFIG_SRCDIR([src/main.c]) | |
AM_INIT_AUTOMAKE([subdir-objects]) | |
AC_CONFIG_HEADERS([config.h]) | |
# Checks for programs. | |
AC_PROG_CC | |
# Checks for libraries. | |
AC_CHECK_LIB([cunit], [CU_initialize_registry]) | |
# Checks for header files. | |
AC_CHECK_HEADERS([limits.h stddef.h stdint.h]) | |
# Checks for typedefs, structures, and compiler characteristics. | |
AC_TYPE_UINT64_T | |
# Checks for library functions. | |
AC_CONFIG_FILES([Makefile | |
src/Makefile | |
src/hello/Makefile | |
tests/Makefile]) | |
AC_OUTPUT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SUBDIRS=src tests | |
TESTS=tests/hello |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include "hello.h" | |
void hello_say() { | |
printf("Hello, world!\n"); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void hello_say(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ifndef HELLO_MAKEFILE | |
HELLO_MAKEFILE = 1 | |
hello: | |
@${CC} -c -o hello.o src/hello/hello.c ${CFLAGS} | |
endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "hello/hello.h" | |
int main(int argv, char **args) { | |
hello_say(); | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AM_CFLAGS= -std=c99 -Wall | |
bin_PROGRAMS=app | |
app_SOURCES=main.c hello/hello.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <CUnit/CUnit.h> | |
#include <CUnit/Basic.h> | |
#include <stdlib.h> | |
#include <err.h> | |
typedef struct { | |
char * name; | |
void (*func)(void); | |
} Test; | |
#define __PRE_POST_TEST(sig, code) \ | |
sig {\ | |
code\ | |
return 0;\ | |
} | |
#define INIT(code)\ | |
__PRE_POST_TEST(int init_test(void), code) | |
#define CLEANUP(code)\ | |
__PRE_POST_TEST(int cleanup_test(void), code) | |
#define CHECK(name) void test_ ## name(void) | |
#define TEST(name) {"name", test_ ## name} | |
#define RUN(suite, ...) \ | |
void fireball(void) { \ | |
CU_cleanup_registry(); \ | |
exit(CU_get_error()); \ | |
} \ | |
int \ | |
main(void) \ | |
{ \ | |
CU_pSuite tsuite = NULL; \ | |
unsigned int fails; \ | |
if (!(CUE_SUCCESS == CU_initialize_registry())) { \ | |
errx(666, "failed to initialise test registry"); \ | |
return EXIT_FAILURE; \ | |
}\ | |
tsuite = CU_add_suite(suite, init_test, cleanup_test); \ | |
if (NULL == tsuite) \ | |
fireball(); \ | |
do { \ | |
Test arr[] = {__VA_ARGS__}; \ | |
for(int i = 0; i < sizeof(arr)/sizeof(Test); i++) { \ | |
if(NULL == CU_add_test(tsuite, arr[i].name, arr[i].func)) \ | |
fireball(); \ | |
} \ | |
}while(0); \ | |
CU_basic_run_tests(); \ | |
fails = CU_get_number_of_tests_failed(); \ | |
warnx("%u tests failed", fails); \ | |
CU_cleanup_registry(); \ | |
return fails; \ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <hello/hello.h> | |
#include "cunit.h" | |
INIT( | |
printf("Hello, world from init!\n"); | |
) | |
CHECK(hello_say) { | |
CU_ASSERT_EQUAL(1, 1); | |
hello_say(); | |
} | |
CHECK(hello_say1) { | |
CU_ASSERT_EQUAL(1, 1); | |
hello_say(); | |
} | |
CLEANUP( | |
printf("Hello, world from cleanup\n"); | |
) | |
RUN("hello", TEST(hello_say), TEST(hello_say1)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AM_CFLAGS=-I${top_srcdir}/src -std=c99 -Wall | |
AM_LGFLAGS=-L/usr/local/include -lcunit | |
check_PROGRAMS=hello | |
hello_SOURCES=hello.c ${top_srcdir}/src/hello/hello.c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for the structure. It helped a lot :)