Skip to content

Commit

Permalink
Release/v0.1.22 (#84)
Browse files Browse the repository at this point in the history
New functionality Retry test
Rename functions RequestRepeat* to RequestRetry*
Fix broken tests
---------

Co-authored-by: Sam B. <sambenattar@gmail.com>
Co-authored-by: Samuel Benattar <samuel.benattar@bytedance.com>
  • Loading branch information
3 people authored Oct 21, 2024
1 parent b1e4e33 commit 059c650
Show file tree
Hide file tree
Showing 21 changed files with 719 additions and 169 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- name: Run examples
run: make examples
- name: Archive code coverage results
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
with:
name: allure-results
path: ./examples/allure-results
2 changes: 1 addition & 1 deletion README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (i *ExampleSuite) BeforeAll(t provider.T) {
// Preparing host
host, err := url.Parse("https://jsonplaceholder.typicode.com/")
if err != nil {
t.Fatalf("could not parse url, error %v", err)
t.Fatalf("could not parse url, error %w", err)
}

i.host = host
Expand Down
6 changes: 3 additions & 3 deletions assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (it *Test) assertHeaders(t internalT, headers http.Header) []error {
return nil
}

return executeWithStep(t, "Assert headers", func(t T) []error {
return it.executeWithStep(t, "Assert headers", func(t T) []error {
errs := make([]error, 0)
// Execute assert only response
for _, f := range asserts {
Expand Down Expand Up @@ -85,7 +85,7 @@ func (it *Test) assertResponse(t internalT, resp *http.Response) []error {
return nil
}

return executeWithStep(t, "Assert response", func(t T) []error {
return it.executeWithStep(t, "Assert response", func(t T) []error {
errs := make([]error, 0)
// Execute assert only response
for _, f := range asserts {
Expand Down Expand Up @@ -117,7 +117,7 @@ func (it *Test) assertBody(t internalT, body []byte) []error {
return nil
}

return executeWithStep(t, "Assert body", func(t T) []error {
return it.executeWithStep(t, "Assert body", func(t T) []error {
errs := make([]error, 0)
// Execute assert only response
for _, f := range asserts {
Expand Down
2 changes: 1 addition & 1 deletion builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func createDefaultTest(m *HTTPTestMaker) *Test {
Middleware: createMiddlewareFromTemplate(m.middleware),
AllureStep: new(AllureStep),
Request: &Request{
Repeat: new(RequestRepeatPolitic),
Retry: new(RequestRetryPolitic),
},
Expect: &Expect{JSONSchema: new(ExpectJSONSchema)},
}
Expand Down
70 changes: 64 additions & 6 deletions builder_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ import (
// RequestRepeat is a function for set options in request
// if response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
// Default delay is 1 second.
// Deprecated: use RequestRetry instead
func (qt *cute) RequestRepeat(count int) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Repeat.Count = count
qt.tests[qt.countTests].Request.Retry.Count = count

return qt
}

// RequestRepeatDelay set delay for request repeat.
// if response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
// Default delay is 1 second.
// Deprecated: use RequestRetryDelay instead
func (qt *cute) RequestRepeatDelay(delay time.Duration) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Repeat.Delay = delay
qt.tests[qt.countTests].Request.Retry.Delay = delay

return qt
}
Expand All @@ -27,28 +29,84 @@ func (qt *cute) RequestRepeatDelay(delay time.Duration) RequestHTTPBuilder {
// if response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
// if Optional is true and request is failed, than test step allure will be skipped, and t.Fail() will not execute.
// If Broken is true and request is failed, than test step allure will be broken, and t.Fail() will not execute.
// Deprecated: use RequestRetryPolitic instead
func (qt *cute) RequestRepeatPolitic(politic *RequestRepeatPolitic) RequestHTTPBuilder {
if politic == nil {
panic("politic is nil in RequestRepeatPolitic")
panic("politic is nil in RequestRetryPolitic")
}

qt.tests[qt.countTests].Request.Repeat = politic
qt.tests[qt.countTests].Request.Retry = &RequestRetryPolitic{
Count: politic.Count,
Delay: politic.Delay,
Optional: politic.Optional,
Broken: politic.Broken,
}

return qt
}

// RequestRepeatOptional set option politic for request repeat.
// if Optional is true and request is failed, than test step allure will be skipped, and t.Fail() will not execute.
// Deprecated: use RequestRetryOptional instead
func (qt *cute) RequestRepeatOptional(option bool) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Repeat.Optional = option
qt.tests[qt.countTests].Request.Retry.Optional = option

return qt
}

// RequestRepeatBroken set broken politic for request repeat.
// If Broken is true and request is failed, than test step allure will be broken, and t.Fail() will not execute.
// Deprecated: use RequestRetryBroken instead
func (qt *cute) RequestRepeatBroken(broken bool) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Repeat.Broken = broken
qt.tests[qt.countTests].Request.Retry.Broken = broken

return qt
}

// RequestRetry is a function for set options in request
// if response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
// Default delay is 1 second.
func (qt *cute) RequestRetry(count int) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Retry.Count = count

return qt
}

// RequestRetryDelay set delay for request repeat.
// if response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
// Default delay is 1 second.
func (qt *cute) RequestRetryDelay(delay time.Duration) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Retry.Delay = delay

return qt
}

// RequestRetryPolitic set politic for request repeat.
// if response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
// if Optional is true and request is failed, than test step allure will be skipped, and t.Fail() will not execute.
// If Broken is true and request is failed, than test step allure will be broken, and t.Fail() will not execute.
func (qt *cute) RequestRetryPolitic(politic *RequestRetryPolitic) RequestHTTPBuilder {
if politic == nil {
panic("politic is nil in RequestRetryPolitic")
}

qt.tests[qt.countTests].Request.Retry = politic

return qt
}

// RequestRetryOptional set option politic for request repeat.
// if Optional is true and request is failed, than test step allure will be skipped, and t.Fail() will not execute.
func (qt *cute) RequestRetryOptional(option bool) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Retry.Optional = option

return qt
}

// RequestRetryBroken set broken politic for request repeat.
// If Broken is true and request is failed, than test step allure will be broken, and t.Fail() will not execute.
func (qt *cute) RequestRetryBroken(broken bool) RequestHTTPBuilder {
qt.tests[qt.countTests].Request.Retry.Broken = broken

return qt
}
Expand Down
29 changes: 29 additions & 0 deletions builder_retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package cute

import "time"

// Retry is a function for configure test repeat
// if response.Code != Expect.Code or any of asserts are failed/broken than test will repeat counts with delay.
// Default delay is 1 second.
func (qt *cute) Retry(count int) MiddlewareRequest {
if count < 1 {
panic("count must be greater than 0")
}

qt.tests[qt.countTests].Retry.MaxAttempts = count

return qt
}

// RetryDelay set delay for test repeat.
// if response.Code != Expect.Code or any of asserts are failed/broken than test will repeat counts with delay.
// Default delay is 1 second.
func (qt *cute) RetryDelay(delay time.Duration) MiddlewareRequest {
if delay < 0 {
panic("delay must be greater than or equal to 0")
}

qt.tests[qt.countTests].Retry.Delay = delay

return qt
}
10 changes: 5 additions & 5 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ func TestHTTPTestMaker(t *testing.T) {
Link(link).
Description(desc).
CreateStep(stepName).
RequestRepeat(repeatCount).
RequestRepeatDelay(repeatDelay).
RequestRetry(repeatCount).
RequestRetryDelay(repeatDelay).
Request(req).
ExpectExecuteTimeout(executeTime).
ExpectStatus(status).
Expand Down Expand Up @@ -330,8 +330,8 @@ func TestHTTPTestMaker(t *testing.T) {
require.Equal(t, setIssue, resHt.allureLinks.issue)
require.Equal(t, setTestCase, resHt.allureLinks.testCase)
require.Equal(t, link, resHt.allureLinks.link)
require.Equal(t, repeatCount, resTest.Request.Repeat.Count)
require.Equal(t, repeatDelay, resTest.Request.Repeat.Delay)
require.Equal(t, repeatCount, resTest.Request.Retry.Count)
require.Equal(t, repeatDelay, resTest.Request.Retry.Delay)

require.Equal(t, len(assertHeaders), len(resTest.Expect.AssertHeaders))
require.Equal(t, len(assertHeadersT), len(resTest.Expect.AssertHeadersT))
Expand Down Expand Up @@ -360,7 +360,7 @@ func TestCreateDefaultTest(t *testing.T) {
BeforeT: make([]BeforeExecuteT, 0),
},
Request: &Request{
Repeat: new(RequestRepeatPolitic),
Retry: new(RequestRetryPolitic),
},
Expect: &Expect{
JSONSchema: new(ExpectJSONSchema),
Expand Down
50 changes: 26 additions & 24 deletions cute.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,27 +113,6 @@ func createAllureT(t *testing.T) *common.Common {
return newT
}

// executeTestsInsideStep is method for run group of tests inside provider.StepCtx
func (qt *cute) executeTestsInsideStep(ctx context.Context, stepCtx provider.StepCtx) []ResultsHTTPBuilder {
var (
res = make([]ResultsHTTPBuilder, 0)
)

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

result := currentTest.executeInsideStep(ctx, stepCtx)

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

res = append(res, result)
}

return res
}

// executeTests is method for run tests
// It's could be table tests or usual tests
func (qt *cute) executeTests(ctx context.Context, allureProvider allureProvider) []ResultsHTTPBuilder {
Expand All @@ -153,26 +132,49 @@ func (qt *cute) executeTests(ctx context.Context, allureProvider allureProvider)
// Set current test name
inT.Title(tableTestName)

res = append(res, qt.executeSingleTest(ctx, inT, currentTest))
res = append(res, qt.executeInsideAllure(ctx, inT, currentTest))
})
} else {
currentTest.Name = allureProvider.Name()

// set labels
qt.setAllureInformation(allureProvider)

res = append(res, qt.executeSingleTest(ctx, allureProvider, currentTest))
res = append(res, qt.executeInsideAllure(ctx, allureProvider, currentTest))
}
}

return res
}

func (qt *cute) executeSingleTest(ctx context.Context, allureProvider allureProvider, currentTest *Test) ResultsHTTPBuilder {
// executeInsideAllure is method for run test inside allure
// It's could be table tests or usual tests
func (qt *cute) executeInsideAllure(ctx context.Context, allureProvider allureProvider, currentTest *Test) ResultsHTTPBuilder {
resT := currentTest.executeInsideAllure(ctx, allureProvider)

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

return resT
}

// executeTestsInsideStep is method for run group of tests inside provider.StepCtx
func (qt *cute) executeTestsInsideStep(ctx context.Context, stepCtx provider.StepCtx) []ResultsHTTPBuilder {
var (
res = make([]ResultsHTTPBuilder, 0)
)

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

result := currentTest.executeInsideStep(ctx, stepCtx)

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

res = append(res, result)
}

return res
}
16 changes: 9 additions & 7 deletions examples/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Test_Single_1(t *testing.T) {
Description("some_description").
Parallel().
Create().
RequestRepeat(3).
RequestRetry(3).
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
cute.WithMarshalBody(struct {
Expand Down Expand Up @@ -96,14 +96,16 @@ func Test_Single_Broken(t *testing.T) {
},
).
ExecuteTest(context.Background(), t)

t.Skip()
}

func Test_Single_RepeatPolitic_Optional_Success_Test(t *testing.T) {
cute.NewTestBuilder().
Title("Test_Single_RepeatPolitic_Optional_Success_Test").
Create().
RequestRepeat(2).
RequestRepeatOptional(true).
RequestRetry(2).
RequestRetryOptional(true).
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
).
Expand All @@ -120,8 +122,8 @@ func Test_Single_RepeatPolitic_Broken_Failed_Test(t *testing.T) {
cute.NewTestBuilder().
Title("Test_Single_RepeatPolitic_Broken_Failed_Test").
Create().
RequestRepeat(2).
RequestRepeatOptional(true).
RequestRetry(2).
RequestRetryOptional(false).
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
).
Expand Down Expand Up @@ -173,8 +175,8 @@ func Test_Single_2_AllureRunner(t *testing.T) {
Tag("single_test").
Description("some_description").
Create().
RequestRepeatDelay(3*time.Second). // delay before new try
RequestRepeat(3). // count attempts
RequestRetryDelay(3*time.Second). // delay before new try
RequestRetry(3). // count attempts
RequestBuilder(
cute.WithURL(u),
cute.WithMethod(http.MethodGet),
Expand Down
Loading

0 comments on commit 059c650

Please sign in to comment.