jorgebucaran/fishtape

feature request: before_each and after_each hooks

edouard-lopez opened this issue · 8 comments

For pure test suite, I'm isolating each test from the other as much as possible to avoid side-effect. To do so, I regularly create before_each and after_each functions in test files. For instance:

Feature Request

Would it be possible to add a before/after hooks mechanism to before/after running the test?

Before running a @test, execute the before_each function defined by the user.
After a @test function was executed, execute the function after_each function defined by the user

Playing around, I realize it's not possible to do hooks as the @test command is executed with the result of it's second parameter:

@test "foo" (echo first) = 1

I was trying to add a call to a hook inside @test, but that happend after the evaluation of (echo first).

Please correct me if I'm wrong 🙏

Your Pure test suite is a lot to go through. How about making a test repo or just providing me an isolated, minimal test example that causes issues? I can dive into that one, and then you can sort out the bigger picture.

It's not a bug, it's a feature request to add some syntactic sugar. Right now, I do the following:

  1. declare a hook function, e.g. before_each
  2. manually call it before each @test

Here is a short example of how I do it:

https://github.com/pure-fish/pure/blob/924c1c6a9c88125d8d58f00ffe5816fed57f4d99/tests/_pure_k8s_context.test.fish#L7-L17

Nevertheless, I was wondering how the framework could do such call such hook automatically before/after each @test. However, it look not possible.

I initially assumed this was a bug report. Could you clarify what the feature request is?

I updated the description

Your current strategy is actually what I'd recommend as well. Are you running into any obstacles with it, or is this more about convenience for you?

I was looking for a solution where Fishtape would be running the before_each / after_each hook so I can drop them from my test files:

function before_each
	set --universal awesome_var true
end

-before_each # WISH: done by fishtape
@test "Awesome test" (
	echo "foo"
) = "foo
-after_each # WISH: done by fishtape

Gotcha. That's how the old Fishtape worked. I opted to cut it out in the interest of simplicity. You might not see eye-to-eye with me on this, but I believe a streamlined Fishtape is better for both me and the project going forward.