1 package phases
2
3 import (
4 "context"
5 "os"
6 "testing"
7
8 "gotest.tools/v3/assert"
9
10 "edge-infra.dev/pkg/lib/fog"
11 "edge-infra.dev/test/f2"
12 )
13
14 var f f2.Framework
15
16
17
18
19
20
21 func TestMain(m *testing.M) {
22 f = f2.New(context.Background(), f2.WithExtensions()).
23
24 Setup(func(ctx f2.Context) (f2.Context, error) {
25 fog.FromContext(ctx).Info("setting up framework")
26 return ctx, nil
27 }).
28
29
30
31
32 Setup(func(ctx f2.Context) (f2.Context, error) {
33 fog.FromContext(ctx).Info("setting up framework again")
34 return ctx, nil
35 }).
36
37 BeforeEachTest(func(ctx f2.Context, _ *testing.T) (f2.Context, error) {
38 fog.FromContext(ctx).Info("before each test")
39 return ctx, nil
40 }).
41
42 BeforeEachFeature(func(ctx f2.Context, _ *testing.T, _ f2.Feature) (f2.Context, error) {
43 fog.FromContext(ctx).Info("before each feature")
44 return ctx, nil
45 }).
46
47 AfterEachFeature(func(ctx f2.Context, _ *testing.T, _ f2.Feature) (f2.Context, error) {
48 fog.FromContext(ctx).Info("after each feature")
49 return ctx, nil
50 }).
51
52 AfterEachTest(func(ctx f2.Context, _ *testing.T) (f2.Context, error) {
53 fog.FromContext(ctx).Info("after each test")
54 return ctx, nil
55 }).
56
57 Teardown(func(ctx f2.Context) (f2.Context, error) {
58 fog.FromContext(ctx).Info("tearing down framework")
59 return ctx, nil
60 })
61
62 os.Exit(f.Run(m))
63 }
64
65
66
67
68 func TestPhasesFeature(t *testing.T) {
69 featureOne := f2.NewFeature("feature1").
70 Setup("setup feature1", func(ctx f2.Context, t *testing.T) f2.Context {
71 t.Log("setting up feature 1")
72 return ctx
73 }).
74
75 Test("test 1", func(ctx f2.Context, t *testing.T) f2.Context {
76 t.Log("running test 1")
77 assert.Assert(t, true)
78
79 return ctx
80 }).
81 Test("test 2", func(ctx f2.Context, t *testing.T) f2.Context {
82 t.Log("running test 2")
83 assert.Assert(t, true)
84
85 return ctx
86 }).
87 Teardown("teardown feature1", func(ctx f2.Context, t *testing.T) f2.Context {
88 t.Log("tearing down feature 1")
89 return ctx
90 }).Feature()
91
92 f.Test(t, featureOne)
93 }
94
95
96
97
98 func TestPhasesFeature2(t *testing.T) {
99
100
101 featureTwo := f2.NewFeature("feature two").
102 Setup("setup feature2", func(ctx f2.Context, _ *testing.T) f2.Context {
103 t.Log("setting up feature 2")
104 return ctx
105 }).
106 Test("test 3", func(ctx f2.Context, t *testing.T) f2.Context {
107 t.Log("running test 3")
108 assert.Assert(t, true)
109
110 return ctx
111 }).
112 Test("test 4", func(ctx f2.Context, t *testing.T) f2.Context {
113 t.Log("running test 4")
114 assert.Assert(t, true)
115
116 return ctx
117 }).
118 Teardown("teardown feature2", func(ctx f2.Context, t *testing.T) f2.Context {
119 t.Log("tearing down feature 2")
120 return ctx
121 }).Feature()
122
123 featureThree := f2.NewFeature("feature 3").
124
125
126 Test("test 5", func(ctx f2.Context, t *testing.T) f2.Context {
127 t.Log("running test 5")
128 assert.Assert(t, true)
129
130 return ctx
131 }).
132 Test("test 6", func(ctx f2.Context, t *testing.T) f2.Context {
133 t.Log("running test 6")
134 assert.Assert(t, true)
135
136 return ctx
137 }).Feature()
138
139
140 f.Test(t, featureTwo, featureThree)
141 }
142
View as plain text