Git Product home page Git Product logo

Comments (13)

vladimirvivien avatar vladimirvivien commented on July 26, 2024

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>
}

from e2e-framework.

shivaDS avatar shivaDS commented on July 26, 2024
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")
}

from e2e-framework.

shivaDS avatar shivaDS commented on July 26, 2024
module LogsPackage

go 1.19

require sigs.k8s.io/e2e-framework v0.3.0

from e2e-framework.

shivaDS avatar shivaDS commented on July 26, 2024

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

from e2e-framework.

vladimirvivien avatar vladimirvivien commented on July 26, 2024

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.

from e2e-framework.

shivaDS avatar shivaDS commented on July 26, 2024

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)

from e2e-framework.

vladimirvivien avatar vladimirvivien commented on July 26, 2024

@shivaDS
Did you figure this out? Do you think it's a bug ?

from e2e-framework.

shivaDS avatar shivaDS commented on July 26, 2024

@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

from e2e-framework.

vladimirvivien avatar vladimirvivien commented on July 26, 2024

@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:

  • You need a TestFunction
  • You also need features and assessments to be defined as shown below:

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

from e2e-framework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.