Refactor handling of stats in suite.Run with the goal of reducing
indented blocks to improve readability.
To achieve this, the SuiteInformation methods now handle being called
with a nil receiver to work as noop. This allows to call them from
suite.Run without nil checks blocks, so with improved readability.
## Summary
Improve readability of suite.Run by moving the running of SetupSuite
outside of the loop iterating over the (test) methods.
This also allows for other simplifications further down in the code.
## Changes
- Move SetupSuite to outside the loop
- Don't run Setup/TeardownSuite if no tests are found (not new
behaviour, but new check)
- Remove variable to keep track of wether SetupSuite was executed or not
## Motivation
This is a subset of the changes I made under PR #1749. It was suggested
by @dolmen to open a separate PR for this part.
## Related issues
N/A
Cleanup runtime use of stdlib's testing internals which was required for older
Go versions.
Note: we are still using testing.RunTests in the suite's test suite for
now.
As pointed out in issue #1520, if the suite is not initialised properly
(buy calling the Run function), then calling suite.Require() or
suite.Assert() will result in a deadlock.
This commit fixes that by panicking if the suite is not initialised
properly. This is justified because, the suite is intended to be
triggered in the right way. If the user does not do that, this panic will
nudge them in the right direction.
It has to be a panic because, at this point, we don't have access to any
testing.T context to gracefully call a t.Fail(). Also, these two
functions are not expected to return an error.
Fixes#1520
This fix adds panic handling for subtests which will achieve:
- subtests will fail for the correct test context when panicking
- the test execution is not stopped; the next subtest will be executed
There were two problems with the order of execution in the Suite.Run() method:
- One could not access the correct testing context ("s.T()") inside the SetupSubTest and TearDownSubTest methods. If the testing context was used for e.g. assertions of mocks in the TearDownSubTest, the results would not be "attached" to the correct test in the test output.
- The behavior was different to the order of execution for "root" tests (see lines 167-201) regarding the SetupTest and TearDownTest methods. This could confuse users of the library.
Also the logic to be deferred was joined together. This was fine beforehand because a panic in the TearDownSubTest would have had no influence on the "suite.SetT(oldT)". Now since a panic in the TearDownSubTest would lead to omitting the "suite.SetT(oldT)" this defer was split into two separate defers.
When suite setup is long, the necessity to wait for all suite setups for testing one single method bothers a lot. Here's a little fix of that behavior.