Programming Testing: Seams: Difference between revisions
From wikinotes
(Created page with "Seams are places where you can alter the behaviour of a program when it is under test.<br> The available seams depend on the programming language in use.<br> When designing a program for testing, you should provide seams to allow it to be tested. = PreProcessor Seams = <blockquote> {{ TODO | untested }} In C/C++ and other languages with macros,<br> you can use an <code>IFDEF</code> to inject or replace test methods. Something like this. <syntaxhighlight lang="c"> #ifd...") |
|||
Line 12: | Line 12: | ||
Something like this. | Something like this. | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
// 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") | |||
} | |||
</syntaxhighlight> | |||
<syntaxhighlight lang="c"> | |||
// testmacros.h | |||
#ifdef TESTING | #ifdef TESTING | ||
struct User { | struct User { | ||
int id; | int id; | ||
char *email; | |||
char *name; | char *name; | ||
} | } | ||
Line 22: | Line 39: | ||
User users[5] = {}; | User users[5] = {}; | ||
#define | #define create_db_user(email, name) \ | ||
{ \ | { \ | ||
struct User user; \ | struct User user; \ | ||
user.id = | user.id = 123; \ | ||
user.email = email; \ | |||
user.name = name; \ | user.name = name; \ | ||
users[0] = user; \ | users[0] = user; \ |
Revision as of 14:10, 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 anIFDEF
to inject or replace test methods.Something like this.
// 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") }// 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