Specify setup/teardown for each test individually
See original GitHub issueIs your feature request related to a problem or a nice-to-have?? Please describe.
Currently, we can:
- Define a setup/teardown for a
describe
block by usingbefore
/after
. - Define a setup/teardown for all
it
blocks by usingbeforeEach
/afterEach
.
There is no way to define a setup/teardown for one it
block.
As a workaround, I’m putting the setup code inside the it
block. Downside:
- The setup code is mixed with the calculation.
- The setup time is also counted in the timing.
Describe the solution you’d like
Some proposals:
- Make
it
accept more arguments to define setup/teardown:it( "my test", () => { // setup }, () => { // test }, () => { // teardown } ); // look better with an option object it({ name: "my test", setup() { // ... }, test() { // ... }, teardown() { // ... } });
- Add new block (or reuse
before
,after
) insideit
:it("my test", () => { itBefore(() => { // ... }); // test itAfter(() => { // ... }); });
- Allows the test to emit an event to indicate the current phase:
it("my test", function () { this.section("setup"); // ... this.section("test"); // ... this.section("teardown"); // ... });
Describe alternatives you’ve considered
Additional context
This should be handy for dynamically-generated tests since they often have to read some data from the disk before running the actual test.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Set Up and Tear Down State in Your Tests - Apple Developer
When you run a test case, XCTest calls the XCTestCase setUp() class method first. Use this method to set up state common to...
Read more >Setup and Teardown - Jest
Jest executes all describe handlers in a test file before it executes any of the actual tests. This is another reason to do...
Read more >Unittest setUp/tearDown for several tests - Stack Overflow
It appears that pydev doesn't use the test suite, but finds all individual test cases and runs them all separately. My solution for...
Read more >All About setUp() And tearDown() - Medium
When Xcode runs tests, it invokes each test method independently. Therefore each method must prepare and cleanup all the allocations.
Read more >One-Time Set Up and Tear Down - Java Extreme ... - O'Reilly
As outlined in Recipe 4.6, JUnit calls setUp( ) before each test, and tearDown( ) after each test. In some cases you might...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That is another workaround. It prints an additional line for each test as the downside:
Or perhaps more simply, just put a
describe
around the test (it
) that you want to add hooks for setup/teardown. No changes required and backwards/forwards-compatible.