User Tools

Site Tools


pcb:unit_tests

Unit Testing Framework

I just figured this out, so, here's how it works, and I'll formalize this page later.

pcb uses the glib testing framework for unit tests: https://developer.gnome.org/glib/stable/glib-Testing.html

Look at main-test.c for examples.

In the actual code, you can wrap the unit test stuff in:

#ifdef PCB_UNIT_TEST
#endif /* PCB_UNIT_TEST */

so that it doesn't bog things down when not testing. Does it really bog things down when not testing??

You need a “<x>_register_tests” function, and then how ever many functions you're going to use to execute your tests. Put prototypes in the header file. Include the header file in main-test.c, and add a call to the “<x>_register_tests” function in main.

In the register function, make a call to g_test_add_func to add whatever testing functions you want to call. It uses a /x/y naming convention, where x is the test module, and y are the individual tests.

You have to add the source file to the list of files to be compiled for testing in Makefile.am. Look for the TEST_SRCS variable, and add your files there, in addition to adding them wherever else you need so that pcb compiles them.

Finally, you can rerun configure (to regen the makefiles), and execute “make check” from the build directory. This will build and execute the tests without necessarily building all of pcb. I'm not sure if this runs the pcb level tests or not.

pcb/unit_tests.txt · Last modified: 2019/01/01 11:21 by cparker