-
Notifications
You must be signed in to change notification settings - Fork 34
Add support for unittest_setup and unittest_teardown functions #96
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
Conversation
I love this idea. I'm going to give it some more thought though. Off the top of my head, here are the things I'm curious about:
Overall though, great work on this. I really like the way you approached it. |
Piggybacking on the Test class was just in order to get easy access to the sRoot list, which was an existing, ready to use storage. But it was really just an ugly hack, requiring the use of "magic" string constants. Working further with it today it became clear that the correct approach is to create separate classes for setup and teardown, and that these can then hold their own static instance references. This makes the changes required in the Test class the absolute minimum it can to be, one optional call to each of the setup/teardown implementations and that's it. I am very pleased with the result. This also provides the following constraints that will be enforced by the compiler:
For now the functions returns void and failures are not handled specifically. Returning boolean and checking the result is not an unreasonable behaviour (one possible failure scenario could for instance be reading in test data from a file which does not exist any longer), however I absolutely do not want to force on a mandatory If there is demand for error handling, it is possible to add an additional TestSetupChecked class with a run method that returns boolean, add a corresponding unittest_setup_checked macro and enforce that only one of unittest_setup and unittest_setup_checked is present. That way people could choose if they wanted error handling or not. But for now only void is supported. I tried making templates out of the classes but got stuck on declaring the static instance members, and I have no plans on spending more effort on that. |
Thanks for all your time and thought on this. "There can be at most one of each setup/teardown macro" and "You cannot use asserts inside" are exactly what this feature needed.
Very good point. I'll create a separate issue as a placeholder in case there is demand for some exception to be raised. For now, I'll put into the documentation that the setup function is just a "silent" (no test output) helper. This is a big step forward for a test system, thank you again for adding it. |
@@ -152,7 +172,9 @@ class Test | |||
static int run_and_report(int argc, char *argv[]) { | |||
// TODO: pick a reporter based on args | |||
ReporterTAP rep; | |||
if (TestSetup::sInstance) TestSetup::sInstance->run(); |
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.
Whoops, this isn't running before each test...
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.
Having setup (and teardown) functions makes it possible to set up dependencies that are unrelated to the functionality being tested, but due to the way code is written is still required.
Let's say you have a calculator class that in addition to performing calculations also print its operation on a LCD.
When testing the operations, the test should be focused on that, and bringing in additional setup that just happens to be required is poor test code, e.g.
This belongs in a common setup function for all the tests in the file.
Does this sound reasonable? I will add some documentation later unless you object.