Permalink
Browse files

Now there can be one output file per test instead of a whole testsuite.

  • Loading branch information...
danfis committed Aug 18, 2015
1 parent 40bc403 commit 2a92a96b318dadc79231dcebbcf2f0af14558419
Showing with 48 additions and 6 deletions.
  1. +28 −4 cu.c
  2. +7 −0 cu.h
  3. +5 −2 testsuites/Makefile
  4. +8 −0 testsuites/test-segfault.c
View
32 cu.c
@@ -35,6 +35,7 @@ static int cu_fail_checks = 0;
#define CU_OUT_PREFIX_LENGTH 128
static char cu_out_prefix[CU_OUT_PREFIX_LENGTH+1] = "";
+static int cu_out_per_test = 0;
/* globally used file descriptor for reading/writing messages */
@@ -68,6 +69,7 @@ static int test_failed;
static void redirect_out_err(const char *testName);
static void close_out_err(void);
+static void redirect_test_out_err(const char *test_suite, const char *test);
static int run_test(const char *t_name, cu_test_func_t t_func);
static void run_test_suite(const char *ts_name, cu_test_suite_t *ts,
int test_id);
@@ -234,6 +236,9 @@ static int run_test(const char *t_name, cu_test_func_t t_func)
char buffer[MSGBUF_LEN];
int len;
+ if (cu_out_per_test)
+ redirect_test_out_err(cu_current_test_suite, t_name);
+
test_failed = 0;
/* 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,
cu_current_test_suite = ts_name;
/* redirect stdout and stderr */
- redirect_out_err(cu_current_test_suite);
+ if (!cu_out_per_test)
+ redirect_out_err(cu_current_test_suite);
while (test_id == -1 && ts->name != NULL && ts->func != NULL){
test_suite_failed |= run_test(ts->name, ts->func);
@@ -389,17 +395,35 @@ void cu_set_out_prefix(const char *str)
strncpy(cu_out_prefix, str, CU_OUT_PREFIX_LENGTH);
}
+void cu_set_out_per_test(int yes)
+{
+ cu_out_per_test = yes;
+}
+
static void redirect_out_err(const char *test_name)
{
- char buf[100];
+ redirect_test_out_err(test_name, NULL);
+}
- snprintf(buf, 99, "%stmp.%s.out", cu_out_prefix, test_name);
+static void redirect_test_out_err(const char *test_suite, const char *test)
+{
+ char buf[256];
+
+ if (test != NULL){
+ snprintf(buf, 255, "%stmp.%s.%s.out", cu_out_prefix, test_suite, test);
+ }else{
+ snprintf(buf, 255, "%stmp.%s.out", cu_out_prefix, test_suite);
+ }
if (freopen(buf, "w", stdout) == NULL){
perror("Redirecting of stdout failed");
exit(-1);
}
- snprintf(buf, 99, "%stmp.%s.err", cu_out_prefix, test_name);
+ if (test != NULL){
+ snprintf(buf, 255, "%stmp.%s.%s.err", cu_out_prefix, test_suite, test);
+ }else{
+ snprintf(buf, 255, "%stmp.%s.err", cu_out_prefix, test_suite);
+ }
if (freopen(buf, "w", stderr) == NULL){
perror("Redirecting of stderr failed");
exit(-1);
View
7 cu.h
@@ -70,6 +70,12 @@ extern "C" {
#define CU_SET_OUT_PREFIX(str) \
cu_set_out_prefix(str)
+/**
+ * Enables (disables) output per test instead of a whole testsuite.
+ */
+#define CU_SET_OUT_PER_TEST(yes) \
+ cu_set_out_per_test(yes)
+
/**
* Assertions
* Assertions with suffix 'M' (e.g. assertTrueM) is variation of macro
@@ -121,6 +127,7 @@ void cu_run(int argc, char *argv[]);
void cu_success_assertion(void);
void cu_fail_assertion(const char *file, int line, const char *msg);
void cu_set_out_prefix(const char *str);
+void cu_set_out_per_test(int yes);
/** Timer **/
#ifdef CU_ENABLE_TIMER
View
@@ -9,8 +9,10 @@ all: test test-segfault
touch regressions/testSuiteName.err
touch regressions/testSuiteName2.out
touch regressions/testSuiteName2.err
- touch regressions/testSuiteSegfault.out
- touch regressions/testSuiteSegfault.err
+ touch regressions/testSuiteSegfault.testFunction.out
+ touch regressions/testSuiteSegfault.testFunction.err
+ touch regressions/testSuiteSegfault.testPrint.out
+ touch regressions/testSuiteSegfault.testPrint.err
touch regressions/testSuiteTest2.out
touch regressions/testSuiteTest2.err
-./test
@@ -19,6 +21,7 @@ all: test test-segfault
@echo "======= SEGFAULT: ========="
@echo ""
-./test-segfault
+ -cd regressions && $(PYTHON) ../../cu-check-regressions
test: $(TEST_OBJS)
$(CC) $(CFLAGS) -o $@ $(TEST_OBJS) -L../ -lcu
@@ -14,8 +14,15 @@ TEST(testFunction)
assertTrue(0);
}
+TEST(testPrint)
+{
+ printf("You should see this in .out :)\n");
+ fprintf(stderr, "You should see this in .err :)\n");
+}
+
TEST_SUITE(testSuiteSegfault)
{
+ TEST_ADD(testPrint),
TEST_ADD(testFunction),
TEST_SUITE_CLOSURE
};
@@ -30,6 +37,7 @@ int main(int argc, char *argv[])
{
CU_SET_OUT_PREFIX("regressions/"); /* define prefix for files written
by testsuites */
+ CU_SET_OUT_PER_TEST(1);
CU_RUN(argc, argv); /* Run testsuites defined by TEST_SUITES macro
in its own process and stdout is redirected to
regressions/tmp.testSuiteName.out

0 comments on commit 2a92a96

Please sign in to comment.