-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add functions that are called before/after every test #48
Conversation
{ | ||
#ifdef TEST_GLOBAL_INIT | ||
TEST_GLOBAL_INIT | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a semicolon before or after the #endif
(maybe with comment). I agree it looks ugly but it is a noop when not needed. However it will help when user defines the macro without one as e.g. in
#define TEST_INIT my_func()
Actually, this might be more natural when defining the macro as a single function call.
Consider people are not used to end their macro definitions with semicolon because then the macro behaves as a normal statement when used. And when people are used to do something they will do the same even in a situation where the rationale does not apply.
include/acutest.h
Outdated
static void | ||
test_global_init(const char *test_name) | ||
{ | ||
#ifdef TEST_GLOBAL_INIT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename it to just TEST_INIT
.
I admit my post could be quite cryptic and I'm sorry if I mislead you. The TEST_GLOBAL_INIT
would be appropriate if we would call it from main()
as originally suggested in the issue, i.e. if it would be the process-wide initialization and not called for every single test.
(And then even for the process one I later came with probably better TEST_PROCESS_INIT()
.)
include/acutest.h
Outdated
@@ -925,6 +925,24 @@ test_error_(const char* fmt, ...) | |||
} | |||
} | |||
|
|||
/* This is called just before each test */ | |||
static void | |||
test_global_init(const char *test_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All our internal function names end with _
to mitigate identifier clash risk with user's code. Should be here too.
include/acutest.h
Outdated
|
||
/* This is called after each test */ | ||
static void | ||
test_global_fini(const char *test_name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All the comments for init apply here too.
- `test_init_()` is called before every test - `test_fini_()` is called after every test The user can define code to be called by defining `TEST_INIT` and/or `TEST_FINI`. ```c #ifdef TEST_INIT { printf("before %s\n", test_case); } #ifdef TEST_FINI { printf("after %s\n", test_case); } #include "acutest.h" ```
Merged, thanks. |
test_init_()
is called before every testtest_fini_()
is called after every testThe user can define code to be called by defining
TEST_INIT
and/orTEST_FINI
.Closes: #46
I've implemented the functions as you suggested and they work nicely.
A few notes:
The
test_init()
is inside thetry
block.I moved the
test_begin_test_line_()
call after so that thetest... [OK]
line won't get split up.The
test_fini()
is also inside thetry
block.This means it won't get called after a crash, probably not a problem :-)
I added the parameter
const char *test_name
.The user functions could have use of it.