smarty / gunit

xUnit-style test fixture adapter for go test
Other
119 stars 11 forks source link

add fixture setup and teardown support #32

Closed bugVanisher closed 1 year ago

bugVanisher commented 2 years ago

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

mdwhatcott commented 2 years ago

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

bugVanisher commented 2 years ago

@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 commented 2 years ago

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()
}
mdwhatcott commented 2 years ago

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

bugVanisher commented 2 years ago

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