Benutzer-Werkzeuge

Webseiten-Werkzeuge


programming:test_framework

Unterschiede

Hier werden die Unterschiede zwischen zwei Versionen angezeigt.

Link zu dieser Vergleichsansicht

programming:test_framework [2013/12/16 13:58] (aktuell)
idefix angelegt
Zeile 1: Zeile 1:
 +
 +====== ​ Test-Framework ​ ======
 +=====  Installation ​ =====
 +To install it type:
 +<​code>​
 +emerge sys-devel/​automake
 +emerge sys-devel/​autoconf
 +emerge dev-libs/​check
 +
 +or
 +apt-get install autoconf automake check
 +</​code>​
 +
 +=====  Setting up with autoconf and automake ​ =====
 +Create a directory for your project and create a file autogen.sh:
 +<​code>​
 +#!/bin/sh
 +if [ -n "​$CHECK_DIR"​ ]; then
 +    aclocal -I $CHECK_DIR
 +else
 +    aclocal
 +fi
 +autoconf
 +autoheader
 +automake --add-missing
 +
 +./configure "​$@"​
 +
 +echo
 +echo "Now type 'make check' to compile."​
 +</​code>​
 +
 +Create the file Makefile.am:​
 +<​code>​
 +INCLUDES=@CHECK_CFLAGS@
 +
 +if HAVE_CHECK
 +TESTS=check_money
 +else
 +TESTS=
 +endif
 +
 +noinst_PROGRAMS=$(TESTS)
 +
 +lib_LIBRARIES= libmoney.a
 +
 +libmoney_a_SOURCES= \
 +    money.h\
 +    money.c
 +
 +check_money_SOURCES= \
 + money.h\
 + money.c\
 + check_money.c
 +
 +check_money_LDADD= @CHECK_LIBS@
 +</​code>​
 +
 +Now create the file configure.in:​
 +<​code>​
 +dnl Process this file with autoconf to produce a configure script.
 +AC_INIT(money.h)
 +AM_INIT_AUTOMAKE(money,​0.2)
 +
 +dnl Checks for programs.
 +AC_PROG_AWK
 +AC_PROG_CC
 +AC_PROG_INSTALL
 +AC_PROG_LN_S
 +
 +dnl This macro is defined in check.m4 and tests if check.h and libcheck.a
 +dnl can be found. It sets CHECK_CFLAGS and CHECK_LIBS accordingly.
 +dnl AM_PATH_CHECK([MINIMUM-VERSION,​ [ACTION-IF-FOUND [, ACTION-IF-NOT-FOUND]]])
 +AM_PATH_CHECK(,​[have_check="​yes"​],​
 + AC_MSG_WARN([Check not found; cannot run unit tests!])
 + [have_check="​no"​])
 +AM_CONDITIONAL(HAVE_CHECK,​ test x"​$have_check"​ = "​xyes"​)
 +
 +dnl Checks for header files.
 +AM_CONFIG_HEADER(config.h)
 +
 +dnl Checks for typedefs, structures, and compiler characteristics.
 +
 +dnl Checks for library functions.
 +
 +AC_OUTPUT(Makefile)
 +</​code>​
 +
 +=====  Create the programm itself ​ =====
 +Create the file money.c:
 +<​code>​
 +#include <​money.h>​
 +</​code>​
 +
 +Create the file money.h:
 +<​code>​
 +#ifndef MONEY_H
 +#define MONEY_H
 +
 +#endif
 +</​code>​
 +
 +Create the file check_money.c:​
 +<​code>​
 +#include <​check.h>​
 +#include <​stdlib.h>​
 +#include "​money.h"​
 +
 +START_TEST (test1)
 +{
 +}
 +END_TEST
 +
 +int main(void)
 +{
 +   ​return 0;
 +}
 +</​code>​
 +
 +=====  Create the testroutines ​ =====
 +Edit check_money.c:​
 +<​code>​
 +#include <​check.h>​
 +#include <​stdlib.h>​
 +#include "​money.h"​
 +
 +Money *five_dollars;​
 +void setup(void)
 +{
 +   ​five_dollars=money_create(5,​ "​USD"​);​
 +}
 +
 +void teardown(void)
 +{
 +   ​money_free(five_dollars);​
 +}
 +
 +START_TEST (test_create)
 +{
 +   ​fail_unless(money_amount(five_dollars)==5, ​
 +          "​Amount not set correctly on creation"​);​
 +   ​fail_unless(strcmp(money_currency(five_dollars),​ "​USD"​) == 0, 
 +          "​Currency not set correctly on creation"​);​
 +}
 +END_TEST
 +
 +START_TEST (test_neg_create)
 +{
 +   Money *m=money_create(-1,​ "​USD"​);​
 +   ​fail_unless(m == NULL,
 +          "NULL should be returned on attempt to create with a negative amount"​);​
 +}
 +END_TEST
 +
 +START_TEST (test_zero_create)
 +{
 +   Money *m=money_create(0,"​USD"​);​
 +   ​fail_unless(money_amount(m)==0,"​Zero is a valid amount of money"​);​
 +}
 +END_TEST
 +
 +Suite *money_suite(void)
 +{
 +   Suite *s=suite_create("​Money"​);​
 +   TCase *tc_core=tcase_create("​Core"​);​
 +   TCase *tc_limits=tcase_create("​Limits"​);​
 +   
 +   
 +   ​suite_add_tcase (s, tc_core);
 +   ​suite_add_tcase (s, tc_limits);
 +   
 +
 +   ​tcase_add_test(tc_core,​ test_create);​
 +   ​tcase_add_checked_fixture(tc_core,​ setup, teardown);
 +   
 +   ​tcase_add_test(tc_limits,​ test_neg_create);​
 +   ​tcase_add_test(tc_limits,​ test_zero_create);​
 +   
 +   ​return s;
 +}
 +
 +
 +int main(void)
 +{
 +   int nf;
 +   Suite *s=money_suite();​
 +   ​SRunner *sr=srunner_create(s);​
 +
 +   ​srunner_set_log(sr,​ "​test.log"​);​
 +   
 +   ​srunner_run_all(sr,​ CK_NORMAL);
 +   ​nf=srunner_ntests_failed(sr);​
 +
 +   ​srunner_free(sr);​
 +   ​return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;​
 +}
 +</​code>​
 +
 +=====  Create the functions ​ =====
 +Edit money.h:
 +<​code>​
 +#ifndef MONEY_H
 +#define MONEY_H
 +
 +typedef struct Money Money;
 +
 +Money *money_create(int amount, char *currency);
 +int money_amount(Money *m);
 +char *money_currency(Money *m);
 +void money_free(Money *m);
 +
 +#endif
 +</​code>​
 +
 +Edit money.c:
 +<​code>​
 +#include <​stdlib.h>​
 +#include <​money.h>​
 +
 +struct Money
 +{
 +      int amount;
 +      char *currency;
 +};
 +
 +Money *money_create(int amount, char *currency) ​
 +
 +   ​if(amount <0)
 +      return NULL;
 +   Money *m = malloc(sizeof(Money));​
 +   if(m == NULL)
 +      return NULL; 
 +   ​m->​amount=amount;​
 +   ​m->​currency=currency;​
 +   ​return m;
 +}
 +int money_amount(Money *m) 
 +
 +  return m->​amount; ​
 +}
 +char *money_currency(Money *m) 
 +
 +  return m->​currency; ​
 +}
 +
 +void money_free(Money *m) 
 +
 +   ​free(m);​
 +}
 +</​code>​
 +
 +=====  Performing the checks ​ =====
 +To prepare the compilation type:
 +<​code>​
 +./​autogen.sh
 +./configure
 +</​code>​
 +
 +To check if everything is fine and compile it type:
 +<​code>​
 +make check
 +</​code>​
  
programming/test_framework.txt · Zuletzt geändert: 2013/12/16 13:58 von idefix