Skip to content

Commit

Permalink
[ISSUE-47] support provider.StepCtx
Browse files Browse the repository at this point in the history
  • Loading branch information
siller174 committed Nov 16, 2023
1 parent 28bb430 commit 500f6c2
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 42 deletions.
77 changes: 48 additions & 29 deletions cute.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,18 @@ type allureLinks struct {
tmsLinks []string
}

func (it *cute) ExecuteTest(ctx context.Context, t testing.TB) []ResultsHTTPBuilder {
func (it *cute) ExecuteTest(ctx context.Context, t tProvider) []ResultsHTTPBuilder {
var internalT allureProvider

if t == nil {
panic("could not start test without testing.T")
}

stepCtx, isStepCtx := t.(provider.StepCtx)
if isStepCtx {
return it.executeTestInsideStep(ctx, stepCtx)
}

tOriginal, ok := t.(*testing.T)
if ok {
newT := createAllureT(tOriginal)
Expand All @@ -84,6 +93,22 @@ func (it *cute) ExecuteTest(ctx context.Context, t testing.TB) []ResultsHTTPBuil
return it.executeTest(ctx, internalT)
}

func (it *cute) executeTestInsideStep(ctx context.Context, stepCtx provider.StepCtx) []ResultsHTTPBuilder {
var (
res = make([]ResultsHTTPBuilder, 0)
)

// Cycle for change number of Test
for i := 0; i <= it.countTests; i++ {
currentTest := it.tests[i]

result := currentTest.executeInsideStep(ctx, stepCtx)
res = append(res, result)
}

return res
}

func createAllureT(t *testing.T) *common.Common {
var (
newT = common.NewT(t)
Expand Down Expand Up @@ -117,45 +142,39 @@ func (it *cute) executeTest(ctx context.Context, allureProvider allureProvider)
tableTestName := currentTest.Name

allureProvider.Run(tableTestName, func(inT provider.T) {
// Copy allure labels from common allure test
it.setAllureInformation(inT)
inT.Title(tableTestName) // set current test name

inT.Logf("Test start %v", tableTestName)
resT := currentTest.execute(ctx, inT)
res = append(res, resT)

if resT.IsFailed() {
inT.Fail()

allureProvider.Logf("Test was failed %v", currentTest.Name)

return
}

inT.Logf("Test finished %v", tableTestName)
resultTest := it.executeSingleTest(ctx, inT, currentTest)
res = append(res, resultTest)
})
} else {
currentTest.Name = allureProvider.Name()

// set labels
it.setAllureInformation(allureProvider)

currentTest.Name = allureProvider.Name()

allureProvider.Logf("Test start %v", currentTest.Name)
resultTest := it.executeSingleTest(ctx, allureProvider, currentTest)
if resultTest != nil {
res = append(res, resultTest)
}
}
}

resT := currentTest.execute(ctx, allureProvider)
res = append(res, resT)
return res
}

if resT.IsFailed() {
allureProvider.Fail()
func (it *cute) executeSingleTest(ctx context.Context, allureProvider allureProvider, currentTest *Test) ResultsHTTPBuilder {
allureProvider.Logf("Test start %v", currentTest.Name)

allureProvider.Logf("Test was failed %v", currentTest.Name)
break
}
resT := currentTest.execute(ctx, allureProvider)
if resT.IsFailed() {
allureProvider.Fail()
allureProvider.Logf("Test was failed %v", currentTest.Name)

allureProvider.Logf("Test finished %v", currentTest.Name)
}
return resT
}

return res
allureProvider.Logf("Test finished %v", currentTest.Name)

return resT
}
43 changes: 43 additions & 0 deletions examples/inside_step_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package examples

import (
"context"
"net/http"
"net/url"
"testing"
"time"

"github.com/ozontech/allure-go/pkg/framework/provider"
"github.com/ozontech/allure-go/pkg/framework/runner"
"github.com/ozontech/cute"
)

func TestInsideStep(t *testing.T) {
runner.Run(t, "Single test with allure-go Runner", func(t provider.T) {

t.WithNewStep("First step", func(sCtx provider.StepCtx) {
sCtx.NewStep("Inside first step")
})

t.WithNewStep("Step name", func(sCtx provider.StepCtx) {
u, _ := url.Parse("https://jsonplaceholder.typicode.com/posts/1/comments")

cute.NewTestBuilder().
Title("Super simple test").
Tags("simple", "suite", "some_local_tag", "json").
Parallel().
Create().
RequestBuilder(
cute.WithHeaders(map[string][]string{
"some_header": []string{"something"},
}),
cute.WithURL(u),
cute.WithMethod(http.MethodPost),
).
ExpectExecuteTimeout(10*time.Second).
ExpectStatus(http.StatusCreated).
ExecuteTest(context.Background(), sCtx)
})
})

}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/ozontech/cute
go 1.18

