-
Notifications
You must be signed in to change notification settings - Fork 179
Add REQUIRE macro for stopping tests early #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
Merged
pjohnmeyer
merged 12 commits into
unittest-cpp:master
from
pjohnmeyer:paxos1977-pjohnmeyer-RequiredMacro
Feb 24, 2016
Merged
Add REQUIRE macro for stopping tests early #95
pjohnmeyer
merged 12 commits into
unittest-cpp:master
from
pjohnmeyer:paxos1977-pjohnmeyer-RequiredMacro
Feb 24, 2016
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test when an assert fails. The following macro has been added: REQUIRE An example of when these type of checks are useful: std::vector<int> v = foo(); REQUIRE(CHECK_EQUAL(3, v.size())); // test stops here on a fail // so we don't segfault looking at // v[0] below. CHECK_EQUAL(1, v[0]); CHECK_EQUAL(2, v[1]); CHECK_EQUAL(3, v[2]); Multiple checks are supported as follows: REQUIRE({ CHECK_EQUAL(1, 2); CHECK(true); }; In the multiple check scenario, all the checks in the REQUIRE block will be run. After which, if any failures were reported, the TEST case will be stopped. When UNITTEST_NO_EXCEPTIONS is defined, REQUIRE is a noop.
I changed the definition of the REQUIRE macro to use for loops and some comma operator shenanigans to allow things like: REQUIRE { CHECK(...); CHECK_EQUAL(..., ...); } or REQUIRE CHECK(...); I updated the tests and they all passed on my machine. My only concern is that some compilers might complain about the unreachable code in the (throw UnitTest::AssertException(), true) expression.
(1) unreachable code in for loop shenanigans is eliminated. (2) code after a failing REQUIRE check no longer executes. Used a decorating TestReporter to achive this.
Rather than re-using AssertException, it felt more correct to create a new special-purpose exception.
Was able to remove ThrowingTestReporter::SetDecorated and ::GetDecorated by changing RequiredCheckTestReporter to accept its TestResults by reference. This simple change removed several if-checks and some functions.
Also, the ThrowingTestReporter could be used regardless of the REQUIRE feature, if the user wished to change the behavior of all tests to stop on first failed check. |
This needs a few tweaks yet; I'm having some warnings on VS due to the empty calls to |
Visual Studio 2015 complained about calling UT_THROW with zero arguments. Visual Studio 6 complained about calling UT_CATCH with an empty second argument. For these cases, I added UT_RETHROW(ExceptionName). I also added a catch of RequiredCheckException to ExecuteTest to avoid two error messages on each failed REQUIRE check.
pjohnmeyer
added a commit
that referenced
this pull request
Feb 24, 2016
…acro Add REQUIRE macro for stopping tests early
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This feature, primarily authored by @paxos1977 with my collaboration, adds a new feature to UnitTest++. A check or group of checks can be marked as "required" -- triggering early termination of the test. For illustration, let's say we want to assert a pointer is non-null before accessing it.
The second syntax is simpler for a single check here and there; the first is easier if you have a block of code / CHECKs you wish to require.
In this iteration, this feature is only available if exceptions are enabled (#ifndef UNITTEST_NO_EXCEPTIONS).
I still have testing to do on a few compilers, but I wanted to post it for comment while I do that.