forked from ozontech/allure-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_test.go
123 lines (105 loc) · 2.95 KB
/
setup_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
//go:build examples_new
// +build examples_new
package suite_demo
import (
"context"
"fmt"
"testing"
"github.com/jackc/fake"
"github.com/ozontech/allure-go/pkg/allure"
"github.com/ozontech/allure-go/pkg/framework/provider"
"github.com/ozontech/allure-go/pkg/framework/suite"
)
type Example struct {
country string
number int
}
func (e *Example) String() string {
return fmt.Sprintf("%s#%d", e.country, e.number)
}
type SetupSuite struct {
suite.Suite
ParamMyTest []*Example
}
func (s *SetupSuite) BeforeAll(t provider.T) {
var params []*allure.Parameter
for i := 0; i < 10; i++ {
param := &Example{
country: fake.Country(),
number: fake.Year(1900, 2000),
}
params = append(params, allure.NewParameter(fmt.Sprintf("Ex %d", i), param))
s.ParamMyTest = append(s.ParamMyTest, param)
}
t.NewStep("BeforeAllStep", params...)
}
func (s *SetupSuite) BeforeEach(t provider.T) {
t.Epic("Demo")
t.Feature("BeforeAfter")
t.NewStep("This Step will be before Each")
}
func (s *SetupSuite) AfterEach(t provider.T) {
t.NewStep("AfterEach Step")
}
func (s *SetupSuite) AfterAll(t provider.T) {
t.NewStep("AfterAll Step")
}
func (s *SetupSuite) TableTestMyTest(t provider.T, example *Example) {
t.Titlef("TableTest With Setup - %s", example)
t.Descriptionf(`
Test will unpack all data from passed parameter to the variables in WithTestSetup func.
After test finish, it will do ctx.Done() in TestTearDown.
All Setup and TearDown tests will be add as Befores and Afters to test's container.
Used Data: %s`, example)
t.Tags("Parametrized", "Parallel", "Setup", "BeforeAfter")
t.Parallel()
var (
country string
year int
ctx context.Context
)
t.WithTestSetup(func(t provider.T) {
t.WithNewStep("init country", func(sCtx provider.StepCtx) {
country = example.country
sCtx.WithNewParameters("country", country)
})
t.WithNewStep("init year", func(sCtx provider.StepCtx) {
year = example.number
sCtx.WithNewParameters("year", year)
})
t.WithNewStep("init ctx", func(sCtx provider.StepCtx) {
ctx = context.Background()
sCtx.WithNewParameters("ctx", ctx)
})
})
t.Require().NotEqual("PonyCountry", country, "No magic countries in the list")
t.Require().NotEqual(2007, year, "No one returned to 2007")
t.Require().NotNil(ctx, "Not empty context")
defer t.WithTestTeardown(func(t provider.T) {
t.WithNewStep("Close ctx", func(sCtx provider.StepCtx) {
ctx.Done()
sCtx.WithNewParameters("ctx", ctx)
})
})
}
func (s *SetupSuite) TestMyOtherTest(t provider.T) {
t.Title("Just Test WithSetup")
t.Description(`
Test will prepare some data at TestSetup.`)
t.Tags("Parallel", "Setup", "BeforeAfter")
t.Parallel()
var (
name string
age int
)
t.WithTestSetup(func(t provider.T) {
t.WithNewStep("init args", func(sCtx provider.StepCtx) {
name = fake.FullName()
age = fake.Day()
sCtx.WithNewParameters("name", name, "age", age)
})
})
}
func TestRunner(t *testing.T) {
suite.RunSuite(t, new(SetupSuite))
}