### Install CUTE Go Package Source: https://github.com/ozontech/cute/blob/master/README.MD Installs the CUTE Go package using the go get command. This is the primary method for integrating CUTE into your Go projects. ```bash go get -u github.com/ozontech/cute ``` -------------------------------- ### Run CUTE Example Source: https://github.com/ozontech/cute/blob/master/README.MD Executes an example test suite using the make command. This is useful for quickly demonstrating CUTE's capabilities. ```bash make example ``` -------------------------------- ### Single-Step Test Example in Go Source: https://github.com/ozontech/cute/blob/master/README.MD Demonstrates a single-step HTTP GET request test using CUTE. It includes setting the URI, method, expected status code, and asserting JSON response fields. This example utilizes the `allure-go` library for report generation. ```go import ( "context" "net/http" "path" "testing" "time" "github.com/ozontech/cute" "github.com/ozontech/cute/asserts/json" ) func TestExample(t *testing.T) { cute.NewTestBuilder(). Title("Title"). Description("some_description"). Create(). RequestBuilder( cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"), cute.WithMethod(http.MethodGet), ). ExpectExecuteTimeout(10*time.Second). ExpectStatus(http.StatusOK). AssertBody( json.Equal("$[0].email", "Eliseo@gardner.biz"), json.Present("$[1].name"), ). ExecuteTest(context.Background(), t) } ``` -------------------------------- ### Install Allure Framework Source: https://github.com/ozontech/cute/blob/master/README.MD Installs the Allure command-line tool using Homebrew. Allure is used to generate and serve test reports, providing detailed insights into test execution. ```bash brew install allure ``` -------------------------------- ### Multi-Step Test Example in Go Source: https://github.com/ozontech/cute/blob/master/README.MD Illustrates a multi-step test scenario in Go using CUTE, where multiple HTTP requests are executed sequentially within a single test. It shows how to capture the response from the first request and use it in subsequent steps or for assertions. The example includes a `AfterTestExecute` hook to process the response. ```go import ( "context" "fmt" "net/http" "testing" "github.com/ozontech/cute" ) func Test_TwoSteps(t *testing.T) { responseCode := 0 // First step cute.NewTestBuilder(). Title("Test with two requests and parse body."). Tag("two_steps"). Create(). RequestBuilder( cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"), cute.WithMethod(http.MethodGet), ). ExpectStatus(http.StatusOK). NextTest(). // Execute after first step and parse response code AfterTestExecute(func(response *http.Response, errors []error) error { responseCode = response.StatusCode return nil }). // Second step Create(). RequestBuilder( cute.WithURI("https://jsonplaceholder.typicode.com/posts/2/comments"), cute.WithMethod(http.MethodDelete), ). ExecuteTest(context.Background(), t) fmt.Println("Response code from first request", responseCode) } ``` -------------------------------- ### Go: Custom AssertBody Implementation Source: https://github.com/ozontech/cute/blob/master/README.MD Provides an example of creating a custom assertion function for the response body in Go using the cute framework. This custom assertion checks if the response body is empty. ```Go func customAssertBody() cute.AssertBody { return func(bytes []byte) error { if len(bytes) == 0 { return errors.New("response body is empty") } return nil } } ``` -------------------------------- ### Create Custom Assert Body Function Source: https://github.com/ozontech/cute/blob/master/README.MD Provides an example of creating a custom assertion function for the response body. This function uses `require.GreaterOrEqual` to ensure the body has at least 100 bytes. ```go func customAssertBodyT() cute.AssertBodyT { return func(t cute.T, bytes []byte) error { require.GreaterOrEqual(t, len(bytes), 100) return nil } } ``` -------------------------------- ### Describe a Single Test Step with Assertions Source: https://github.com/ozontech/cute/blob/master/README.MD This Go code demonstrates defining a single test step within a suite. It includes setting up a request with headers, URL, and method, and then defining expectations for the response status, JSON schema, and body assertions. ```Go import ( "github.com/ozontech/cute" "github.com/ozontech/cute/asserts/headers" "github.com/ozontech/cute/asserts/json" ) func (i *ExampleSuite) TestExample_OneStep(t provider.T) { var ( testBuilder = i.testMaker.NewTestBuilder() ) u, _ := url.Parse(i.host.String()) u.Path = path.Join(u.Path, "/posts/1/comments") testBuilder. Title("TestExample_OneStep"). Tags("one_step", "some_local_tag", "json"). Create(). StepName("Example GET json request"). RequestBuilder( cute.WithHeaders(map[string][]string{ "some_header": []string{"something"}, "some_array_header": []string{"1", "2", "3", "some_thing"}, }), cute.WithURL(u), cute.WithMethod(http.MethodGet), ). ExpectExecuteTimeout(10*time.Second). ExpectJSONSchemaFile("file://./resources/example_valid_request.json"). ExpectStatus(http.StatusOK). AssertBody( json.Equal("$[0].email", "Eliseo@gardner.biz"), json.Present("$[1].name"), json.NotPresent("$[1].some_not_present"), json.GreaterThan("$", 3), json.Length("$", 5), json.LessThan("$", 100), json.NotEqual("$[3].name", "kekekekeke"), ). OptionalAssertBody( json.GreaterThan("$", 3), json.Length("$", 5), json.LessThan("$", 100), ). AssertHeaders( headers.Present("Content-Type"), ). ExecuteTest(context.Background(), t) } ``` -------------------------------- ### Serve Allure Reports Source: https://github.com/ozontech/cute/blob/master/README.MD Serves the generated Allure test reports from a specified directory. This command allows you to view the reports in a web browser. ```bash allure serve ./examples/allure-results ``` -------------------------------- ### Run a Test Suite Source: https://github.com/ozontech/cute/blob/master/README.MD This Go code snippet shows how to run a declared test suite using `suite.RunSuite`. It's a standard way to integrate custom test suites with testing frameworks. ```Go import ( "github.com/ozontech/allure-go/pkg/framework/suite" ) func TestExampleTest(t *testing.T) { suite.RunSuite(t, new(ExampleSuite)) } ``` -------------------------------- ### Go: Execute Array Test with Cute Source: https://github.com/ozontech/cute/blob/master/README.MD Demonstrates how to execute array tests using the cute testing framework in Go. It includes setting up test cases with different request methods and URIs, and defining expected responses with status codes and body assertions. ```Go func Test_Execute_Array(t *testing.T) { tests := []*cute.Test{ { Name: "test_1", Middleware: nil, Request: &cute.Request{ Builders: []cute.RequestBuilder{ cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"), cute.WithMethod(http.MethodPost), }, }, Expect: &cute.Expect{ Code: 200, }, }, { Name: "test_2", Middleware: nil, Request: &cute.Request{ Builders: []cute.RequestBuilder{ cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"), cute.WithMethod(http.MethodGet), }, }, Expect: &cute.Expect{ Code: 200, AssertBody: []cute.AssertBody{ json.Equal("$[0].email", "Eliseo@gardner.biz"), json.Present("$[1].name"), func(body []byte) error { return errors.NewAssertError("example error", "example message", nil, nil) }, }, }, }, } for _, test := range tests { test.Execute(context.Background(), t) } } ``` -------------------------------- ### Create Builder Table Tests Source: https://github.com/ozontech/cute/blob/master/README.MD This Go code illustrates how to create table tests using the `cute` builder pattern. It defines an array of test cases, each with a name, request details, and expected outcomes, and then executes them as a table test. ```Go import ( "context" "fmt" "net/http" "testing" "github.com/ozontech/cute" ) func Test_Table_Array(t *testing.T) { tests := []*cute.Test{ { Name: "test_1", Middleware: nil, Request: &cute.Request{ Builders: []cute.RequestBuilder{ cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"), cute.WithMethod(http.MethodPost), }, }, Expect: &cute.Expect{ Code: 200, }, }, { Name: "test_2", Middleware: nil, Request: &cute.Request{ Builders: []cute.RequestBuilder{ cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"), cute.WithMethod(http.MethodGet), }, }, Expect: &cute.Expect{ Code: 200, AssertBody: []cute.AssertBody{ json.Equal("$[0].email", "Eliseo@gardner.biz"), json.Present("$[1].name"), func(body []byte) error { return errors.NewAssertError("example error", "example message", nil, nil) }, }, }, }, } cute.NewTestBuilder(). Title("Example table test"). Tag("table_test"). Description("Execute array tests"). CreateTableTest(). PutTests(tests...). ExecuteTest(context.Background(), t) } ``` -------------------------------- ### Declare Suite Structure with HTTPTestMaker Source: https://github.com/ozontech/cute/blob/master/README.MD This Go code snippet demonstrates how to declare a test suite structure that embeds `suite.Suite` and includes an `*cute.HTTPTestMaker`. It also shows the `BeforeAll` method for initializing the test maker and parsing a host URL. ```Go import ( "github.com/ozontech/cute" "github.com/ozontech/allure-go/pkg/framework/provider" "github.com/ozontech/allure-go/pkg/framework/suite" ) type ExampleSuite struct { suite.Suite host *url.URL testMaker *cute.HTTPTestMaker } func (i *ExampleSuite) BeforeAll(t provider.T) { // Prepare http test builder i.testMaker = cute.NewHTTPTestMaker() // Preparing host host, err := url.Parse("https://jsonplaceholder.typicode.com/") if err != nil { t.Fatalf("could not parse url, error %w", err) } i.host = host } ``` -------------------------------- ### Go: JSON Schema Validation with Cute Source: https://github.com/ozontech/cute/blob/master/README.MD Illustrates how to perform JSON schema validations using the cute testing framework in Go. It shows three methods for providing the JSON schema: as a string, as a byte array, or from a file/URL. ```Go ExpectJSONSchemaString(string) ExpectJSONSchemaByte([]byte) ExpectJSONSchemaFile(string) ``` -------------------------------- ### Create Custom Assert Body with Allure Step Source: https://github.com/ozontech/cute/blob/master/README.MD Demonstrates creating a custom assertion for the response body that includes a custom Allure step. It checks if the body is empty, marks the step as failed, and adds an attachment if it is. ```go func customAssertBodySuite() cute.AssertBodyT { return func(t cute.T, bytes []byte) error { step := allure.NewSimpleStep("Custom assert step") defer func() { t.Step(step) }() if len(bytes) == 0 { step.Status = allure.Failed step.Attachment(allure.NewAttachment("Error", allure.Text, []byte("response body is empty"))) return nil } return nil } } ``` -------------------------------- ### Interface for Custom Error Fields Source: https://github.com/ozontech/cute/blob/master/README.MD Defines the `WithFields` interface, allowing custom errors to include additional parameters or fields, which can be used for richer Allure step attachments. ```go type WithFields interface { GetFields() map[string]interface{} PutFields(map[string]interface{}) } ``` -------------------------------- ### Interface for Optional Error Source: https://github.com/ozontech/cute/blob/master/README.MD Defines the `OptionalError` interface, which custom errors can implement to signal that they should be treated as optional failures. ```go type OptionalError interface { IsOptional() bool SetOptional(bool) } ``` -------------------------------- ### Create Custom Assert Body with Custom Error Source: https://github.com/ozontech/cute/blob/master/README.MD Shows how to create a custom assertion function that returns a custom error using `errors.NewAssertError`. This allows for detailed error messages including expected and actual values. ```go import ( "github.com/ozontech/cute" "github.com/ozontech/cute/errors" ) func customAssertBodyWithCustomError() cute.AssertBody { return func(bytes []byte) error { if len(bytes) == 0 { return errors.NewAssertError("customAssertBodyWithCustomError", "body must be not empty", "len is 0", "len more 0") } return nil } } ``` -------------------------------- ### Define Custom Assert Headers Type Source: https://github.com/ozontech/cute/blob/master/README.MD Defines the signature for a custom assertion function that checks HTTP headers. It takes a cute.T context and http.Header, returning an error on failure. ```go type AssertHeadersT func(t cute.T, headers http.Header) error ``` -------------------------------- ### Interface for Custom Error Name Source: https://github.com/ozontech/cute/blob/master/README.MD Defines the `WithNameError` interface, which custom errors can implement to provide a name for better identification in reports. ```go type WithNameError interface { GetName() string SetName(string) } ``` -------------------------------- ### Create Custom Assert Body with Optional Error Source: https://github.com/ozontech/cute/blob/master/README.MD Illustrates creating a custom assertion that returns an optional error using `errors.NewOptionalError`. This type of error causes the step to fail but does not mark the overall test as failed. ```go import ( "github.com/ozontech/cute" "github.com/ozontech/cute/errors" ) func customAssertBodyWithCustomError() cute.AssertBody { return func(bytes []byte) error { if len(bytes) == 0 { return errors.NewOptionalError("body is empty") } return nil } } ``` -------------------------------- ### Define Custom Assert Body Type Source: https://github.com/ozontech/cute/blob/master/README.MD Defines the signature for a custom assertion function that takes a cute.T context and a byte slice representing the response body, returning an error if the assertion fails. ```go type AssertBodyT func(t cute.T, body []byte) error ``` -------------------------------- ### Define Custom Assert Response Type Source: https://github.com/ozontech/cute/blob/master/README.MD Defines the signature for a custom assertion function that validates an entire HTTP response. It accepts a cute.T context and an http.Response pointer, returning an error if the assertion fails. ```go type AssertResponseT func(t cute.T, response *http.Response) error ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.