Adding "AddCleanup" to gocheck
Tim Penhey
tim.penhey at canonical.com
Fri Aug 16 05:16:48 UTC 2013
Hi Gustavo,
One thing that has bothered me for ages about gocheck is the lack of
equivalent to the python addCleanup, so I looked at implementing it.
However I hit what appears to be a road block.
My first attempt was to add:
cleanup []func()
to the C struct.
Then a method like:
func (c *C) AddCleanup(cleanupFunc func()) {
c.cleanup = append(c.cleanup, cleanupFunc)
}
Then the next bit was to hook up the a cleanup function that would then
execute these in LIFO manner.
func (runner *suiteRunner) forkTest(method *methodType) *C
This method seems to create a callback for each test, but it also seems
to me that the SetUpTest, TearDownTest, and the test method itself are
run with different instances of C, is that right or am I misreading the
code?
What I want to be able to do is to have something like the following in
either a SetUpTest or Test* method:
c.AddCleanup(testing.PatchEnvironment("FOO", "bar"))
where testing.PatchEnvironment returns a func()
This is then executed after the TearDownTest, but before the test is
marked as completed. So the place I wanted to add it was to make a
defer call just prior to:
defer runner.runFixtureWithPanic(runner.tearDownTest, nil, &skipped)
However the problem is that the "c *C" that is in scope isn't actually
the same C that is used for either the SetUpTest, nor the Test itself.
Can you see a simple way to implement this?
Cheers,
Tim
More information about the Juju-dev
mailing list