Programming Testing: Seams: Difference between revisions
From wikinotes
Line 18: | Line 18: | ||
#include <stdio.h> | #include <stdio.h> | ||
#include "libusers.h" | #include "libusers.h" | ||
#include testmacros.h // <-- when testing, define 'create_db_user' locally | #include "testmacros.h" // <-- when testing, define 'create_db_user' locally | ||
int main(int argc, char *argv[]) { | int main(int argc, char *argv[]) { | ||
int id = 100; | int id = 100; | ||
create_db_user("foo@example.com", "foo"); | create_db_user("foo@example.com", "foo"); // <-- this will use our macro defined func | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 14:17, 23 July 2022
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.In the real file, we include
testmacros.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"); // <-- this will use our macro defined func }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
Linker/Import Seams
You can alter the path code is sourced from to substitute in entirely new files.
CLASSPATH=test/foo:${CLASSPATH} # java GOPATH=test/foo:${GOPATH} # go PYTHONPATH=test/foo:${PYTHONPATH} # python