📄 x68.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><HTML><HEAD><TITLE>Setting up the money tests </TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINKREL="HOME"TITLE="Check Tutorial"HREF="index.html"><LINKREL="UP"TITLE="Tutorial: Basic unit testing "HREF="c57.html"><LINKREL="PREVIOUS"TITLE="Tutorial: Basic unit testing "HREF="c57.html"><LINKREL="NEXT"TITLE="Test a little, code a little "HREF="x80.html"></HEAD><BODYCLASS="SECTION"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Check Tutorial</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="c57.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 3. Tutorial: Basic unit testing</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="x80.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECTION"><H1CLASS="SECTION"><ANAME="AEN68">3.2. Setting up the money tests</A></H1><P>Since we are creating a library to handle money, we will first create a header money.h, and a file to contain our unit tests, check_money.c (there is a pun there, but no matter...). To manage everything we'll use Autoconf/Automake for this example. (One could do something similar with ordinary makefiles, but in the author's opinion, it is generally easier to use Autoconf/Automake than bare makefiles, and it provides built-in support for running tests). Here is the Makefile.am: </P><PRECLASS="PROGRAMLISTING">TESTS=check_moneynoinst_PROGRAMS=check_moneycheck_money_SOURCES= money.h money.c check_money.ccheck_money_INCLUDES= @CHECK_CFLAGS@check_money_LDADD= @CHECK_LIBS@</PRE><P>Special mumbo jumbo to use in configure.in is: </P><PRECLASS="PROGRAMLISTING">AC_INIT()AM_INIT_AUTOMAKE()AC_PROG_CCAM_PATH_CHECK()AC_OUTPUT(Makefile)</PRE><P>This will ensure that things link up properly with Check by defining the appropriate compiler and linker flags as CHECK_CFLAGS and CHECK_LIBS. It also makes sure that we can only compile in an environment that has Check. The money.h header should only contain the standard #ifndef MONEY_H stuff, money.c should be empty, and check_money.c should only contain an empty main function. Run this with make -k check, after going through the setups to get autoconf and friends working. If all goes well, make should report that our tests passed. No surprise, because there aren't any tests to fail. </P><P>The AM_PATH_CHECK() macro is defined in the file check.m4 which is installed by Check. If you see warnings from automake or aclocal this most certainly means that you forgot to call aclocal or that aclocal can't find check.m4. AM_PATH_CHECK() has some optional parameters that you might find useful: </P><PRECLASS="PROGRAMLISTING">AM_PATH_CHECK([MINIMUM-VERSION,[ACTION-IF-FOUND[,ACTION-IF-NOT-FOUND]]])</PRE><P>One way to run the autoconf/automake tools is shown below in a simpleshell script. The variable CHECK_DIR should point to the directorywhere check.m4 is installed. If you install it in a standard location,e.g. /usr/share/aclocal, then you won't need the variable defined. </P><PRECLASS="PROGRAMLISTING">#!/bin/shif [ -n "$CHECK_DIR" ]; then aclocal -I $CHECK_DIRelse aclocalfiautoconfautoheaderautomake --add-missing</PRE><P>This shell script and the above configure.in were run on a Red Hat 9box with Autoconf version 2.13 and automake (GNU automake) 1.4-p6. Asthe saying goes, "Your mileage may vary..." </P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="c57.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="x80.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Tutorial: Basic unit testing</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="c57.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Test a little, code a little</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -