Skip to content

Commit 2a92a96

Browse files
committedAug 18, 2015
Now there can be one output file per test instead of a whole testsuite.
1 parent 40bc403 commit 2a92a96

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed
 

‎cu.c

+28-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static int cu_fail_checks = 0;
3535

3636
#define CU_OUT_PREFIX_LENGTH 128
3737
static char cu_out_prefix[CU_OUT_PREFIX_LENGTH+1] = "";
38+
static int cu_out_per_test = 0;
3839

3940

4041
/* globally used file descriptor for reading/writing messages */
@@ -68,6 +69,7 @@ static int test_failed;
6869

6970
static void redirect_out_err(const char *testName);
7071
static void close_out_err(void);
72+
static void redirect_test_out_err(const char *test_suite, const char *test);
7173
static int run_test(const char *t_name, cu_test_func_t t_func);
7274
static void run_test_suite(const char *ts_name, cu_test_suite_t *ts,
7375
int test_id);
@@ -234,6 +236,9 @@ static int run_test(const char *t_name, cu_test_func_t t_func)
234236
char buffer[MSGBUF_LEN];
235237
int len;
236238

239+
if (cu_out_per_test)
240+
redirect_test_out_err(cu_current_test_suite, t_name);
241+
237242
test_failed = 0;
238243

239244
/* set up name of test for later messaging */
@@ -266,7 +271,8 @@ static void run_test_suite(const char *ts_name, cu_test_suite_t *ts,
266271
cu_current_test_suite = ts_name;
267272

268273
/* redirect stdout and stderr */
269-
redirect_out_err(cu_current_test_suite);
274+
if (!cu_out_per_test)
275+
redirect_out_err(cu_current_test_suite);
270276

271277
while (test_id == -1 && ts->name != NULL && ts->func != NULL){
272278
test_suite_failed |= run_test(ts->name, ts->func);
@@ -389,17 +395,35 @@ void cu_set_out_prefix(const char *str)
389395
strncpy(cu_out_prefix, str, CU_OUT_PREFIX_LENGTH);
390396
}
391397

398+
void cu_set_out_per_test(int yes)
399+
{
400+
cu_out_per_test = yes;
401+
}
402+
392403
static void redirect_out_err(const char *test_name)
393404
{
394-
char buf[100];
405+
redirect_test_out_err(test_name, NULL);
406+
}
395407

396-
snprintf(buf, 99, "%stmp.%s.out", cu_out_prefix, test_name);
408+
static void redirect_test_out_err(const char *test_suite, const char *test)
409+
{
410+
char buf[256];
411+
412+
if (test != NULL){
413+
snprintf(buf, 255, "%stmp.%s.%s.out", cu_out_prefix, test_suite, test);
414+
}else{
415+
snprintf(buf, 255, "%stmp.%s.out", cu_out_prefix, test_suite);
416+
}
397417
if (freopen(buf, "w", stdout) == NULL){
398418
perror("Redirecting of stdout failed");
399419
exit(-1);
400420
}
401421

402-
snprintf(buf, 99, "%stmp.%s.err", cu_out_prefix, test_name);
422+
if (test != NULL){
423+
snprintf(buf, 255, "%stmp.%s.%s.err", cu_out_prefix, test_suite, test);
424+
}else{
425+
snprintf(buf, 255, "%stmp.%s.err", cu_out_prefix, test_suite);
426+
}
403427
if (freopen(buf, "w", stderr) == NULL){
404428
perror("Redirecting of stderr failed");
405429
exit(-1);

‎cu.h

+7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ extern "C" {
7070
#define CU_SET_OUT_PREFIX(str) \
7171
cu_set_out_prefix(str)
7272

73+
/**
74+
* Enables (disables) output per test instead of a whole testsuite.
75+
*/
76+
#define CU_SET_OUT_PER_TEST(yes) \
77+
cu_set_out_per_test(yes)
78+
7379
/**
7480
* Assertions
7581
* Assertions with suffix 'M' (e.g. assertTrueM) is variation of macro
@@ -121,6 +127,7 @@ void cu_run(int argc, char *argv[]);
121127
void cu_success_assertion(void);
122128
void cu_fail_assertion(const char *file, int line, const char *msg);
123129
void cu_set_out_prefix(const char *str);
130+
void cu_set_out_per_test(int yes);
124131

125132
/** Timer **/
126133
#ifdef CU_ENABLE_TIMER

‎testsuites/Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ all: test test-segfault
99
touch regressions/testSuiteName.err
1010
touch regressions/testSuiteName2.out
1111
touch regressions/testSuiteName2.err
12-
touch regressions/testSuiteSegfault.out
13-
touch regressions/testSuiteSegfault.err
12+
touch regressions/testSuiteSegfault.testFunction.out
13+
touch regressions/testSuiteSegfault.testFunction.err
14+
touch regressions/testSuiteSegfault.testPrint.out
15+
touch regressions/testSuiteSegfault.testPrint.err
1416
touch regressions/testSuiteTest2.out
1517
touch regressions/testSuiteTest2.err
1618
-./test
@@ -19,6 +21,7 @@ all: test test-segfault
1921
@echo "======= SEGFAULT: ========="
2022
@echo ""
2123
-./test-segfault
24+
-cd regressions && $(PYTHON) ../../cu-check-regressions
2225

2326
test: $(TEST_OBJS)
2427
$(CC) $(CFLAGS) -o $@ $(TEST_OBJS) -L../ -lcu

‎testsuites/test-segfault.c

+8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,15 @@ TEST(testFunction)
1414
assertTrue(0);
1515
}
1616

17+
TEST(testPrint)
18+
{
19+
printf("You should see this in .out :)\n");
20+
fprintf(stderr, "You should see this in .err :)\n");
21+
}
22+
1723
TEST_SUITE(testSuiteSegfault)
1824
{
25+
TEST_ADD(testPrint),
1926
TEST_ADD(testFunction),
2027
TEST_SUITE_CLOSURE
2128
};
@@ -30,6 +37,7 @@ int main(int argc, char *argv[])
3037
{
3138
CU_SET_OUT_PREFIX("regressions/"); /* define prefix for files written
3239
by testsuites */
40+
CU_SET_OUT_PER_TEST(1);
3341
CU_RUN(argc, argv); /* Run testsuites defined by TEST_SUITES macro
3442
in its own process and stdout is redirected to
3543
regressions/tmp.testSuiteName.out

0 commit comments

Comments
 (0)
Please sign in to comment.