Skip to content

chore: Refactor agent tests to avoid t.Run when not needed #5376

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
merged 3 commits into from
Dec 12, 2022

Conversation

mafredri
Copy link
Member

It turns out that writing tests that contain subtests should probably be
limited to table-based tests and tests that share a common setup shared
between tests.

Writing tests with a subtest like this:

func TestSomething(t *testing.T) {
	t.Run("Subtest", func(t *testing.t) {})
}

Has the following disadvantages:

  • It can lead to multiple tests failing with (unknown) status when
    only one of the subtests hang (never exit)
  • In Go 1.20rc1, using t.Setenv is no longer allowed if the parent
    test is parallel

This is from Go 1.20rc1, trying to run the agent tests:

--- FAIL: TestAgent (0.00s)
    --- FAIL: TestAgent/Session_TTY_MOTD (0.00s)
panic: testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests [recovered]
	panic: testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests

It's unclear if the above is a Go bug or not, but I've (and maybe others) have observed in the past that t.Setenv isn't always behaving as expected. This could potentially be the source of that confusing behavior.

It turns out that writing tests that contain subtests should probably be
limited to table-based tests and tests that share a common setup shared
between tests.

Writing tests with a subtest like this:

```
func TestSomething(t *testing.T) {
	t.Run("Subtest", func(t *testing.t) {})
}
```

Has the following disadvantages:

- It can lead to multiple tests failing with `(unknown)` status when
  only one of the subtests hang (never exit)
- In Go 1.20rc1, using `t.Setenv` is no longer allowed if the parent
  test is parallel
@mafredri mafredri self-assigned this Dec 12, 2022
@mafredri mafredri requested a review from a team December 12, 2022 10:09
@mafredri mafredri merged commit 760419a into main Dec 12, 2022
@mafredri mafredri deleted the mafredri/agent-test-refactor branch December 12, 2022 20:20
@github-actions github-actions bot locked and limited conversation to collaborators Dec 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants