Skip to content

Documentation: Update Go style guide with assert vs require usage

The following discussion from https://gitlab.com/gitlab-org/gitlab-releaser/-/merge_requests/4 should be addressed:

  • @steveazz started a discussion: (+4 comments)

    For gitlab-org/gitlab-runner we usually use assert instead of require for scenarios where we are checking the state of things so that the developer gets all the data that is unexpected instead 1 by 1. We only use require for error checks for example where it doesn't make sense to continue the test should we do the same?

    got := New("version", "revision")
    require.NotNil(t, got)
    
    assert.Equal(t, "gitlab-releaser", got.Name)
    assert.Len(t, got.Commands, 1)

Problem to solve

Discuss the use of testify assert vs require in Go tests.

Further details

Our own Go standards and style guides on testing frameworks states that testify is a suitable framework to help with writing and reading unit tests. However; it wasn't defined wether we should use assert and/or require only and this has sparked some controversy.

Using assert will allow the developer to learn about failures on different tests as they occur but it will not halt the rest of the tests execution. But using require will stop the tests on the first failure, providing a fail-fast approach.

Proposal

Update documentation with some guidance

  • Use assert at the developer's/team's discretion
  • Explain both pros and cons of assert vs require

Who can address the issue

Go developers

Other links/references

Edited by 🤖 GitLab Bot 🤖