-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from lamoda/subtest
#187 Wrap test in subtest for go test runner
- Loading branch information
Showing
9 changed files
with
307 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package runner | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/lamoda/gonkey/models" | ||
) | ||
|
||
type ConsoleHandler struct { | ||
totalTests int | ||
failedTests int | ||
skippedTests int | ||
brokenTests int | ||
} | ||
|
||
func NewConsoleHandler() *ConsoleHandler { | ||
return &ConsoleHandler{} | ||
} | ||
|
||
func (h *ConsoleHandler) HandleTest(test models.TestInterface, executeTest testExecutor) error { | ||
testResult, err := executeTest(test) | ||
switch { | ||
case err != nil && errors.Is(err, errTestSkipped): | ||
h.skippedTests++ | ||
case err != nil && errors.Is(err, errTestBroken): | ||
h.brokenTests++ | ||
case err != nil: | ||
return err | ||
} | ||
|
||
h.totalTests++ | ||
if !testResult.Passed() { | ||
h.failedTests++ | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (h *ConsoleHandler) Summary() *models.Summary { | ||
return &models.Summary{ | ||
Success: h.failedTests == 0, | ||
Skipped: h.skippedTests, | ||
Broken: h.brokenTests, | ||
Failed: h.failedTests, | ||
Total: h.totalTests, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package runner | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/lamoda/gonkey/models" | ||
) | ||
|
||
func TestConsoleHandler_HandleTest_Summary(t *testing.T) { | ||
type result struct { | ||
result *models.Result | ||
err error | ||
} | ||
tests := []struct { | ||
name string | ||
testExecResults []result | ||
wantErr bool | ||
expectedSummary *models.Summary | ||
}{ | ||
{ | ||
name: "1 successful test", | ||
testExecResults: []result{{result: &models.Result{Errors: nil}}}, | ||
expectedSummary: &models.Summary{ | ||
Success: true, | ||
Failed: 0, | ||
Total: 1, | ||
}, | ||
}, | ||
{ | ||
name: "1 successful test, 1 broken test", | ||
testExecResults: []result{ | ||
{result: &models.Result{}, err: nil}, | ||
{result: &models.Result{}, err: errTestBroken}, | ||
}, | ||
expectedSummary: &models.Summary{ | ||
Success: true, | ||
Failed: 0, | ||
Broken: 1, | ||
Total: 2, | ||
}, | ||
}, | ||
{ | ||
name: "1 successful test, 1 skipped test", | ||
testExecResults: []result{ | ||
{result: &models.Result{}, err: nil}, | ||
{result: &models.Result{}, err: errTestSkipped}, | ||
}, | ||
expectedSummary: &models.Summary{ | ||
Success: true, | ||
Skipped: 1, | ||
Total: 2, | ||
}, | ||
}, | ||
{ | ||
name: "1 successful test, 1 failed test", | ||
testExecResults: []result{ | ||
{result: &models.Result{}, err: nil}, | ||
{result: &models.Result{Errors: []error{errors.New("some err")}}, err: nil}, | ||
}, | ||
expectedSummary: &models.Summary{ | ||
Success: false, | ||
Failed: 1, | ||
Total: 2, | ||
}, | ||
}, | ||
{ | ||
name: "test with unexpected error", | ||
testExecResults: []result{ | ||
{result: &models.Result{}, err: errors.New("unexpected error")}, | ||
}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
h := NewConsoleHandler() | ||
|
||
for _, execResult := range tt.testExecResults { | ||
executor := func(models.TestInterface) (*models.Result, error) { | ||
return execResult.result, execResult.err | ||
} | ||
err := h.HandleTest(nil, executor) | ||
if tt.wantErr { | ||
require.Error(t, err) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
} | ||
|
||
require.Equal(t, tt.expectedSummary, h.Summary()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.