Closed shivaDS closed 4 months ago
Hi @shivaDS
testenv.BeforeEachTest
is executed right before defined Go test functions.
So, for you to see BeforeEachTest
executed, you need to define a Go test function, which I don't see in your example.
For instance, when you run the following example,
the framework will execute your BeforeEachTest
before it runs test function TestSomeCode
:
var testenv env.Env
func TestMain(m *testing.M) {
cfg, err := envconf.NewFromFlags()
if err != nil {
log.Fatalf("failed to load env config: %s", err)
}
testenv = env.NewWithConfig(cfg)
testenv.Setup(
envfuncs.InstallTanzuCli(),
)
testenv.Finish()
testenv.BeforeEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) {
t.Log("Its before test function")
return ctx, nil
})
os.Exit(testenv.Run(m))
}
func TestSomeCode(t *testing.T){
<insert-test-code>
}
package log_test
import (
"context"
"envfuncs"
//"log"
"os"
"testing"
"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
)
var testenv env.Environment
func TestMain(m *testing.M) {
testenv = env.New()
testenv.Setup(
envfuncs.InstallTanzuCli(),
)
testenv.Finish()
testenv.BeforeEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) {
t.Log("Its before test function")
return ctx, nil
})
os.Exit(testenv.Run(m))
}
func TestSingleclusterInnerloopBasicSupplychainLocalSource(t *testing.T) {
t.Log("its test case")
}
module LogsPackage
go 1.19
require sigs.k8s.io/e2e-framework v0.3.0
hi @vladimirvivien I have added the complete code. Here the log in BeforeEachTest is not getting executed with both testing logger and "log" package logger
Hi @shivaDS
Thanks for the your example. As I explained, the BeforeEachTest
callback will get executed right before the TestSingleclusterInnerloopBasicSupplychainLocalSource
test function is executed.
Specifically for your example, you should see "Its before test function"
prior to "its test case"
.
When you run the test, make sure to use the -v
to see log output:
go test -v .
Otherwise, you will not see the t.Log output.
Hope that helps.
Hi @vladimirvivien , Thanks for your inputs. I am running using the command go test . -v Still I couldn't see that log.
package log_test
import (
"context"
"envfuncs"
"os"
"testing"
"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
)
var testenv env.Environment
func TestMain(m *testing.M) {
testenv = env.New()
testenv.Setup(
envfuncs.InstallTanzuCli(),
)
testenv.Finish()
testenv.BeforeEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) {
t.Log("Its before test function")
return ctx, nil
})
os.Exit(testenv.Run(m))
}
func TestSingleclusterInnerloopBasicSupplychainLocalSource(t *testing.T) {
t.Log("its test case")
}
// func setupTest(t *testing.T) func(t *testing.T) {
// log.Println("setup test")
// return func(t *testing.T) {
// log.Println("teardown test")
// }
// }
// func createNSForTest(ctx context.Context, cfg *envconf.Config, t *testing.T, runID string) (context.Context, error) {
// ns := envconf.RandomName(runID, 10)
// // ctx = context.WithValue(ctx, GetNamespaceKey(t), ns)
// t.Logf("Creating NS %v for test %v", ns, t.Name())
// // nsObj := v1.Namespace{}
// // nsObj.Name = ns
// return ctx, nil
// }
module LogsPackage
go 1.19
require sigs.k8s.io/e2e-framework v0.3.0
The output I get is,
dshiva@dshiva206HL log_test % go test -v . 2024/01/13 22:52:11 In env function === RUN TestSingleclusterInnerloopBasicSupplychainLocalSource main_test.go:36: its test case --- PASS: TestSingleclusterInnerloopBasicSupplychainLocalSource (0.00s) PASS ok LogsPackage/log_test (cached)
@shivaDS Did you figure this out? Do you think it's a bug ?
@vladimirvivien I am unable to make it work. Since I am new to golang and e2e-framework, I find it bit hard to understand why it is not working. So I cannot confirm whether it is really a bug
@shivaDS Apologies for the late reply as I was super busy.
But, the following shows how the {Before,After}EachTestest
works.
For it to work you need the followings:
Hope this helps
var testenv env.Environment
func TestMain(m *testing.M) {
testenv = env.New()
testenv.Setup(func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
fmt.Println("** Setting up e2e test...")
return ctx, nil
})
testenv.BeforeEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) {
fmt.Printf(" --> Executing BeforeTest: test %s \n", t.Name())
return ctx, nil
})
testenv.BeforeEachFeature(func(ctx context.Context, cfg *envconf.Config, t *testing.T, f features.Feature) (context.Context, error) {
fmt.Printf(" > Executing BeforeFeature: %s \n", f.Name())
return ctx, nil
})
testenv.AfterEachFeature(func(ctx context.Context, cfg *envconf.Config, t *testing.T, f features.Feature) (context.Context, error) {
fmt.Printf(" > Executing AfterFeature: %s \n", f.Name())
return ctx, nil
})
testenv.AfterEachTest(func(ctx context.Context, cfg *envconf.Config, t *testing.T) (context.Context, error) {
fmt.Printf(" --> Executing AfterTest: %s \n", t.Name())
return ctx, nil
})
testenv.Finish(func(ctx context.Context, _ *envconf.Config) (context.Context, error) {
fmt.Println("** Finishing e2e test ")
return ctx, nil
})
os.Exit(testenv.Run(m))
}
func TestSomething(t *testing.T) {
// calls testenv.BeforeEachFeature here
f1 := features.New("Feature 1").
Assess("Assessment 1", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
return ctx
})
// calls testenv.AfterEachFeature here
// calls testenv.BeforeEachFeature here
f2 := features.New("Feature 2").
Assess("Assessment 2", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
return ctx
})
// calls testenv.AfterEachFeature here
// testevn.BeforeEachTest
testenv.Test(t, f1.Feature(), f2.Feature())
// testenv.AfterEachTest
}
When you run the previous code, you will get the following
go test -v .
** Setting up e2e test...
=== RUN TestSomething
--> Executing BeforeTest: test TestSomething
> Executing BeforeFeature: Feature 1
=== RUN TestSomething/Feature_1
=== RUN TestSomething/Feature_1/Assessment_1
> Executing AfterFeature: Feature 1
> Executing BeforeFeature: Feature 2
=== RUN TestSomething/Feature_2
=== RUN TestSomething/Feature_2/Assessment_2
> Executing AfterFeature: Feature 2
--> Executing AfterTest: TestSomething
--- PASS: TestSomething (0.00s)
--- PASS: TestSomething/Feature_1 (0.00s)
--- PASS: TestSomething/Feature_1/Assessment_1 (0.00s)
--- PASS: TestSomething/Feature_2 (0.00s)
--- PASS: TestSomething/Feature_2/Assessment_2 (0.00s)
PASS
** Finishing e2e test
ok e2e-framework/workbench 0.679s
The Kubernetes project currently lacks enough contributors to adequately respond to all issues.
This bot triages un-triaged issues according to the following rules:
lifecycle/stale
is appliedlifecycle/stale
was applied, lifecycle/rotten
is appliedlifecycle/rotten
was applied, the issue is closedYou can:
/remove-lifecycle stale
/close
Please send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle stale
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.
This bot triages un-triaged issues according to the following rules:
lifecycle/stale
is appliedlifecycle/stale
was applied, lifecycle/rotten
is appliedlifecycle/rotten
was applied, the issue is closedYou can:
/remove-lifecycle rotten
/close
Please send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle rotten
The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.
This bot triages issues according to the following rules:
lifecycle/stale
is appliedlifecycle/stale
was applied, lifecycle/rotten
is appliedlifecycle/rotten
was applied, the issue is closedYou can:
/reopen
/remove-lifecycle rotten
Please send feedback to sig-contributor-experience at kubernetes/community.
/close not-planned
@k8s-triage-robot: Closing this issue, marking it as "Not Planned".
`func TestMain(m *testing.M) { log.Println("Its main function") //testenv = env.New()
}`
Here testenv.Setup() is getting executed but not BeforeEachTest. I am new to this framework. Could anyone please help?