Skip to content

Commit c134b04

Browse files
authored
✨ Added error testing utilities (#252)
<!-- Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved. SPDX-License-Identifier: Apache-2.0 --> ### Description -Added error testing utilities ### Test Coverage <!-- Please put an `x` in the correct box e.g. `[x]` to indicate the testing coverage of this change. --> - [x] This change is covered by existing or additional automated tests. - [ ] Manual testing has been performed (and evidence provided) as automated testing was not feasible. - [ ] Additional tests are not required for this change (e.g. documentation update).
1 parent af049fd commit c134b04

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

changes/202305241743.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
:sparkles: Added testing utilities with regards to error assertions
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package errortest
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
9+
"github.com/ARM-software/golang-utils/utils/commonerrors"
10+
)
11+
12+
// AssertError asserts that the error is matching one of the `expectedErrors`
13+
// This is a wrapper for commonerrors.Any.
14+
func AssertError(t *testing.T, err error, expectedErrors ...error) bool {
15+
if commonerrors.Any(err, expectedErrors...) {
16+
return true
17+
}
18+
return assert.Fail(t, fmt.Sprintf("Failed error assertion:\n actual: %v\n expected: %+v", err, expectedErrors))
19+
}
20+
21+
// RequireError requires that the error is matching one of the `expectedErrors`
22+
// This is a wrapper for commonerrors.Any.
23+
func RequireError(t *testing.T, err error, expectedErrors ...error) {
24+
t.Helper()
25+
if commonerrors.Any(err, expectedErrors...) {
26+
return
27+
}
28+
t.FailNow()
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package errortest
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ARM-software/golang-utils/utils/commonerrors"
7+
)
8+
9+
func TestAssertError(t *testing.T) {
10+
AssertError(t, commonerrors.ErrUndefined, commonerrors.ErrNotFound, commonerrors.ErrMarshalling, commonerrors.ErrUndefined)
11+
}
12+
13+
func TestRequireError(t *testing.T) {
14+
RequireError(t, commonerrors.ErrUndefined, commonerrors.ErrNotFound, commonerrors.ErrMarshalling, commonerrors.ErrUndefined)
15+
}

0 commit comments

Comments
 (0)