gunit icon indicating copy to clipboard operation
gunit copied to clipboard

add fixture setup and teardown support

Open bugVanisher opened this issue 2 years ago • 5 comments

Enable setup before all tests and teardown after all tests. Just like BeforeClass and AfterClass in Junit4.

bugVanisher avatar Dec 11 '21 03:12 bugVanisher

@bugVanisher - I've tinkered with this idea a few times but I've always come back to just putting code before & after gunit.Run, something like this:

func TestExampleFixture(t *testing.T) {
	// SetupAll code
	gunit.Run(new(ExampleFixture), t)
	// TeardownAll code
}

Would that work?

mdwhatcott avatar Dec 11 '21 04:12 mdwhatcott

@bugVanisher - I've tinkered with this idea a few times but I've always come back to just putting code before & after gunit.Run, something like this:

func TestExampleFixture(t *testing.T) {
	// SetupAll code
	gunit.Run(new(ExampleFixture), t)
	// TeardownAll code
}

Would that work?

Yeah, this is good enough in some easy cases. But in my case, the SetupAll code is complicated, it's not clear if it is wirtten before or after gunit.Run, don't you think? Moreover, with FixtureSetup and TearDown, it is more like xunit style : )

Now in my project, It works fine with FixtureSetup and TearDown.

Another case is that putting code before & after gunit.Run can not use the code in struct nested gunit.Fixture

bugVanisher avatar Dec 11 '21 06:12 bugVanisher

this is a testcase in my project, the InitRoom/StartLiving/Release etc. are the basic functions in MMCHelper.

func TestCoverAutoPlayNoTranscode(t *testing.T) {
	gunit.Run(new(CoverAutoPlayNoTranscode), t, gunit.Options.SequentialTestCases())
}

type CoverAutoPlayNoTranscode struct {
         MMCHelper
}

func (g *CoverAutoPlayNoTranscode) Setup() {
}

func (g *CoverAutoPlayNoTranscode) Teardown() {
}

func (g *CoverAutoPlayNoTranscode) FixtureSetup() {
	g.Info().Msgf("get roomId:%s", g.GetRoomId())
	g.InitRoom(livetech_streamapi.AppID_LS.String(), g.GetRoomId())
	url := g.GetPushUrlList()[0]
	outputOpts := live.GetDefaultOutputOpts()
	outputOpts.Resolution = proto.String(live.Resolution_360P)
	g.GetStreamController().Input(g.GetInputVideoPath()).Output(url.GetPushUrl()).
		WithInputOptions(live.GetDefaultInputOpts()).WithOutputOptions(outputOpts).
		Start()
	req := g.GetDefaultStartLivingRequest()
	req.DomainId = url.DomainId
	g.StartLiving(req, global.LiveTypeDefault)
}

func (g *CoverAutoPlayNoTranscode) FixtureTeardown() {
	g.Release()
}

bugVanisher avatar Dec 11 '21 06:12 bugVanisher

Ok, so you need access to state on the fixture. I no longer maintain this repository so if this PR is not accepted you might try another similar library I built which already has support for fixture setup/teardown:

https://github.com/mdwhatcott/testing

mdwhatcott avatar Dec 11 '21 19:12 mdwhatcott

Ok, so you need access to state on the fixture. I no longer maintain this repository so if this PR is not accepted you might try another similar library I built which already has support for fixture setup/teardown:

https://github.com/mdwhatcott/testing

ok, thanks

bugVanisher avatar Dec 12 '21 06:12 bugVanisher