Setup and teardown of e2e tests.

Written by
Link to Post

https://www.reddit.com/r/golang/comments/advpn2/setup_and_teardown_of_e2e_tests/

I am currently writing a little web app to manage apartment rentals. I need to write e2e tests that require setup/teardown configuration before and after the tests are run. I have been taking advantage of t.Run, but I have the suspicion I might be abusing it. To illustrate, this is what I am doing:

“` func TestCreateUserByAdmin(t *testing.T) { // Arrange // Create app and run the http server. var wg sync.WaitGroup app, err := NewApp(addr)

// Clean up the db after we are done. defer app.dropDB() wg.Add(1) go func() { defer wg.Done() log.Printf("[ERROR] %s", app.ServeHTTP()) }() payload := []byte(`{"username":"john", "password": "secret", "role": "client"}`) t.Run("Create user no auth, fail", func(t *testing.T) { // Act & Assert: test for condition 1. // ... }) t.Run("Create user with admin, success", func(t *testing.T) { // Act & Assert: test for condition 2. // ... }) 

} “`

I know I could tests my handlers directly, but I wanted to also test all the startup of my app (after all it’s an e2e test). Here is all the project in case you need more detail: https://gitlab.com/kazlu/rentals

Now, my question: is this a good way of achieving this or Am I indeed abusing t.Run? As a bonus, if anyone could point me to some well e2e tested applications in Go, I would greately appreciate it.

Thanks in advance

submitted by /u/afruizc
[link] [comments]

Article Tags:
· ·
Article Categories:
reddit

Leave a Reply