📄 tutorial.sgml
字号:
<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</programlisting> <para>Let's put these in a separate test case, called “Limits” so that money_suite looks like so: </para> <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;}</programlisting> <para>Now we can rerun our suite, and fix the problem(s). Note that errors in the Core test case will be reported as “Core” and errors in the Limits test case will be reported as “Limits,” giving you additional information about where things broke. </para> </section> <section> <title>No fork mode </title> <para>Check normally forks to create a separate address space. This allows a signal or early exit to be caught and reported, rather than taking down the entire test program, and is normally very useful. However, when you are trying to debug why the segmentation fault or other program error occurred, forking makes it difficult to use debugging tools. To define fork mode for an SRunner object, you can do one of the following: </para> <orderedlist> <listitem> <para>Define the CK_FORK environment variable to equal “no”. </para> </listitem> <listitem> <para>Explicitly define the fork status through the use of the following function: </para> <programlisting>void srunner_set_fork_status(SRunner *sr, enum fork_status fstat);</programlisting> </listitem> </orderedlist> <para>The enum fork_status defines the following values: CK_FORK and CK_NOFORK. </para> <para>The explicit definition overrides the environment variable. </para> </section> <section> <title>Test fixtures </title> <para>We may want multiple tests that all use the same Money. In such cases, rather than setting up and tearing down objects for each unit test, it may be convenient to add some setup that is constant across all the tests in a test case. In the extreme programming approach to unit tests, such setup is called a test fixture. </para> <para>A fixture is created by defining a setup and/or a teardown function, and associating it with a test case. There are two kinds of test fixtures in Check: checked and unchecked fixtures. These are defined as follows: </para> <variablelist> <varlistentry> <term>Checked fixtures</term><listitem><para>are run inside the address space created by the fork to create the unit test. Before each unit test in a test case, the setup function is run, if defined. After each unit test, the teardown function is run, if defined. Because they run inside the forked address space, if checked fixtures signal or otherwise fail, they will be caught and reported by the SRunner. </para> </listitem> </varlistentry> <varlistentry> <term>Unchecked fixtures</term><listitem><para>are run in the same address space as the test program. Therefore they may not signal or exit, but may use the fail functions. The unchecked setup, if defined, is run before the test case is started. The unchecked teardown, if defined, is run after the test case is done. </para> </listitem> </varlistentry> </variablelist> <section> <title>Test fixture examples </title> <para>We create a test fixture in Check as follows: </para> <orderedlist> <listitem> <para>Define the global variables, and functions to setup and teardown the globals. The functions both take void and return void </para> <programlisting>Money *five_dollars; void setup(void) { five_dollars = money_create(5, "USD");}void teardown(void){ money_free(five_dollars);}</programlisting> </listitem> <listitem> <para>Add the setup and teardown functions to the test case with “tcase_add_checked_fixture” (this belongs in the suite setup function “money_suite”: </para> <programlisting>tcase_add_checked_fixture(tc_core, setup, teardown);</programlisting> </listitem> <listitem> <para>We can now rewrite the first test we wrote as follows: </para> <programlisting>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</programlisting> </listitem> </orderedlist> </section> <section> <title>Checked vs Unchecked fixtures </title> <para>Because checked fixtures run once per unit test, they should not be used for expensive setup. Because unchecked fixtures may take down the entire test program, they should only be used if they are known to be safe. </para> <para>Additionally, checked and uncheck fixtures may behave differently in CK_NOFORK mode. Because the fork creates a separate address space, in CK_FORK mode unit tests may abuse the objects created in an unchecked fixture with impunity, without affecting other unit tests in the same test case. However, in CK_NOFORK mode, all tests live in the same address space, and side effects in one test will affect the unchecked fixture for other tests. A checked fixture, however, will generally not be affected by unit test side effects, since the setup is run before each unit test. (There is an exception for side effects to the total environment in which the test program lives: e.g., if the setup function initializes a file that a unit test then changes, the combination of the teardown function and setup fuction must be able to restore the environment for the next unit test). </para> <para>If the setup function in a fixture fails, in either checked or unchecked fixtures, the unit tests for the test case, and the teardown function for the fixture will not be run. A fixture error will be created and reported to the SRunner. </para> </section> </section> <section> <title>Multiple suites run through the same SRunner </title> <para>In a large program, it will be convenient to create multiple suites, each testing a module of the program. While one can create several test programs, each running one Suite, it may be convenient to create one main test program, and use it to run multiple suites. The Check test suite provides an example of how to do this. The main testing program is called check_check, and has a header file that declares suite creation functions for all the module tests: </para> <programlisting>Suite *make_sub_suite(void);Suite *make_sub2_suite(void);Suite *make_master_suite(void);Suite *make_list_suite(void);Suite *make_msg_suite(void);Suite *make_log_suite(void);</programlisting> <para>The function srunner_add_suite is used to add additional suites to an SRunner. Here is the code to setup and run the SRunner in the main function: </para> <programlisting>SRunner *sr;sr = srunner_create(make_master_suite());srunner_add_suite(sr, make_list_suite());srunner_add_suite(sr, make_msg_suite());srunner_add_suite(sr, make_log_suite());</programlisting> </section> <section> <title>Testing signal handling </title> <para>To enable testing of signal handling, there is a function <function>tcase_add_test_raise_signal()</function> which is used instead of <function>tcase_add_test()</function>. This function takes an additional signal argument, specifying a signal that the test expects to receive. If no signal is received this is logged as a failure. If a different signal is received this is logged as an error. </para> <para> The signal handling functionality only works in CK_FORK mode. </para> </section> <section> <title>Test timeouts </title> <para>To be certain that a test won't hang indefinitely, all tests are run with a timeout (default 3 seconds). If the test is not finished within that time, it is killed and logged as an error. The timeout for a specific test case (not an individual test) can be changed with the <function>tcase_set_timeout()</function> function. The default timeout used for all test cases can be changed with the environment variable CK_DEFAULT_TIMEOUT, of course this does not override a specifically set timeout. </para> <para>All timeout arguments are in seconds and a timeout of 0 seconds turns off the timeout functionality. </para> <para> The timeout functionality is only available in CK_FORK mode. </para> </section> <section> <title>Test Logging<anchor id="TestLogging"> </title> <para>Check supports operation to log the results of a test run. To use test logging, use the srunner_set_log function with the name of the log file you wish to create: </para> <programlisting>SRunner *sr;sr = srunner_create(make_s1_suite());srunner_add_suite(sr, make_s2_suite());srunner_set_log(sr, "test.log");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -