Programming Testing: Seams
From wikinotes
Seams are places where you can alter the behaviour of a program when it is under test.
The available seams depend on the programming language in use.
When designing a program for testing, you should provide seams to allow it to be tested.
PreProcessor Seams
TODO:
untested
In C/C++ and other languages with macros,
you can use an#ifdef
to inject or replace test methods.Example
In the real file, we includetestmacros.h
,
which in our tests will define functions locally.// main.c #include <stdio.h> #include "libusers.h" #include testmacros.h // <-- when testing, define 'create_db_user' locally int main(int argc, char *argv[]) { int id = 100 create_db_user("foo@example.com", "foo") }In our macro, we define a function that fakes creating a user in the database.
// testmacros.h #ifdef TESTING struct User { int id; char *email; char *name; } User users[5] = {}; #define create_db_user(email, name) \ { \ struct User user; \ user.id = 123; \ user.email = email; \ user.name = name; \ users[0] = user; \ } #endif