Better testify usage (#54)
* Use assert.NoError instead of assert.Nil
This gives nicer error messages.
* Swap arguments to assert.Equal
The expectation comes first. Otherwise, error messages will be
misleading.
An example:
=== RUN TestDeleteWithIncorrectRepoForDeleteCaches
Error: authentication token not found for host github.com
delete_test.go:56:
Error Trace: /build/source/cmd/delete_test.go:56
Error: Should be true
Test: TestDeleteWithIncorrectRepoForDeleteCaches
Messages: 1 unmatched mocks: https://api.github.com/repos/testOrg/testRepo/actions/caches?key=cacheName
delete_test.go:59:
Error Trace: /build/source/cmd/delete_test.go:59
Error: Not equal:
expected: "authentication token not found for host github.com"
actual : "The given repo does not exist."
Diff:
--- Expected
+++ Actual
@@ -1 +1 @@
-authentication token not found for host github.com
+The given repo does not exist.
Test: TestDeleteWithIncorrectRepoForDeleteCaches
--- FAIL: TestDeleteWithIncorrectRepoForDeleteCaches (0.00s)
* Use assert.Error instead of assert.NotNil
This gives nicer error messages.
* Use assert.ErrorAs and assert.ErrorContains
This simplifies the assertions and potentially gives better error
messages.
* Use require instead of assert and use assert.NotNil as guard
This is to prevent panics in tests, when things get accessed which
shouldn't be nil.
This commit is contained in:
@@ -11,45 +11,44 @@ func TestGetRepo_IncorrectRepoString(t *testing.T) {
|
||||
r := "testOrg/testRepo/123/123"
|
||||
repo, err := GetRepo(r)
|
||||
|
||||
assert.Error(t, err)
|
||||
assert.ErrorContains(t, err, fmt.Sprintf("expected the \"[HOST/]OWNER/REPO\" format, got \"%s\"", r))
|
||||
assert.Nil(t, repo)
|
||||
assert.Equal(t, err.Error(), fmt.Sprintf("expected the \"[HOST/]OWNER/REPO\" format, got \"%s\"", r))
|
||||
}
|
||||
|
||||
func TestGetRepo_CorrectRepoString(t *testing.T) {
|
||||
r := "testOrg/testRepo"
|
||||
repo, err := GetRepo(r)
|
||||
|
||||
assert.NotNil(t, repo)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, repo.Host(), "github.com")
|
||||
assert.Equal(t, repo.Owner(), "testOrg")
|
||||
assert.Equal(t, repo.Name(), "testRepo")
|
||||
assert.NoError(t, err)
|
||||
if assert.NotNil(t, repo) {
|
||||
assert.Equal(t, "github.com", repo.Host())
|
||||
assert.Equal(t, "testOrg", repo.Owner())
|
||||
assert.Equal(t, "testRepo", repo.Name())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetRepo_CorrectRepoStringWithCustomHost(t *testing.T) {
|
||||
r := "api.testEnterprise.com/testOrg/testRepo"
|
||||
repo, err := GetRepo(r)
|
||||
|
||||
assert.NotNil(t, repo)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, repo.Host(), "api.testEnterprise.com")
|
||||
assert.Equal(t, repo.Owner(), "testOrg")
|
||||
assert.Equal(t, repo.Name(), "testRepo")
|
||||
assert.NoError(t, err)
|
||||
if assert.NotNil(t, repo) {
|
||||
assert.Equal(t, "api.testEnterprise.com", repo.Host())
|
||||
assert.Equal(t, "testOrg", repo.Owner())
|
||||
assert.Equal(t, "testRepo", repo.Name())
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatCacheSize_MB(t *testing.T) {
|
||||
cacheSizeInBytes := 1024 * 1024 * 1.5
|
||||
cacheSizeDetailString := FormatCacheSize(cacheSizeInBytes)
|
||||
|
||||
assert.NotNil(t, cacheSizeDetailString)
|
||||
assert.Equal(t, cacheSizeDetailString, "1.50 MB")
|
||||
assert.Equal(t, "1.50 MB", cacheSizeDetailString)
|
||||
}
|
||||
|
||||
func TestFormatCacheSize_GB(t *testing.T) {
|
||||
cacheSizeInBytes := 1024 * 1024 * 1024 * 1.5
|
||||
cacheSizeDetailString := FormatCacheSize(cacheSizeInBytes)
|
||||
|
||||
assert.NotNil(t, cacheSizeDetailString)
|
||||
assert.Equal(t, cacheSizeDetailString, "1.50 GB")
|
||||
assert.Equal(t, "1.50 GB", cacheSizeDetailString)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user