-
Notifications
You must be signed in to change notification settings - Fork 18
/
single_test.go
195 lines (176 loc) · 4.89 KB
/
single_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//go:build example
// +build example
package examples
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"path"
"testing"
"time"
"github.com/ozontech/allure-go/pkg/allure"
"github.com/ozontech/allure-go/pkg/framework/provider"
"github.com/ozontech/allure-go/pkg/framework/runner"
cuteErrors "github.com/ozontech/cute/errors"
"github.com/ozontech/cute"
"github.com/ozontech/cute/asserts/json"
)
func Test_Single_1(t *testing.T) {
cute.NewTestBuilder().
Title("Single test with default T").
Tag("single_test").
Description("some_description").
Parallel().
Create().
RequestRetry(3).
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
cute.WithMarshalBody(struct {
Name string `json:"name"`
}{
Name: "Vasya Pupkin",
}),
cute.WithQueryKV("socks", "42"),
cute.WithMethod(http.MethodGet),
).
ExpectExecuteTimeout(10*time.Second).
ExpectStatus(http.StatusOK).
AssertBody(json.Diff("{\"aaa\":\"bb\"}")).
AssertBody(
json.Present("$[1].name"),
json.Present("$[0].passport"), // Example fail
json.Equal("$[0].email", "Eliseo@gardner.biz"),
CustomAssertBody(),
).
AssertBodyT(func(t cute.T, body []byte) error {
t.Step(allure.NewSimpleStep("inside Assert body. 1 ", allure.NewParameters("key", "value")...))
return nil
}).
After(
func(response *http.Response, errors []error) error {
b, err := io.ReadAll(response.Body)
if err != nil {
return err
}
email, err := json.GetValueFromJSON(b, "$[0].email")
if err != nil {
return err
}
fmt.Println("Email from test", email)
return nil
},
).
ExecuteTest(context.Background(), t)
}
func Test_Single_Broken(t *testing.T) {
cute.NewTestBuilder().
Title("Test_Single_Broken").
Create().
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
).
BrokenAssertBodyT(func(t cute.T, body []byte) error {
return errors.New("example broken error")
}).
ExpectStatus(http.StatusOK).
NextTest().
Create().
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
).
AssertBody(func(body []byte) error {
return errors.New("it's NOT must be run")
},
).
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().
RequestRetry(2).
RequestRetryOptional(true).
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
).
BrokenAssertBodyT(func(t cute.T, body []byte) error {
return errors.New("example broken error")
}).
ExpectStatus(http.StatusCreated).
ExecuteTest(context.Background(), t)
t.Logf("You should see it")
}
func Test_Single_RepeatPolitic_Broken_Failed_Test(t *testing.T) {
cute.NewTestBuilder().
Title("Test_Single_RepeatPolitic_Broken_Failed_Test").
Create().
RequestRetry(2).
RequestRetryOptional(false).
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
).
BrokenAssertBodyT(func(t cute.T, body []byte) error {
return errors.New("example broken error")
}).
ExpectStatus(http.StatusCreated).
ExecuteTest(context.Background(), t)
t.Logf("You should see it")
}
func Test_Single_Broken_2(t *testing.T) {
cute.NewTestBuilder().
Title("Test_Single_Broken_2").
Create().
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
).
AssertBodyT(func(t cute.T, body []byte) error {
err := errors.New("example broken error")
return cuteErrors.WrapBrokenError(err)
}).
ExpectStatus(http.StatusOK).
NextTest().
Create().
RequestBuilder(
cute.WithURI("https://jsonplaceholder.typicode.com/posts/1/comments"),
).
AssertBody(func(body []byte) error {
return errors.New("it's NOT must be run")
},
).
ExecuteTest(context.Background(), t)
}
func Test_Single_2_AllureRunner(t *testing.T) {
runner.Run(t, "Single test with allure-go Runner", func(t provider.T) {
var (
testMaker = cute.NewHTTPTestMaker()
testBuilder = testMaker.NewTestBuilder()
)
u, _ := url.Parse("https://jsonplaceholder.typicode.com/")
u.Path = path.Join(u.Path, "/posts/1/comments")
testBuilder.
Title("Single test with allure.T and repeat errors").
Tag("single_test").
Description("some_description").
Create().
RequestRetryDelay(3*time.Second). // delay before new try
RequestRetry(3). // count attempts
RequestBuilder(
cute.WithURL(u),
cute.WithMethod(http.MethodGet),
).
ExpectExecuteTimeout(10*time.Second).
ExpectStatus(http.StatusBadGateway).
AssertBody(
json.Equal("$[0].email", "Eliseo@gardner.biz"),
json.Present("$[1].name"),
).
OptionalAssertBody(
json.Present("$[0].photo"), // Example optional fail
).
ExecuteTest(context.Background(), t)
})
}