c - Better way to discard output from function tested by glib test -


if test functions glib's testharness, face ugly fact, output of functions i'm testign mixed output of glib's functions. code:

#include <stdlib.h> #include <glib.h> #include <stdio.h> #include <fcntl.h> #include <unistd.h>  void to_test(void) {   printf("this function being tested"); }  void test_to_test(void) {   to_test(); }  int main(int argc, char *argv[]) {   g_test_init(&argc, &argv, null);    g_test_add_func("/test", test_to_test);    return g_test_run(); } 

generates:

/test: function being testedok 

the solution found redirecting filedescriptors of standardout/-err /dev/null time function called , resetting them afterwards, like:

void test_to_test(void) {   int backup, new;   new = open("/dev/null", o_wronly);   backup = dup(stdout_fileno);   dup2(new, stdout_fileno);   to_test();   fflush(stdout);   close(new);   dup2(backup, stdout_fileno); } 

the output looks intended:

/test: ok 

unfortunately approach 1.) ugly , 2.) posix specific. question is: there other way this, code portable , @ same time aestetically appealing?

thanks in advance!

yours in neverending, beautiful, transcendetal love floxo

this possible using freopen and/or fdopen. there isn't cross platform way of doing unfortunately, luckily windows has fdopen equivalent can use (is there windows equivalent fdopen handles?).

note work if using stdio. won't work if reason writing directly fd

as longer term recommendation, why not use fprintf instead? , maintain file* field in structure can directed custom output or wherever.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -