Skip to content

Commit

Permalink
Add some docs
Browse files Browse the repository at this point in the history
  • Loading branch information
siller174 committed Sep 14, 2022
1 parent b9d2e34 commit dd747f7
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 15 deletions.
Binary file added .images/multistep_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .images/table_tests_execute_array.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .images/table_tests_execute_array_test_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
181 changes: 181 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ Three steps for testing your HTTP service:
5. [Examples](#examples)
1. [Single test with Allure](#example_single_test)
2. [Suite tests](#example_suite_test)
3. [Multi-step test](#example_multi_step_test)
4. [Table tests](#example_table_tests)
1. [Builder](#example_table_tests_builder)
2. [Array](#example_table_tests_builder)
6. [Asserts](#asserts)
1. [JSON asserts](#asserts_json)
2. [Headers asserts](#asserts_headers)
Expand Down Expand Up @@ -218,6 +222,183 @@ See full example [here](examples/suite)

![one_step.png](.images/one_step.png)


## <a name="example_multi_step_test"/>Multi-step test

```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)
}
```
See full example [here](examples/two_step_test.go)

**Allure:**

![multistep_test.png](.images/multistep_test.png)


## <a name="example_table_tests"/>Table tests

One step to table tests...

You have 2 ways to create table test. These ways have same allure reports.

### <a name="example_table_tests_builder"/>Builder table tests

```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", "[email protected]"),
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)
}
```


### <a name="example_table_tests_array"/>Array tests

```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", "[email protected]"),
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)
}
}
```

See full example [here](examples/table_test/table_test.go)

**Common allure for all table tests:**

Report has **2 different** tests/suites:

![table_tests_execute_array.png](.images/table_tests_execute_array.png)

**Main report:**

![table_tests_execute_array_test_1.png](.images/table_tests_execute_array_test_1.png)

## <a name="asserts"/>Asserts

You can create your own asserts or use ready-made asserts from the package asserts
Expand Down
8 changes: 4 additions & 4 deletions examples/table_test/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Test_Table(t *testing.T) {
func Test_Table_Array(t *testing.T) {
tests := []*cute.Test{
{
Name: "test_1",
Name: "Create something",
Middleware: nil,
Request: &cute.Request{
Builders: []cute.RequestBuilder{
Expand All @@ -66,7 +66,7 @@ func Test_Table_Array(t *testing.T) {
},
},
{
Name: "test_2",
Name: "Delete something",
Middleware: nil,
Request: &cute.Request{
Builders: []cute.RequestBuilder{
Expand Down Expand Up @@ -96,7 +96,7 @@ func Test_Table_Array(t *testing.T) {
ExecuteTest(context.Background(), t)
}

func Test_Execute(t *testing.T) {
func Test_One_Execute(t *testing.T) {
test := &cute.Test{
Name: "test_1",
Request: &cute.Request{
Expand All @@ -111,7 +111,7 @@ func Test_Execute(t *testing.T) {
test.Execute(context.Background(), t)
}

func Test_Execute_Array(t *testing.T) {
func Test_Array(t *testing.T) {
tests := []*cute.Test{
{
Name: "test_1",
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/moul/http2curl v1.0.0 h1:dRMWoAtb+ePxMlLkrCbAqh4TlPHXvoGUSQ323/9Zahs=
github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ=
github.com/ozontech/allure-go/pkg/allure v0.6.3 h1:+nGs+2Kq+XprmONt4xww+MEDjnShkLK6kPtfOknernY=
github.com/ozontech/allure-go/pkg/allure v0.6.3/go.mod h1:xyVZ+6tLDzQ4pr19eqLR0EckaI51kYUC1A5ihsDTygo=
github.com/ozontech/allure-go/pkg/framework v0.6.17 h1:XCnysk4udjqR4EzJzYFRvmTtwNDkyiLYBo4mnYi80C8=
github.com/ozontech/allure-go/pkg/framework v0.6.17/go.mod h1:RxbhHdppAf70AST+S0G3iMDxkHcNb11ygMaXISF3m1A=
github.com/ozontech/allure-go/pkg/allure v0.6.4 h1:lQ2NJSgl3sj/0oi4HTG+8EXkV1A+wWN7WIfDkOaBtmY=
github.com/ozontech/allure-go/pkg/allure v0.6.4/go.mod h1:xyVZ+6tLDzQ4pr19eqLR0EckaI51kYUC1A5ihsDTygo=
github.com/ozontech/allure-go/pkg/framework v0.6.18 h1:QoYFww5gMu3qPaoVDVetBa17ahKTHNYEhbyUIP+qYn8=
github.com/ozontech/allure-go/pkg/framework v0.6.18/go.mod h1:QNKRZHCSCxJHSBuJssp00Fc7EL5XvVX/IEJNSgxBN18=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
18 changes: 11 additions & 7 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ var (
errorRequestURLEmpty = errors.New("url request must be not empty")
)

// Test TODO
// Test is a main struct of test.
// You may field Request and Expect for create simple test
type Test struct {
httpClient *http.Client

Expand All @@ -39,33 +40,35 @@ type Test struct {
Expect *Expect
}

// Request TODO
// Request is struct with HTTP request.
// You may use your *http.Request or create new with help Builders
type Request struct {
Base *http.Request
Builders []RequestBuilder
Repeat *RequestRepeatPolitic
}

// RequestRepeatPolitic TODO
// RequestRepeatPolitic is struct for repeat politic
// If response.Code != Expect.Code, than request will repeat Count counts with Delay delay.
type RequestRepeatPolitic struct {
Count int
Delay time.Duration
}

// Middleware TODO
// Middleware is struct for execute something before or after test
type Middleware struct {
After []AfterExecute
AfterT []AfterExecuteT
Before []BeforeExecute
BeforeT []BeforeExecuteT
}

// AllureStep TODO
// AllureStep is struct with test name
type AllureStep struct {
Name string
}

// Expect TODO
// Expect is structs with validate politics for response
type Expect struct {
ExecuteTime time.Duration

Expand All @@ -81,7 +84,7 @@ type Expect struct {
AssertResponseT []AssertResponseT
}

// ExpectJSONSchema TODO
// ExpectJSONSchema is structs with JSON politics for response
type ExpectJSONSchema struct {
String string
Byte []byte
Expand Down Expand Up @@ -113,6 +116,7 @@ func (it *Test) Execute(ctx context.Context, t testing.TB) ResultsHTTPBuilder {

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

if it.AllureStep == nil {
it.AllureStep = new(AllureStep)
}
Expand Down

0 comments on commit dd747f7

Please sign in to comment.