...

Source file src/edge-infra.dev/test/f2/examples/integrationlevels/levels_test.go

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

     1  // Package integrationlevels demonstrates how tests can conditionally change behavior based on the integration test level
     2  package integrationlevels
     3  
     4  import (
     5  	"context"
     6  	"os"
     7  	"testing"
     8  
     9  	"gotest.tools/v3/assert"
    10  
    11  	"edge-infra.dev/test/f2"
    12  	"edge-infra.dev/test/f2/integration"
    13  )
    14  
    15  // you can use flags to run these tests
    16  // bazel test //test/f2/examples/integrationlevels/... --test_arg=-integration-level=2
    17  
    18  // or you can use the test/config.json file
    19  // ex:
    20  //	{
    21  //	  "integration-level": 1
    22  //	}
    23  
    24  var f f2.Framework
    25  
    26  func TestMain(m *testing.M) {
    27  	f = f2.New(context.Background())
    28  	os.Exit(f.Run(m))
    29  }
    30  
    31  func TestSkipIfNot(t *testing.T) {
    32  	// You can skip an entire Test function
    33  	// if the integration level isnt what you expect
    34  	integration.SkipIfNot(t, integration.L2)
    35  
    36  	fin := f2.NewFeature("SkipIfNot").
    37  		Test("test skip if not", func(ctx f2.Context, t *testing.T) f2.Context {
    38  			t.Log("in SkipIfNot", "level", 2)
    39  			assert.Assert(t, true)
    40  
    41  			return ctx
    42  		}).Feature()
    43  
    44  	f.Test(t, fin)
    45  }
    46  
    47  func TestSkipIf(t *testing.T) {
    48  	fin := f2.NewFeature("SkipIf").
    49  		Test("test skip if", func(ctx f2.Context, t *testing.T) f2.Context {
    50  
    51  			// or you can skip a specific test
    52  			integration.SkipIf(t, integration.L2)
    53  
    54  			t.Log("in SkipIf", "level", 1)
    55  			assert.Assert(t, true)
    56  
    57  			return ctx
    58  		}).Feature()
    59  
    60  	f.Test(t, fin)
    61  }
    62  
    63  func TestLevels(t *testing.T) {
    64  	fin := f2.NewFeature("Levels").
    65  		Test("test levels", func(ctx f2.Context, t *testing.T) f2.Context {
    66  
    67  			// If your test requires different functionality depending
    68  			// on the integration level
    69  			// ie:
    70  			// level 1 needs to create an embedded postgres database
    71  			if integration.IsL1() {
    72  				t.Log("in TestLevels", "level", 1)
    73  			}
    74  			// level 2 needs to connect to a live postgres database
    75  			if integration.IsL2() {
    76  				t.Log("in TestLevels", "level", 2)
    77  			}
    78  			assert.Assert(t, true)
    79  
    80  			return ctx
    81  		}).Feature()
    82  
    83  	f.Test(t, fin)
    84  }
    85  

View as plain text