c163.html

来自「c unit test framework」· HTML 代码 · 共 187 行

HTML
187
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><HTML><HEAD><TITLE>Advanced Features  </TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINKREL="HOME"TITLE="Check Tutorial"HREF="index.html"><LINKREL="PREVIOUS"TITLE="SRunner output   "HREF="x115.html"><LINKREL="NEXT"TITLE="No fork mode   "HREF="x172.html"></HEAD><BODYCLASS="CHAPTER"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="x115.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="x172.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="AEN163"></A>Chapter 4. Advanced Features  </H1><DIVCLASS="SECTION"><H1CLASS="SECTION"><ANAME="AEN165">4.1. Running multiple cases</A></H1><P>What happens if we pass -1 as the amount in money_create? What should happen? Let's write a unit test. Since we are testing limits, we should also test what happens when we create money with amount 0:   </P><PRECLASS="PROGRAMLISTING">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_TESTSTART_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</PRE><P>Let's put these in a separate test case, called &ldquo;Limits&rdquo; so that money_suite looks like so:   </P><PRECLASS="PROGRAMLISTING">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_test(tc_limits, test_neg_create);  tcase_add_test(tc_limits, test_zero_create);  return s;}</PRE><P>Now we can rerun our suite, and fix the problem(s). Note that errors in the Core test case will be reported as &ldquo;Core&rdquo; and errors in the Limits test case will be reported as &ldquo;Limits,&rdquo; giving you additional information about where things broke.   </P></DIV></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="x115.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="x172.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">SRunner output</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top">&nbsp;</TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">No fork mode</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?