Skip to content

refact(tests): improves handling of cleanup func#21

Merged
supitsdu merged 1 commit intomainfrom
refact/tests
Jun 25, 2024
Merged

refact(tests): improves handling of cleanup func#21
supitsdu merged 1 commit intomainfrom
refact/tests

Conversation

@supitsdu
Copy link
Copy Markdown
Owner

Handles cleanup using t.Cleanup() instead of manually calling defer after using replaceStdin.

Related review: #19 discussion

Handles cleanup using t.Cleanup() instead of manually calling defer
after using replaceStdin.
@supitsdu supitsdu added testing Issues or PRs specifically related to Go testing frameworks or practices. refactoring Issues or PRs to improving code structure, without changing its external behavior labels Jun 25, 2024
@ccoVeille
Copy link
Copy Markdown
Contributor

That's the main reason why t.Cleanup was added: avoid the callbaxk that you need to return and defer everywhere you call the method

That's why t.Cleanup supersedes defer in tests.

So when I see a defer in tests I ask to use t.Cleanup

Also, the defer and t.Cleanup registration order differs. It helps in a few cases

Here are a few articles

https://brandur.org/fragments/go-prefer-t-cleanup-with-parallel-subtests#:~:text=The%20fix%20is%20t.,with%20pauses%20caused%20by%20t.
https://stackoverflow.com/questions/61609085/what-is-useful-for-t-cleanup

@supitsdu supitsdu merged commit 0951d39 into main Jun 25, 2024
@supitsdu supitsdu deleted the refact/tests branch June 25, 2024 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring Issues or PRs to improving code structure, without changing its external behavior testing Issues or PRs specifically related to Go testing frameworks or practices.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants