README
1TABLE OF CONTENTS
2 1. HOW TO RUN LIBLWGEOM UNIT TESTS
3 2. HOW TO ADD A SINGLE TEST
4 3. HOW TO ADD AN ENTIRE TEST SUITE
5 4. ABOUT TEST OUTPUT
6 5. HOW TO ASSERT A FAILURE
7
8
91. HOW TO RUN LIBLWGEOM UNIT TESTS
10
11NOTE: We use the CUnit test framework, so you will need to have
12 this installed before you will be able to build and run the
13 unit tests.
14
15If you have already built the rest of the code, then from the
16postgis/liblwgeom/cunit directory, run:
17
18make
19./cu_tester
20
21This will run all the tests. To run just one suite:
22
23./cu_tester <suite name>
24
25To run just one test:
26
27./cu_tester <test name>
28
29To run selected suites or tests (will be run in the order you specify):
30
31./cu_tester <test name> <suite name> <other suite name> <other test name> <etc>
32
33Unit tests for the entire system (including both these unit tests and others
34that require postgresql to be running) can be done by running the following
35command from the top of the directory tree (postgis directory):
36
37make check
38
39
402. HOW TO ADD A SINGLE TEST
41
42To add a test to an existing suite, follow these steps:
43
442.1 Create the test:
45
46Open the cu_<suite name>.c file, and add your
47new test function. Test functions must have the following signature:
48
49static void <test_name>(void)
50
51<test_name> must be unique among all tests. A useful naming convention is:
52
53static void test_<suite>_<specific name>(void)
54
55Although not all existing tests follow that convention.
56
57For information on the various ASSERT macros you can use, see the CUnit
58documentation:
59
60http://cunit.sourceforge.net/doc/writing_tests.html
61
622.2 Add the test to the suite:
63
64At the bottom of the cu_<suite name>.c file, below all the test functions, you
65will find a block that looks like this (this example is from cu_print.c):
66
67/*
68** Used by the test harness to register the tests in this file.
69*/
70void print_suite_setup(void);
71void print_suite_setup(void)
72{
73 CU_pSuite suite = CU_add_suite("Print", init_print_suite, clean_print_suite);
74 PG_ADD_TEST(test_lwprint_default_format);
75 PG_ADD_TEST(test_lwprint_format_orders);
76 PG_ADD_TEST(test_lwprint_optional_format);
77};
78
79Add a new line for your test:
80
81 PG_ADD_TEST(<your test function name>);
82
83The tests will be run in the order they appear in the list.
84CU_TEST_INFO_NULL must always be the last entry.
85
862.3 Add any necessary init / cleanup code.
87
88If your test needs global data created or any other kind of init done
89before it runs, or cleanup done after it runs, add the appropriate code
90to the init_<suite name> or clean_<suite name> functions. If the test
91suite does not seem to have these functions (they are optional), see
92below (3.3) for how to create them.
93
94Save your changes, run make, and run ./cu_tester, and your test
95should be executed.
96
97
98
993. HOW TO ADD AN ENTIRE TEST SUITE
100
101Do the following steps to create a whole new test suite (new .c file).
102
1033.1 Create the file.
104
105Create the new file (remember to add to repository as well).
106The naming convention is cu_<suite name>.c.
107
108Make sure to import:
109
110#include "CUnit/Basic.h"
111#include "cu_tester.h"
112
113Now add the file to Makefile.in. Look for "ADD YOUR NEW TEST FILE HERE".
114Remember that you'll have to re-run "configure" (from the top directory)
115after modifying a .in file.
116
1173.2 Write the tests.
118
119Write the test functions as described in section 2. Then at the bottom
120of the file, construct the array of tests (example taken from cu_print.c):
121
122/*
123** Used by the test harness to register the tests in this file.
124*/
125void print_suite_setup(void);
126void print_suite_setup(void)
127{
128 CU_pSuite suite = CU_add_suite("Print", init_print_suite, clean_print_suite);
129 PG_ADD_TEST(test_lwprint_default_format);
130 PG_ADD_TEST(test_lwprint_format_orders);
131 PG_ADD_TEST(test_lwprint_optional_format);
132};
133
134The naming convention is generally <suite name>_suite_setup.
135
1363.3 Construct the init / clean functions and the suite struct.
137
138Test suites can have initialization and cleanup functions to setup and
139dispose of any common or global data. They must have the following
140signature:
141
142static int <function name>(void)
143
144The naming convention is generally:
145
146static int init_<suite name>(void)
147static int clean_<suite_name>(void)
148
1493.4 Add your suite to cu_tester.c.
150
151Edit cu_tester.c. Search for "ADD YOUR SUITE HERE" and add new lines in
152the appropriate places, using the _suite_setup name you used in the last step.
153
154Now run make (remember to run configure first), then ./cu_tester and your
155new suite should run.
156
157
158
1594. ABOUT TEST OUTPUT
160
161CUnit does not print much about asserts that fail, just the line number
162within the appropriate file. If you need any more detailed output, it
163is up to you to printf it. If you discover that all the test suites
164are full of individual hacks to do this, please consolidate them into
165cu_tester.h / .c, and/or enter a trac issue suggesting an improvement.
166
167
1685. HOW TO ASSERT A FAILURE
169
170Often you may want to assert that lwerror was called, possibly verifying
171that a specific error message was generated. There is now a way to do
172this. The global char array "cu_error_msg" will always contain the most
173recent error from an lwerror call. You can check it in your test function
174(either asserting its length is greater than zero, or looking for a
175specific string). Then call cu_error_msg_reset() to clear it when you're
176done. It is a good idea to call cu_error_msg_reset prior to your test,
177in case a previous test has generated an error that was not cleared.
178
179Example:
180
181cu_error_msg_reset();
182<do something here>
183if (strlen(cu_error_msg) > 0)
184{
185 printf("\nError: <whatever your test was> generated an error: %s\n", cu_error_msg);
186 CU_FAIL();
187 /* be nice and clean it up for the next test. */
188 cu_error_msg_reset();
189}