package phases import ( "context" "os" "testing" "gotest.tools/v3/assert" "edge-infra.dev/pkg/lib/fog" "edge-infra.dev/test/f2" ) var f f2.Framework // refer to the diagram here to help with visualizing the flow // https://wiki.edge-infra.dev/dev/testing/framework/#core-concepts // TestMain is used to setup the framework // https://pkg.go.dev/testing#hdr-Main func TestMain(m *testing.M) { f = f2.New(context.Background(), f2.WithExtensions()). // setup always runs before anything else Setup(func(ctx f2.Context) (f2.Context, error) { fog.FromContext(ctx).Info("setting up framework") return ctx, nil }). // all of the functions in TestMain can be duplicated // ill only show this one so this file doesnt get massive // you could also define none of these functions if theyre irrelevant // see the simple example Setup(func(ctx f2.Context) (f2.Context, error) { fog.FromContext(ctx).Info("setting up framework again") return ctx, nil }). // runs before each Test function is executed (ex: TestPhasesFeature) BeforeEachTest(func(ctx f2.Context, _ *testing.T) (f2.Context, error) { fog.FromContext(ctx).Info("before each test") return ctx, nil }). // runs before each feature BeforeEachFeature(func(ctx f2.Context, _ *testing.T, _ f2.Feature) (f2.Context, error) { fog.FromContext(ctx).Info("before each feature") return ctx, nil }). // runs after each feature AfterEachFeature(func(ctx f2.Context, _ *testing.T, _ f2.Feature) (f2.Context, error) { fog.FromContext(ctx).Info("after each feature") return ctx, nil }). // runs after a Test function AfterEachTest(func(ctx f2.Context, _ *testing.T) (f2.Context, error) { fog.FromContext(ctx).Info("after each test") return ctx, nil }). // teardown is called once all tests and features have been executed Teardown(func(ctx f2.Context) (f2.Context, error) { fog.FromContext(ctx).Info("tearing down framework") return ctx, nil }) os.Exit(f.Run(m)) } // one feature with multiple tests // //nolint:dupl func TestPhasesFeature(t *testing.T) { featureOne := f2.NewFeature("feature1"). Setup("setup feature1", func(ctx f2.Context, t *testing.T) f2.Context { t.Log("setting up feature 1") return ctx }). // you can define as many tests as you need Test("test 1", func(ctx f2.Context, t *testing.T) f2.Context { t.Log("running test 1") assert.Assert(t, true) return ctx }). Test("test 2", func(ctx f2.Context, t *testing.T) f2.Context { t.Log("running test 2") assert.Assert(t, true) return ctx }). Teardown("teardown feature1", func(ctx f2.Context, t *testing.T) f2.Context { t.Log("tearing down feature 1") return ctx }).Feature() f.Test(t, featureOne) } // multiple features with multiple tests // //nolint:dupl func TestPhasesFeature2(t *testing.T) { // note: BeforeEachTest and AfterEachTest only runs once for this function // even though theres two features defined featureTwo := f2.NewFeature("feature two"). Setup("setup feature2", func(ctx f2.Context, _ *testing.T) f2.Context { t.Log("setting up feature 2") return ctx }). Test("test 3", func(ctx f2.Context, t *testing.T) f2.Context { t.Log("running test 3") assert.Assert(t, true) return ctx }). Test("test 4", func(ctx f2.Context, t *testing.T) f2.Context { t.Log("running test 4") assert.Assert(t, true) return ctx }). Teardown("teardown feature2", func(ctx f2.Context, t *testing.T) f2.Context { t.Log("tearing down feature 2") return ctx }).Feature() featureThree := f2.NewFeature("feature 3"). // you dont have to setup or teardown // you could also have one and not the other Test("test 5", func(ctx f2.Context, t *testing.T) f2.Context { t.Log("running test 5") assert.Assert(t, true) return ctx }). Test("test 6", func(ctx f2.Context, t *testing.T) f2.Context { t.Log("running test 6") assert.Assert(t, true) return ctx }).Feature() // test the two features defined above f.Test(t, featureTwo, featureThree) }