From efd35df95d3f84c4676e3a77a3770c022a9056f7 Mon Sep 17 00:00:00 2001 From: Sergey Makarov Date: Thu, 23 Nov 2023 14:26:35 +0100 Subject: [PATCH] [ISSUE-53] --- allure.go | 110 ++++----- builder.go | 462 +++++++++++++++++++------------------- cute.go | 77 ++++--- errors/error.go | 1 + examples/two_step_test.go | 17 +- roundtripper.go | 19 +- test.go | 32 ++- 7 files changed, 359 insertions(+), 359 deletions(-) diff --git a/allure.go b/allure.go index 2c32510..90432ae 100644 --- a/allure.go +++ b/allure.go @@ -1,89 +1,89 @@ package cute -func (it *cute) setAllureInformation(t allureProvider) { +func (qt *cute) setAllureInformation(t allureProvider) { // Log main vars to allureProvider - it.setLabelsAllure(t) - it.setInfoAllure(t) - it.setLinksAllure(t) + qt.setLabelsAllure(t) + qt.setInfoAllure(t) + qt.setLinksAllure(t) } -func (it *cute) setLinksAllure(t linksAllureProvider) { - if it.allureLinks.issue != "" { - t.SetIssue(it.allureLinks.issue) +func (qt *cute) setLinksAllure(t linksAllureProvider) { + if qt.allureLinks.issue != "" { + t.SetIssue(qt.allureLinks.issue) } - if it.allureLinks.testCase != "" { - t.SetTestCase(it.allureLinks.testCase) + if qt.allureLinks.testCase != "" { + t.SetTestCase(qt.allureLinks.testCase) } - if it.allureLinks.link != nil { - t.Link(it.allureLinks.link) + if qt.allureLinks.link != nil { + t.Link(qt.allureLinks.link) } - if it.allureLinks.tmsLink != "" { - t.TmsLink(it.allureLinks.tmsLink) + if qt.allureLinks.tmsLink != "" { + t.TmsLink(qt.allureLinks.tmsLink) } - if len(it.allureLinks.tmsLinks) > 0 { - t.TmsLinks(it.allureLinks.tmsLinks...) + if len(qt.allureLinks.tmsLinks) > 0 { + t.TmsLinks(qt.allureLinks.tmsLinks...) } } -func (it *cute) setLabelsAllure(t labelsAllureProvider) { - if it.allureLabels.id != "" { - t.ID(it.allureLabels.id) +func (qt *cute) setLabelsAllure(t labelsAllureProvider) { + if qt.allureLabels.id != "" { + t.ID(qt.allureLabels.id) } - if it.allureLabels.suiteLabel != "" { - t.AddSuiteLabel(it.allureLabels.suiteLabel) + if qt.allureLabels.suiteLabel != "" { + t.AddSuiteLabel(qt.allureLabels.suiteLabel) } - if it.allureLabels.subSuite != "" { - t.AddSubSuite(it.allureLabels.subSuite) + if qt.allureLabels.subSuite != "" { + t.AddSubSuite(qt.allureLabels.subSuite) } - if it.allureLabels.parentSuite != "" { - t.AddParentSuite(it.allureLabels.parentSuite) + if qt.allureLabels.parentSuite != "" { + t.AddParentSuite(qt.allureLabels.parentSuite) } - if it.allureLabels.story != "" { - t.Story(it.allureLabels.story) + if qt.allureLabels.story != "" { + t.Story(qt.allureLabels.story) } - if it.allureLabels.tag != "" { - t.Tag(it.allureLabels.tag) + if qt.allureLabels.tag != "" { + t.Tag(qt.allureLabels.tag) } - if it.allureLabels.allureID != "" { - t.AllureID(it.allureLabels.allureID) + if qt.allureLabels.allureID != "" { + t.AllureID(qt.allureLabels.allureID) } - if it.allureLabels.severity != "" { - t.Severity(it.allureLabels.severity) + if qt.allureLabels.severity != "" { + t.Severity(qt.allureLabels.severity) } - if it.allureLabels.owner != "" { - t.Owner(it.allureLabels.owner) + if qt.allureLabels.owner != "" { + t.Owner(qt.allureLabels.owner) } - if it.allureLabels.lead != "" { - t.Lead(it.allureLabels.lead) + if qt.allureLabels.lead != "" { + t.Lead(qt.allureLabels.lead) } - if it.allureLabels.label != nil { - t.Label(it.allureLabels.label) + if qt.allureLabels.label != nil { + t.Label(qt.allureLabels.label) } - if len(it.allureLabels.labels) != 0 { - t.Labels(it.allureLabels.labels...) + if len(qt.allureLabels.labels) != 0 { + t.Labels(qt.allureLabels.labels...) } - if it.allureLabels.feature != "" { - t.Feature(it.allureLabels.feature) + if qt.allureLabels.feature != "" { + t.Feature(qt.allureLabels.feature) } - if it.allureLabels.epic != "" { - t.Epic(it.allureLabels.epic) + if qt.allureLabels.epic != "" { + t.Epic(qt.allureLabels.epic) } - if len(it.allureLabels.tags) != 0 { - t.Tags(it.allureLabels.tags...) + if len(qt.allureLabels.tags) != 0 { + t.Tags(qt.allureLabels.tags...) } - if it.allureLabels.layer != "" { - t.Layer(it.allureLabels.layer) + if qt.allureLabels.layer != "" { + t.Layer(qt.allureLabels.layer) } } -func (it *cute) setInfoAllure(t infoAllureProvider) { - if it.allureInfo.title != "" { - t.Title(it.allureInfo.title) +func (qt *cute) setInfoAllure(t infoAllureProvider) { + if qt.allureInfo.title != "" { + t.Title(qt.allureInfo.title) } - if it.allureInfo.description != "" { - t.Description(it.allureInfo.description) + if qt.allureInfo.description != "" { + t.Description(qt.allureInfo.description) } - if it.allureInfo.stage != "" { - t.Stage(it.allureInfo.stage) + if qt.allureInfo.stage != "" { + t.Stage(qt.allureInfo.stage) } } diff --git a/builder.go b/builder.go index d2d1912..ac51118 100644 --- a/builder.go +++ b/builder.go @@ -174,574 +174,574 @@ func createDefaultTest(m *HTTPTestMaker) *Test { } } -func (it *cute) Title(title string) AllureBuilder { - it.allureInfo.title = title +func (qt *cute) Title(title string) AllureBuilder { + qt.allureInfo.title = title - return it + return qt } -func (it *cute) Epic(epic string) AllureBuilder { - it.allureLabels.epic = epic +func (qt *cute) Epic(epic string) AllureBuilder { + qt.allureLabels.epic = epic - return it + return qt } -func (it *cute) Titlef(format string, args ...interface{}) AllureBuilder { - it.allureInfo.title = fmt.Sprintf(format, args...) +func (qt *cute) Titlef(format string, args ...interface{}) AllureBuilder { + qt.allureInfo.title = fmt.Sprintf(format, args...) - return it + return qt } -func (it *cute) Descriptionf(format string, args ...interface{}) AllureBuilder { - it.allureInfo.description = fmt.Sprintf(format, args...) +func (qt *cute) Descriptionf(format string, args ...interface{}) AllureBuilder { + qt.allureInfo.description = fmt.Sprintf(format, args...) - return it + return qt } -func (it *cute) Stage(stage string) AllureBuilder { - it.allureInfo.stage = stage +func (qt *cute) Stage(stage string) AllureBuilder { + qt.allureInfo.stage = stage - return it + return qt } -func (it *cute) Stagef(format string, args ...interface{}) AllureBuilder { - it.allureInfo.stage = fmt.Sprintf(format, args...) +func (qt *cute) Stagef(format string, args ...interface{}) AllureBuilder { + qt.allureInfo.stage = fmt.Sprintf(format, args...) - return it + return qt } -func (it *cute) Layer(value string) AllureBuilder { - it.allureLabels.layer = value +func (qt *cute) Layer(value string) AllureBuilder { + qt.allureLabels.layer = value - return it + return qt } -func (it *cute) TmsLink(tmsLink string) AllureBuilder { - it.allureLinks.tmsLink = tmsLink +func (qt *cute) TmsLink(tmsLink string) AllureBuilder { + qt.allureLinks.tmsLink = tmsLink - return it + return qt } -func (it *cute) TmsLinks(tmsLinks ...string) AllureBuilder { - it.allureLinks.tmsLinks = append(it.allureLinks.tmsLinks, tmsLinks...) +func (qt *cute) TmsLinks(tmsLinks ...string) AllureBuilder { + qt.allureLinks.tmsLinks = append(qt.allureLinks.tmsLinks, tmsLinks...) - return it + return qt } -func (it *cute) SetIssue(issue string) AllureBuilder { - it.allureLinks.issue = issue +func (qt *cute) SetIssue(issue string) AllureBuilder { + qt.allureLinks.issue = issue - return it + return qt } -func (it *cute) SetTestCase(testCase string) AllureBuilder { - it.allureLinks.testCase = testCase +func (qt *cute) SetTestCase(testCase string) AllureBuilder { + qt.allureLinks.testCase = testCase - return it + return qt } -func (it *cute) Link(link *allure.Link) AllureBuilder { - it.allureLinks.link = link +func (qt *cute) Link(link *allure.Link) AllureBuilder { + qt.allureLinks.link = link - return it + return qt } -func (it *cute) ID(value string) AllureBuilder { - it.allureLabels.id = value +func (qt *cute) ID(value string) AllureBuilder { + qt.allureLabels.id = value - return it + return qt } -func (it *cute) AllureID(value string) AllureBuilder { - it.allureLabels.allureID = value +func (qt *cute) AllureID(value string) AllureBuilder { + qt.allureLabels.allureID = value - return it + return qt } -func (it *cute) AddSuiteLabel(value string) AllureBuilder { - it.allureLabels.suiteLabel = value +func (qt *cute) AddSuiteLabel(value string) AllureBuilder { + qt.allureLabels.suiteLabel = value - return it + return qt } -func (it *cute) AddSubSuite(value string) AllureBuilder { - it.allureLabels.subSuite = value +func (qt *cute) AddSubSuite(value string) AllureBuilder { + qt.allureLabels.subSuite = value - return it + return qt } -func (it *cute) AddParentSuite(value string) AllureBuilder { - it.allureLabels.parentSuite = value +func (qt *cute) AddParentSuite(value string) AllureBuilder { + qt.allureLabels.parentSuite = value - return it + return qt } -func (it *cute) Story(value string) AllureBuilder { - it.allureLabels.story = value +func (qt *cute) Story(value string) AllureBuilder { + qt.allureLabels.story = value - return it + return qt } -func (it *cute) Tag(value string) AllureBuilder { - it.allureLabels.tag = value +func (qt *cute) Tag(value string) AllureBuilder { + qt.allureLabels.tag = value - return it + return qt } -func (it *cute) Severity(value allure.SeverityType) AllureBuilder { - it.allureLabels.severity = value +func (qt *cute) Severity(value allure.SeverityType) AllureBuilder { + qt.allureLabels.severity = value - return it + return qt } -func (it *cute) Owner(value string) AllureBuilder { - it.allureLabels.owner = value +func (qt *cute) Owner(value string) AllureBuilder { + qt.allureLabels.owner = value - return it + return qt } -func (it *cute) Lead(value string) AllureBuilder { - it.allureLabels.lead = value +func (qt *cute) Lead(value string) AllureBuilder { + qt.allureLabels.lead = value - return it + return qt } -func (it *cute) Label(label *allure.Label) AllureBuilder { - it.allureLabels.label = label +func (qt *cute) Label(label *allure.Label) AllureBuilder { + qt.allureLabels.label = label - return it + return qt } -func (it *cute) Labels(labels ...*allure.Label) AllureBuilder { - it.allureLabels.labels = labels +func (qt *cute) Labels(labels ...*allure.Label) AllureBuilder { + qt.allureLabels.labels = labels - return it + return qt } -func (it *cute) Description(description string) AllureBuilder { - it.allureInfo.description = description +func (qt *cute) Description(description string) AllureBuilder { + qt.allureInfo.description = description - return it + return qt } -func (it *cute) Tags(tags ...string) AllureBuilder { - it.allureLabels.tags = tags +func (qt *cute) Tags(tags ...string) AllureBuilder { + qt.allureLabels.tags = tags - return it + return qt } -func (it *cute) Feature(feature string) AllureBuilder { - it.allureLabels.feature = feature +func (qt *cute) Feature(feature string) AllureBuilder { + qt.allureLabels.feature = feature - return it + return qt } -func (it *cute) Create() MiddlewareRequest { - return it +func (qt *cute) Create() MiddlewareRequest { + return qt } -func (it *cute) CreateStep(name string) MiddlewareRequest { - it.tests[it.countTests].AllureStep.Name = name +func (qt *cute) CreateStep(name string) MiddlewareRequest { + qt.tests[qt.countTests].AllureStep.Name = name - return it + return qt } -func (it *cute) Parallel() AllureBuilder { - it.parallel = true +func (qt *cute) Parallel() AllureBuilder { + qt.parallel = true - return it + return qt } -func (it *cute) CreateRequest() RequestHTTPBuilder { - return it +func (qt *cute) CreateRequest() RequestHTTPBuilder { + return qt } -func (it *cute) StepName(name string) MiddlewareRequest { - it.tests[it.countTests].AllureStep.Name = name +func (qt *cute) StepName(name string) MiddlewareRequest { + qt.tests[qt.countTests].AllureStep.Name = name - return it + return qt } -func (it *cute) BeforeExecute(fs ...BeforeExecute) MiddlewareRequest { - it.tests[it.countTests].Middleware.Before = append(it.tests[it.countTests].Middleware.Before, fs...) +func (qt *cute) BeforeExecute(fs ...BeforeExecute) MiddlewareRequest { + qt.tests[qt.countTests].Middleware.Before = append(qt.tests[qt.countTests].Middleware.Before, fs...) - return it + return qt } -func (it *cute) BeforeExecuteT(fs ...BeforeExecuteT) MiddlewareRequest { - it.tests[it.countTests].Middleware.BeforeT = append(it.tests[it.countTests].Middleware.BeforeT, fs...) +func (qt *cute) BeforeExecuteT(fs ...BeforeExecuteT) MiddlewareRequest { + qt.tests[qt.countTests].Middleware.BeforeT = append(qt.tests[qt.countTests].Middleware.BeforeT, fs...) - return it + return qt } -func (it *cute) After(fs ...AfterExecute) ExpectHTTPBuilder { - it.tests[it.countTests].Middleware.After = append(it.tests[it.countTests].Middleware.After, fs...) +func (qt *cute) After(fs ...AfterExecute) ExpectHTTPBuilder { + qt.tests[qt.countTests].Middleware.After = append(qt.tests[qt.countTests].Middleware.After, fs...) - return it + return qt } -func (it *cute) AfterT(fs ...AfterExecuteT) ExpectHTTPBuilder { - it.tests[it.countTests].Middleware.AfterT = append(it.tests[it.countTests].Middleware.AfterT, fs...) +func (qt *cute) AfterT(fs ...AfterExecuteT) ExpectHTTPBuilder { + qt.tests[qt.countTests].Middleware.AfterT = append(qt.tests[qt.countTests].Middleware.AfterT, fs...) - return it + return qt } -func (it *cute) AfterExecute(fs ...AfterExecute) MiddlewareRequest { - it.tests[it.countTests].Middleware.After = append(it.tests[it.countTests].Middleware.After, fs...) +func (qt *cute) AfterExecute(fs ...AfterExecute) MiddlewareRequest { + qt.tests[qt.countTests].Middleware.After = append(qt.tests[qt.countTests].Middleware.After, fs...) - return it + return qt } -func (it *cute) AfterExecuteT(fs ...AfterExecuteT) MiddlewareRequest { - it.tests[it.countTests].Middleware.AfterT = append(it.tests[it.countTests].Middleware.AfterT, fs...) +func (qt *cute) AfterExecuteT(fs ...AfterExecuteT) MiddlewareRequest { + qt.tests[qt.countTests].Middleware.AfterT = append(qt.tests[qt.countTests].Middleware.AfterT, fs...) - return it + return qt } -func (it *cute) AfterTestExecute(fs ...AfterExecute) NextTestBuilder { +func (qt *cute) AfterTestExecute(fs ...AfterExecute) NextTestBuilder { previousTest := 0 - if it.countTests != 0 { - previousTest = it.countTests - 1 + if qt.countTests != 0 { + previousTest = qt.countTests - 1 } - it.tests[previousTest].Middleware.After = append(it.tests[previousTest].Middleware.After, fs...) + qt.tests[previousTest].Middleware.After = append(qt.tests[previousTest].Middleware.After, fs...) - return it + return qt } -func (it *cute) AfterTestExecuteT(fs ...AfterExecuteT) NextTestBuilder { +func (qt *cute) AfterTestExecuteT(fs ...AfterExecuteT) NextTestBuilder { previousTest := 0 - if it.countTests != 0 { - previousTest = it.countTests - 1 + if qt.countTests != 0 { + previousTest = qt.countTests - 1 } - it.tests[previousTest].Middleware.AfterT = append(it.tests[previousTest].Middleware.AfterT, fs...) + qt.tests[previousTest].Middleware.AfterT = append(qt.tests[previousTest].Middleware.AfterT, fs...) - return it + return qt } -func (it *cute) RequestRepeat(count int) RequestHTTPBuilder { - it.tests[it.countTests].Request.Repeat.Count = count +func (qt *cute) RequestRepeat(count int) RequestHTTPBuilder { + qt.tests[qt.countTests].Request.Repeat.Count = count - return it + return qt } -func (it *cute) RequestRepeatDelay(delay time.Duration) RequestHTTPBuilder { - it.tests[it.countTests].Request.Repeat.Delay = delay +func (qt *cute) RequestRepeatDelay(delay time.Duration) RequestHTTPBuilder { + qt.tests[qt.countTests].Request.Repeat.Delay = delay - return it + return qt } -func (it *cute) Request(r *http.Request) ExpectHTTPBuilder { - it.tests[it.countTests].Request.Base = r +func (qt *cute) Request(r *http.Request) ExpectHTTPBuilder { + qt.tests[qt.countTests].Request.Base = r - return it + return qt } -func (it *cute) RequestBuilder(r ...RequestBuilder) ExpectHTTPBuilder { - it.tests[it.countTests].Request.Builders = append(it.tests[it.countTests].Request.Builders, r...) +func (qt *cute) RequestBuilder(r ...RequestBuilder) ExpectHTTPBuilder { + qt.tests[qt.countTests].Request.Builders = append(qt.tests[qt.countTests].Request.Builders, r...) - return it + return qt } -func (it *cute) ExpectExecuteTimeout(t time.Duration) ExpectHTTPBuilder { - it.tests[it.countTests].Expect.ExecuteTime = t +func (qt *cute) ExpectExecuteTimeout(t time.Duration) ExpectHTTPBuilder { + qt.tests[qt.countTests].Expect.ExecuteTime = t - return it + return qt } -func (it *cute) ExpectStatus(code int) ExpectHTTPBuilder { - it.tests[it.countTests].Expect.Code = code +func (qt *cute) ExpectStatus(code int) ExpectHTTPBuilder { + qt.tests[qt.countTests].Expect.Code = code - return it + return qt } -func (it *cute) ExpectJSONSchemaString(schema string) ExpectHTTPBuilder { - it.tests[it.countTests].Expect.JSONSchema.String = schema +func (qt *cute) ExpectJSONSchemaString(schema string) ExpectHTTPBuilder { + qt.tests[qt.countTests].Expect.JSONSchema.String = schema - return it + return qt } -func (it *cute) ExpectJSONSchemaByte(schema []byte) ExpectHTTPBuilder { - it.tests[it.countTests].Expect.JSONSchema.Byte = schema +func (qt *cute) ExpectJSONSchemaByte(schema []byte) ExpectHTTPBuilder { + qt.tests[qt.countTests].Expect.JSONSchema.Byte = schema - return it + return qt } -func (it *cute) ExpectJSONSchemaFile(filePath string) ExpectHTTPBuilder { - it.tests[it.countTests].Expect.JSONSchema.File = filePath +func (qt *cute) ExpectJSONSchemaFile(filePath string) ExpectHTTPBuilder { + qt.tests[qt.countTests].Expect.JSONSchema.File = filePath - return it + return qt } -func (it *cute) AssertBody(asserts ...AssertBody) ExpectHTTPBuilder { +func (qt *cute) AssertBody(asserts ...AssertBody) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } } - it.tests[it.countTests].Expect.AssertBody = append(it.tests[it.countTests].Expect.AssertBody, asserts...) + qt.tests[qt.countTests].Expect.AssertBody = append(qt.tests[qt.countTests].Expect.AssertBody, asserts...) - return it + return qt } -func (it *cute) OptionalAssertBody(asserts ...AssertBody) ExpectHTTPBuilder { +func (qt *cute) OptionalAssertBody(asserts ...AssertBody) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } - it.tests[it.countTests].Expect.AssertBody = append(it.tests[it.countTests].Expect.AssertBody, optionalAssertBody(assert)) + qt.tests[qt.countTests].Expect.AssertBody = append(qt.tests[qt.countTests].Expect.AssertBody, optionalAssertBody(assert)) } - return it + return qt } -func (it *cute) RequireBody(asserts ...AssertBody) ExpectHTTPBuilder { +func (qt *cute) RequireBody(asserts ...AssertBody) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } - it.tests[it.countTests].Expect.AssertBody = append(it.tests[it.countTests].Expect.AssertBody, requireAssertBody(assert)) + qt.tests[qt.countTests].Expect.AssertBody = append(qt.tests[qt.countTests].Expect.AssertBody, requireAssertBody(assert)) } - return it + return qt } -func (it *cute) AssertHeaders(asserts ...AssertHeaders) ExpectHTTPBuilder { +func (qt *cute) AssertHeaders(asserts ...AssertHeaders) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } } - it.tests[it.countTests].Expect.AssertHeaders = append(it.tests[it.countTests].Expect.AssertHeaders, asserts...) + qt.tests[qt.countTests].Expect.AssertHeaders = append(qt.tests[qt.countTests].Expect.AssertHeaders, asserts...) - return it + return qt } -func (it *cute) OptionalAssertHeaders(asserts ...AssertHeaders) ExpectHTTPBuilder { +func (qt *cute) OptionalAssertHeaders(asserts ...AssertHeaders) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } - it.tests[it.countTests].Expect.AssertHeaders = append(it.tests[it.countTests].Expect.AssertHeaders, optionalAssertHeaders(assert)) + qt.tests[qt.countTests].Expect.AssertHeaders = append(qt.tests[qt.countTests].Expect.AssertHeaders, optionalAssertHeaders(assert)) } - return it + return qt } -func (it *cute) RequireHeaders(asserts ...AssertHeaders) ExpectHTTPBuilder { +func (qt *cute) RequireHeaders(asserts ...AssertHeaders) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } - it.tests[it.countTests].Expect.AssertHeaders = append(it.tests[it.countTests].Expect.AssertHeaders, requireAssertHeaders(assert)) + qt.tests[qt.countTests].Expect.AssertHeaders = append(qt.tests[qt.countTests].Expect.AssertHeaders, requireAssertHeaders(assert)) } - return it + return qt } -func (it *cute) AssertResponse(asserts ...AssertResponse) ExpectHTTPBuilder { +func (qt *cute) AssertResponse(asserts ...AssertResponse) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } } - it.tests[it.countTests].Expect.AssertResponse = append(it.tests[it.countTests].Expect.AssertResponse, asserts...) + qt.tests[qt.countTests].Expect.AssertResponse = append(qt.tests[qt.countTests].Expect.AssertResponse, asserts...) - return it + return qt } -func (it *cute) OptionalAssertResponse(asserts ...AssertResponse) ExpectHTTPBuilder { +func (qt *cute) OptionalAssertResponse(asserts ...AssertResponse) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } - it.tests[it.countTests].Expect.AssertResponse = append(it.tests[it.countTests].Expect.AssertResponse, optionalAssertResponse(assert)) + qt.tests[qt.countTests].Expect.AssertResponse = append(qt.tests[qt.countTests].Expect.AssertResponse, optionalAssertResponse(assert)) } - return it + return qt } -func (it *cute) RequireResponse(asserts ...AssertResponse) ExpectHTTPBuilder { +func (qt *cute) RequireResponse(asserts ...AssertResponse) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } - it.tests[it.countTests].Expect.AssertResponse = append(it.tests[it.countTests].Expect.AssertResponse, requireAssertResponse(assert)) + qt.tests[qt.countTests].Expect.AssertResponse = append(qt.tests[qt.countTests].Expect.AssertResponse, requireAssertResponse(assert)) } - return it + return qt } -func (it *cute) AssertBodyT(asserts ...AssertBodyT) ExpectHTTPBuilder { +func (qt *cute) AssertBodyT(asserts ...AssertBodyT) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } } - it.tests[it.countTests].Expect.AssertBodyT = append(it.tests[it.countTests].Expect.AssertBodyT, asserts...) + qt.tests[qt.countTests].Expect.AssertBodyT = append(qt.tests[qt.countTests].Expect.AssertBodyT, asserts...) - return it + return qt } -func (it *cute) OptionalAssertBodyT(asserts ...AssertBodyT) ExpectHTTPBuilder { +func (qt *cute) OptionalAssertBodyT(asserts ...AssertBodyT) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } - it.tests[it.countTests].Expect.AssertBodyT = append(it.tests[it.countTests].Expect.AssertBodyT, optionalAssertBodyT(assert)) + qt.tests[qt.countTests].Expect.AssertBodyT = append(qt.tests[qt.countTests].Expect.AssertBodyT, optionalAssertBodyT(assert)) } - return it + return qt } -func (it *cute) RequireBodyT(asserts ...AssertBodyT) ExpectHTTPBuilder { +func (qt *cute) RequireBodyT(asserts ...AssertBodyT) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } - it.tests[it.countTests].Expect.AssertBodyT = append(it.tests[it.countTests].Expect.AssertBodyT, requireAssertBodyT(assert)) + qt.tests[qt.countTests].Expect.AssertBodyT = append(qt.tests[qt.countTests].Expect.AssertBodyT, requireAssertBodyT(assert)) } - return it + return qt } -func (it *cute) AssertHeadersT(asserts ...AssertHeadersT) ExpectHTTPBuilder { +func (qt *cute) AssertHeadersT(asserts ...AssertHeadersT) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } } - it.tests[it.countTests].Expect.AssertHeadersT = append(it.tests[it.countTests].Expect.AssertHeadersT, asserts...) + qt.tests[qt.countTests].Expect.AssertHeadersT = append(qt.tests[qt.countTests].Expect.AssertHeadersT, asserts...) - return it + return qt } -func (it *cute) OptionalAssertHeadersT(asserts ...AssertHeadersT) ExpectHTTPBuilder { +func (qt *cute) OptionalAssertHeadersT(asserts ...AssertHeadersT) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } - it.tests[it.countTests].Expect.AssertHeadersT = append(it.tests[it.countTests].Expect.AssertHeadersT, optionalAssertHeadersT(assert)) + qt.tests[qt.countTests].Expect.AssertHeadersT = append(qt.tests[qt.countTests].Expect.AssertHeadersT, optionalAssertHeadersT(assert)) } - return it + return qt } -func (it *cute) RequireHeadersT(asserts ...AssertHeadersT) ExpectHTTPBuilder { +func (qt *cute) RequireHeadersT(asserts ...AssertHeadersT) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } - it.tests[it.countTests].Expect.AssertHeadersT = append(it.tests[it.countTests].Expect.AssertHeadersT, requireAssertHeadersT(assert)) + qt.tests[qt.countTests].Expect.AssertHeadersT = append(qt.tests[qt.countTests].Expect.AssertHeadersT, requireAssertHeadersT(assert)) } - return it + return qt } -func (it *cute) AssertResponseT(asserts ...AssertResponseT) ExpectHTTPBuilder { +func (qt *cute) AssertResponseT(asserts ...AssertResponseT) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } } - it.tests[it.countTests].Expect.AssertResponseT = append(it.tests[it.countTests].Expect.AssertResponseT, asserts...) + qt.tests[qt.countTests].Expect.AssertResponseT = append(qt.tests[qt.countTests].Expect.AssertResponseT, asserts...) - return it + return qt } -func (it *cute) OptionalAssertResponseT(asserts ...AssertResponseT) ExpectHTTPBuilder { +func (qt *cute) OptionalAssertResponseT(asserts ...AssertResponseT) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } - it.tests[it.countTests].Expect.AssertResponseT = append(it.tests[it.countTests].Expect.AssertResponseT, optionalAssertResponseT(assert)) + qt.tests[qt.countTests].Expect.AssertResponseT = append(qt.tests[qt.countTests].Expect.AssertResponseT, optionalAssertResponseT(assert)) } - return it + return qt } -func (it *cute) RequireResponseT(asserts ...AssertResponseT) ExpectHTTPBuilder { +func (qt *cute) RequireResponseT(asserts ...AssertResponseT) ExpectHTTPBuilder { for _, assert := range asserts { if assert == nil { panic(errorAssertIsNil) } - it.tests[it.countTests].Expect.AssertResponseT = append(it.tests[it.countTests].Expect.AssertResponseT, requireAssertResponseT(assert)) + qt.tests[qt.countTests].Expect.AssertResponseT = append(qt.tests[qt.countTests].Expect.AssertResponseT, requireAssertResponseT(assert)) } - return it + return qt } -func (it *cute) CreateTableTest() MiddlewareTable { - it.isTableTest = true +func (qt *cute) CreateTableTest() MiddlewareTable { + qt.isTableTest = true - return it + return qt } -func (it *cute) PutNewTest(name string, r *http.Request, expect *Expect) TableTest { +func (qt *cute) PutNewTest(name string, r *http.Request, expect *Expect) TableTest { // Validate, that first step is empty - if it.countTests == 0 { - if it.tests[0].Request.Base == nil && - len(it.tests[0].Request.Builders) == 0 { - it.tests[0].Expect = expect - it.tests[0].Name = name - it.tests[0].Request.Base = r - - return it + if qt.countTests == 0 { + if qt.tests[0].Request.Base == nil && + len(qt.tests[0].Request.Builders) == 0 { + qt.tests[0].Expect = expect + qt.tests[0].Name = name + qt.tests[0].Request.Base = r + + return qt } } - newTest := createDefaultTest(it.baseProps) + newTest := createDefaultTest(qt.baseProps) newTest.Expect = expect newTest.Name = name newTest.Request.Base = r - it.tests = append(it.tests, newTest) - it.countTests++ // async? + qt.tests = append(qt.tests, newTest) + qt.countTests++ // async? - return it + return qt } -func (it *cute) PutTests(params ...*Test) TableTest { +func (qt *cute) PutTests(params ...*Test) TableTest { for _, param := range params { // Validate, that first step is empty - if it.countTests == 0 { - if it.tests[0].Request.Base == nil && - len(it.tests[0].Request.Builders) == 0 { - it.tests[0] = param + if qt.countTests == 0 { + if qt.tests[0].Request.Base == nil && + len(qt.tests[0].Request.Builders) == 0 { + qt.tests[0] = param continue } } - it.tests = append(it.tests, param) - it.countTests++ + qt.tests = append(qt.tests, param) + qt.countTests++ } - return it + return qt } -func (it *cute) NextTest() NextTestBuilder { - it.countTests++ // async? +func (qt *cute) NextTest() NextTestBuilder { + qt.countTests++ // async? - it.tests = append(it.tests, createDefaultTest(it.baseProps)) + qt.tests = append(qt.tests, createDefaultTest(qt.baseProps)) - return it + return qt } diff --git a/cute.go b/cute.go index e1773da..3a55fa1 100644 --- a/cute.go +++ b/cute.go @@ -59,7 +59,7 @@ type allureLinks struct { tmsLinks []string } -func (it *cute) ExecuteTest(ctx context.Context, t tProvider) []ResultsHTTPBuilder { +func (qt *cute) ExecuteTest(ctx context.Context, t tProvider) []ResultsHTTPBuilder { var internalT allureProvider if t == nil { @@ -68,13 +68,13 @@ func (it *cute) ExecuteTest(ctx context.Context, t tProvider) []ResultsHTTPBuild stepCtx, isStepCtx := t.(provider.StepCtx) if isStepCtx { - return it.executeTestInsideStep(ctx, stepCtx) + return qt.executeTestsInsideStep(ctx, stepCtx) } tOriginal, ok := t.(*testing.T) if ok { newT := createAllureT(tOriginal) - if !it.isTableTest { + if !qt.isTableTest { defer newT.FinishTest() //nolint } @@ -86,27 +86,11 @@ func (it *cute) ExecuteTest(ctx context.Context, t tProvider) []ResultsHTTPBuild internalT = allureT } - if it.parallel { + if qt.parallel { internalT.Parallel() } - 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 + return qt.executeTests(ctx, internalT) } func createAllureT(t *testing.T) *common.Common { @@ -128,45 +112,65 @@ func createAllureT(t *testing.T) *common.Common { return newT } -func (it *cute) executeTest(ctx context.Context, allureProvider allureProvider) []ResultsHTTPBuilder { +// 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 <= it.countTests; i++ { - currentTest := it.tests[i] + 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 { + var ( + res = make([]ResultsHTTPBuilder, 0) + ) + + // Cycle for change number of Test + for i := 0; i <= qt.countTests; i++ { + currentTest := qt.tests[i] // Execute by new T for table tests - if it.isTableTest { + if qt.isTableTest { tableTestName := currentTest.Name allureProvider.Run(tableTestName, func(inT provider.T) { - inT.Title(tableTestName) // set current test name + // Set current test name + inT.Title(tableTestName) - resultTest := it.executeSingleTest(ctx, inT, currentTest) - res = append(res, resultTest) + res = append(res, qt.executeSingleTest(ctx, inT, currentTest)) }) } else { currentTest.Name = allureProvider.Name() // set labels - it.setAllureInformation(allureProvider) + qt.setAllureInformation(allureProvider) - resultTest := it.executeSingleTest(ctx, allureProvider, currentTest) - if resultTest != nil { - res = append(res, resultTest) - } + res = append(res, qt.executeSingleTest(ctx, allureProvider, currentTest)) } } return res } -func (it *cute) executeSingleTest(ctx context.Context, allureProvider allureProvider, currentTest *Test) ResultsHTTPBuilder { +func (qt *cute) executeSingleTest(ctx context.Context, allureProvider allureProvider, currentTest *Test) ResultsHTTPBuilder { allureProvider.Logf("Test start %v", currentTest.Name) - resT := currentTest.execute(ctx, allureProvider) + resT := currentTest.executeInsideAllure(ctx, allureProvider) if resT.IsFailed() { allureProvider.Fail() allureProvider.Logf("Test was failed %v", currentTest.Name) @@ -176,5 +180,8 @@ func (it *cute) executeSingleTest(ctx context.Context, allureProvider allureProv allureProvider.Logf("Test finished %v", currentTest.Name) + // Remove from base struct all asserts + currentTest.clearFields() + return resT } diff --git a/errors/error.go b/errors/error.go index c51761a..cac490c 100644 --- a/errors/error.go +++ b/errors/error.go @@ -5,6 +5,7 @@ const ( expectedField = "Expected" ) +// CuteError ... type CuteError interface { error WithNameError diff --git a/examples/two_step_test.go b/examples/two_step_test.go index 7a8ddcc..dbb4daf 100644 --- a/examples/two_step_test.go +++ b/examples/two_step_test.go @@ -7,8 +7,6 @@ import ( "context" "errors" "fmt" - "io" - "log" "net/http" "testing" "time" @@ -49,7 +47,9 @@ func Test_TwoSteps_1(t *testing.T) { func Test_TwoSteps_2_AllureRunner(t *testing.T) { runner.Run(t, "Test with two steps", func(t provider.T) { - test := cute.NewTestBuilder(). + testBuilder := cute.NewHTTPTestMaker().NewTestBuilder() + + testBuilder. Title("Test with two requests executed by allure-go"). Tag("two_steps"). Description("some_description"). @@ -61,17 +61,10 @@ func Test_TwoSteps_2_AllureRunner(t *testing.T) { ExpectStatus(http.StatusOK). ExecuteTest(context.Background(), t) - bodyBytes, err := io.ReadAll(test[0].GetHTTPResponse().Body) - if err != nil { - log.Fatal(err) - } - // process body - _ = string(bodyBytes) - - cute.NewTestBuilder(). + testBuilder. CreateStep("Request 2"). RequestBuilder( - cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"), + cute.WithURI("https://jsonplaceholder.typicode.com/posts/2/comments"), cute.WithMethod(http.MethodGet), ). ExpectExecuteTimeout(10*time.Second). diff --git a/roundtripper.go b/roundtripper.go index 7b6a42e..5966887 100644 --- a/roundtripper.go +++ b/roundtripper.go @@ -59,17 +59,21 @@ func (it *Test) doRequest(t T, baseReq *http.Request) (*http.Response, error) { // copy request, because body can be read once req, err := copyRequest(baseReq.Context(), baseReq) if err != nil { - return nil, err } resp, httpErr := it.httpClient.Do(req) - // Add information (method, host, curl) about request to Allure step - // should be after httpClient.Do, because in roundTripper request could be changed - err = addInformationRequest(t, resp.Request) + // BAD CODE. Need to copy body, because we can't read body again from resp.Request.Body. Problem is io.Reader + resp.Request.Body, baseReq.Body, err = utils.DrainBody(baseReq.Body) if err != nil { - t.Log("[ERROR] Could not log information about request. Error %v", err) + t.Log("[ERROR] Could not drain body from baseReq.Body. Error %v", err) + } + + // Add information (method, host, curl) about request to Allure step + // should be after httpClient.Do and from resp.Request, because in roundTripper request may be changed + if addErr := addInformationRequest(t, resp.Request); addErr != nil { + t.Log("[ERROR] Could not log information about request. Error %v", addErr) } if httpErr != nil { @@ -80,9 +84,8 @@ func (it *Test) doRequest(t T, baseReq *http.Request) (*http.Response, error) { // Add information (code, body, headers) about response to Allure step addInformationResponse(t, resp) - err = it.validateResponseCode(t, resp) - if err != nil { - return nil, err + if validErr := it.validateResponseCode(t, resp); validErr != nil { + return nil, validErr } } diff --git a/test.go b/test.go index 982284e..99574a8 100644 --- a/test.go +++ b/test.go @@ -57,7 +57,7 @@ type RequestRepeatPolitic struct { Delay time.Duration } -// Middleware is struct for execute something before or after test +// Middleware is struct for executeInsideAllure something before or after test type Middleware struct { After []AfterExecute AfterT []AfterExecuteT @@ -93,6 +93,7 @@ type ExpectJSONSchema struct { File string } +// Execute is common method for run test from builder func (it *Test) Execute(ctx context.Context, t tProvider) ResultsHTTPBuilder { var ( internalT allureProvider @@ -119,7 +120,7 @@ func (it *Test) Execute(ctx context.Context, t tProvider) ResultsHTTPBuilder { } internalT.Run(it.Name, func(inT provider.T) { - res = it.execute(ctx, inT) + res = it.executeInsideAllure(ctx, inT) }) return res @@ -164,21 +165,17 @@ func (it *Test) initEmptyFields() { } } +// executeInsideStep is method for start test with provider.StepCtx +// It's test inside the step 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 { +func (it *Test) executeInsideAllure(ctx context.Context, allureProvider allureProvider) ResultsHTTPBuilder { var ( resp *http.Response errs []error @@ -189,24 +186,19 @@ func (it *Test) execute(ctx context.Context, allureProvider allureProvider) Resu it.initEmptyFields() if it.AllureStep.Name != "" { - // Test with step + // Set name of test for results name = it.AllureStep.Name - // Execute Test - allureProvider.Logf("Start step %v", name) + // Execute test inside step resp, errs = it.startTestWithStep(ctx, allureProvider) - allureProvider.Logf("Finish step %v", name) } else { + // Execute Test - // Test without step resp, errs = it.startTest(ctx, allureProvider) } isFailedTest := it.processTestErrors(allureProvider, errs) - // Remove from base struct all asserts - it.clearFields() - return newTestResult(name, resp, isFailedTest, errs) } @@ -245,6 +237,7 @@ func (it *Test) processTestErrors(t internalT, errs []error) bool { if countNotOptionalErrors != 0 { t.Errorf("Test finished with %v errors", countNotOptionalErrors) + t.Fail() } return failTest @@ -257,6 +250,9 @@ func (it *Test) startTestWithStep(ctx context.Context, t internalT) (*http.Respo ) t.WithNewStep(it.AllureStep.Name, func(stepCtx provider.StepCtx) { + t.Logf("Start step %v", it.AllureStep.Name) + defer t.Logf("Finish step %v", it.AllureStep.Name) + resp, errs = it.startTest(ctx, stepCtx) if len(errs) != 0 { @@ -273,7 +269,7 @@ func (it *Test) startTest(ctx context.Context, t internalT) (*http.Response, []e err error ) - // CreateWithStep execute timer + // CreateWithStep executeInsideAllure timer if it.Expect.ExecuteTime == 0 { it.Expect.ExecuteTime = defaultExecuteTestTime }