...

Source file src/edge-infra.dev/test/f2/examples/phases/phases_test.go

Documentation: edge-infra.dev/test/f2/examples/phases

     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  // refer to the diagram here to help with visualizing the flow
    17  // https://wiki.edge-infra.dev/dev/testing/framework/#core-concepts
    18  
    19  // TestMain is used to setup the framework
    20  // https://pkg.go.dev/testing#hdr-Main
    21  func TestMain(m *testing.M) {
    22  	f = f2.New(context.Background(), f2.WithExtensions()).
    23  		// setup always runs before anything else
    24  		Setup(func(ctx f2.Context) (f2.Context, error) {
    25  			fog.FromContext(ctx).Info("setting up framework")
    26  			return ctx, nil
    27  		}).
    28  		// all of the functions in TestMain can be duplicated
    29  		// ill only show this one so this file doesnt get massive
    30  		// you could also define none of these functions if theyre irrelevant
    31  		// see the simple example
    32  		Setup(func(ctx f2.Context) (f2.Context, error) {
    33  			fog.FromContext(ctx).Info("setting up framework again")
    34  			return ctx, nil
    35  		}).
    36  		// runs before each Test function is executed (ex: TestPhasesFeature)
    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  		// runs before each feature
    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  		// runs after each feature
    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  		// runs after a Test function
    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  		// teardown is called once all tests and features have been executed
    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  // one feature with multiple tests
    66  //
    67  //nolint:dupl
    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  		// you can define as many tests as you need
    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  // multiple features with multiple tests
    96  //
    97  //nolint:dupl
    98  func TestPhasesFeature2(t *testing.T) {
    99  	// note: BeforeEachTest and AfterEachTest only runs once for this function
   100  	//       even though theres two features defined
   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  		// you dont have to setup or teardown
   125  		// you could also have one and not the other
   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  	// test the two features defined above
   140  	f.Test(t, featureTwo, featureThree)
   141  }
   142  

View as plain text