|
| 1 | +# Fail Fast Mode |
| 2 | + |
| 3 | +There are times in your test infra that you want the rest of your feature assessments to fail in |
| 4 | +case if one of the current executing assessments fail. |
| 5 | +This can aid in getting a better turn around time especially in cases where your assessments are |
| 6 | +inter-related and a failed assessment in step 1 can mean that running the rest of the assessment |
| 7 | +is guaranteed to fail. This can be done with the help of a `--fail-fast` flag provided at the |
| 8 | +framework level. |
| 9 | + |
| 10 | +This works similar to how the `-failfast` mode of the `go test` works but provides the same |
| 11 | +feature at the context of the `e2e-framework`. |
| 12 | + |
| 13 | +# How to Use this feature ? |
| 14 | + |
| 15 | +1. Invoke the tests using `--fail-fast` argument |
| 16 | +2. Test developers should make sure they invoke either `t.Fail()` or `t.FailNow()` from the assessment to make sure the |
| 17 | +additional handlers kick in to stop the test execution of the feature in question where the assessment has failed |
| 18 | + |
| 19 | + |
| 20 | +When the framework specific `--fail-fast` mode is used, this works as follows: |
| 21 | + |
| 22 | +1. It stops the rest of the assessments from getting executed for the feature under test |
| 23 | +2. This stops the next feature from getting executed in case if the feature under test fails as per step 1. |
| 24 | +3. Marks the feature and test associated with it as Failure. |
| 25 | +4. Skips the teardown sequence to make sure it is easier to debug the test failure |
| 26 | + |
| 27 | +> Current limitation is that this can't be combined with the `--parallel` switch |
| 28 | +
|
| 29 | +Since this can lead to a test failure, we have just documented an example of this. Thanks to @divmadan for the example. |
| 30 | + |
| 31 | +```go |
| 32 | +// main_test.go |
| 33 | +package example |
| 34 | + |
| 35 | +import ( |
| 36 | + "log" |
| 37 | + "os" |
| 38 | + "path/filepath" |
| 39 | + "testing" |
| 40 | + |
| 41 | + "sigs.k8s.io/e2e-framework/pkg/env" |
| 42 | +) |
| 43 | + |
| 44 | +var testenv env.Environment |
| 45 | + |
| 46 | +func TestMain(m *testing.M) { |
| 47 | + cfg, _ := envconf.NewFromFlags() |
| 48 | + testenv = env.NewWithConfig(cfg) |
| 49 | + |
| 50 | + os.Exit(testenv.Run(m)) |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +```go |
| 55 | +// example_test.go |
| 56 | +package example |
| 57 | + |
| 58 | +import ( |
| 59 | + "context" |
| 60 | + "testing" |
| 61 | + |
| 62 | + "sigs.k8s.io/e2e-framework/pkg/envconf" |
| 63 | + "sigs.k8s.io/e2e-framework/pkg/features" |
| 64 | +) |
| 65 | + |
| 66 | +func TestExample(t *testing.T) { |
| 67 | + failFeature := features.New("fail-feature"). |
| 68 | + Assess("1==2", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 69 | + if 1 != 2 { |
| 70 | + t.Log("1 != 2") |
| 71 | + t.FailNow() // mark test case as failed here, don't continue execution |
| 72 | + } else { |
| 73 | + t.Log("1 == 2") |
| 74 | + } |
| 75 | + return ctx |
| 76 | + }). |
| 77 | + Assess("print", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 78 | + t.Log("THIS LINE SHOULDN'T BE PRINTED") |
| 79 | + return ctx |
| 80 | + }). |
| 81 | + Teardown(func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { |
| 82 | + t.Log("This teardown should not be invoked") |
| 83 | + return ctx |
| 84 | + }). |
| 85 | + Feature() |
| 86 | + |
| 87 | + nextFeature := features.New("next-feature"). |
| 88 | + Assess("print", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 89 | + t.Log("THIS LINE ALSO SHOULDN'T BE PRINTED") |
| 90 | + return ctx |
| 91 | + }). |
| 92 | + Feature() |
| 93 | + |
| 94 | + testenv.Test(t, failFeature, nextFeature) |
| 95 | +} |
| 96 | + |
| 97 | +// even if the previous testcase fails, execute this testcase |
| 98 | +func TestNext(t *testing.T) { |
| 99 | + nextFeature := features.New("next-test-feature"). |
| 100 | + Assess("print", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context { |
| 101 | + t.Log("THIS LINE SHOULD BE PRINTED") |
| 102 | + return ctx |
| 103 | + }). |
| 104 | + Teardown(func(ctx context.Context, t *testing.T, c *envconf.Config) context.Context { |
| 105 | + t.Log("This teardown should be invoked") |
| 106 | + return ctx |
| 107 | + }). |
| 108 | + Feature() |
| 109 | + |
| 110 | + testenv.Test(t, nextFeature) |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +When run this using `--fail-fast` you get the following behavior. |
| 115 | + |
| 116 | +```bash |
| 117 | +❯ go test . -test.v -args --fail-fast |
| 118 | + |
| 119 | +=== RUN TestExample |
| 120 | +=== RUN TestExample/fail-feature |
| 121 | +=== RUN TestExample/fail-feature/1==2 |
| 122 | + example_test.go:15: 1 != 2 |
| 123 | +--- FAIL: TestExample (0.00s) |
| 124 | + --- FAIL: TestExample/fail-feature (0.00s) |
| 125 | + --- FAIL: TestExample/fail-feature/1==2 (0.00s) |
| 126 | +=== RUN TestNext |
| 127 | +=== RUN TestNext/next-test-feature |
| 128 | +=== RUN TestNext/next-test-feature/print |
| 129 | + example_test.go:42: THIS LINE SHOULD BE PRINTED |
| 130 | +--- PASS: TestNext (0.00s) |
| 131 | + --- PASS: TestNext/next-test-feature (0.00s) |
| 132 | + --- PASS: TestNext/next-test-feature/print (0.00s) |
| 133 | +FAIL |
| 134 | +``` |
0 commit comments