require (
github.com/PaesslerAG/jsonpath v0.1.1
github.com/ohler55/ojg v1.12.9
github.com/ozontech/allure-go/pkg/allure v0.6.11
github.com/ozontech/allure-go/pkg/framework v0.6.28
Expand All @@ -13,7 +12,6 @@ require (
)

require (
github.com/PaesslerAG/gval v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
github.com/PaesslerAG/gval v1.0.0 h1:GEKnRwkWDdf9dOmKcNrar9EA1bz1z9DqPIO1+iLzhd8=
github.com/PaesslerAG/gval v1.0.0/go.mod h1:y/nm5yEyTeX6av0OfKJNp9rBNj2XrGhAf5+v24IBN1I=
github.com/PaesslerAG/jsonpath v0.1.0/go.mod h1:4BzmtoM/PI8fPO4aQGIusjGxGir2BzcV0grWtFzq1Y8=
github.com/PaesslerAG/jsonpath v0.1.1 h1:c1/AToHQMVsduPAa4Vh6xp2U0evy4t8SWp8imEsylIk=
github.com/PaesslerAG/jsonpath v0.1.1/go.mod h1:lVboNxFGal/VwW6d9JzIy56bUsYAP6tH/x80vjnCseY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
3 changes: 1 addition & 2 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cute
import (
"context"
"net/http"
"testing"
"time"

"github.com/ozontech/allure-go/pkg/allure"
Expand Down Expand Up @@ -266,7 +265,7 @@ type ControlTest interface {
NextTest() NextTestBuilder

// ExecuteTest is a function for execute Test
ExecuteTest(ctx context.Context, t testing.TB) []ResultsHTTPBuilder
ExecuteTest(ctx context.Context, t tProvider) []ResultsHTTPBuilder
}

// NextTestBuilder is a scope of methods for processing response, after Test.
Expand Down
6 changes: 3 additions & 3 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ type tProvider interface {

Name() string

Log(args ...interface{})
Logf(format string, args ...interface{})

Error(args ...interface{})
Errorf(format string, args ...interface{})
}

type logProvider interface {
Log(args ...interface{})
Logf(format string, args ...interface{})

LogStep(args ...interface{})
LogfStep(format string, args ...interface{})
}
Expand Down
29 changes: 28 additions & 1 deletion test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,21 @@ type ExpectJSONSchema struct {
File string
}

func (it *Test) Execute(ctx context.Context, t testing.TB) ResultsHTTPBuilder {
func (it *Test) Execute(ctx context.Context, t tProvider) ResultsHTTPBuilder {
var (
internalT allureProvider
res ResultsHTTPBuilder
)

if t == nil {
panic("could not start test without testing.T")
}

stepCtx, isStepCtx := t.(provider.StepCtx)
if isStepCtx {
return it.executeInsideStep(ctx, stepCtx)
}

tOriginal, ok := t.(*testing.T)
if ok {
internalT = createAllureT(tOriginal)
Expand Down Expand Up @@ -126,6 +135,10 @@ func (it *Test) clearFields() {
}

func (it *Test) initEmptyFields() {
if it.httpClient == nil {
it.httpClient = http.DefaultClient
}

if it.AllureStep == nil {
it.AllureStep = new(AllureStep)
}
Expand All @@ -151,6 +164,20 @@ func (it *Test) initEmptyFields() {
}
}

func (it *Test) executeInsideStep(ctx context.Context, t internalT) ResultsHTTPBuilder {
resp, errs := it.startTest(ctx, t)

if len(errs) != 0 {
t.Fail()
}
isFailedTest := it.processTestErrors(t, errs)

// Remove from base struct all asserts
it.clearFields()

return newTestResult(t.Name(), resp, isFailedTest, errs)
}

func (it *Test) execute(ctx context.Context, allureProvider allureProvider) ResultsHTTPBuilder {
var (
resp *http.Response
Expand Down

0 comments on commit 500f6c2

Please sign in to comment.