Skip to content
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

Merged
merged 1 commit into from
May 4, 2020

Conversation

flatcap
Copy link
Contributor

@flatcap flatcap commented May 4, 2020

  • 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.

#ifdef TEST_INIT { printf("before %s\n", test_case); }
#ifdef TEST_FINI { printf("after %s\n", test_case); }

#include "acutest.h"

Closes: #46


I've implemented the functions as you suggested and they work nicely.
A few notes:

  • The test_init() is inside the try block.
    I moved the test_begin_test_line_() call after so that the test... [OK] line won't get split up.

  • The test_fini() is also inside the try 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.

{
#ifdef TEST_GLOBAL_INIT
TEST_GLOBAL_INIT
#endif
Copy link
Owner

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.

static void
test_global_init(const char *test_name)
{
#ifdef TEST_GLOBAL_INIT
Copy link
Owner

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().)

@@ -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)
Copy link
Owner

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.


/* This is called after each test */
static void
test_global_fini(const char *test_name)
Copy link
Owner

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"
```
@mity mity merged commit cde0c8e into mity:master May 4, 2020
@mity
Copy link
Owner

mity commented May 4, 2020

Merged, thanks.

@flatcap flatcap deleted the init-fini branch May 8, 2022 03:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Q: constructor/destructor functions?
2 participants