New version of TestBalloon-Addons is out, with a n...
# testballoon
b
New version of TestBalloon-Addons is out, with a nifty new feature based on input from @dave08!
testballoon 1
d
So the per-test fixtures is still only for context parameters?
b
nope. you put in a generator (which can be anything) and you get a fresh instance of your generator's output)
d
In the readme it says:
Look no further , if you have context parameters enabled for your codebase:
in that section
b
so you can do
Copy code
{ class StateHolder(val number: Int)
  StateHolder(Random.nextInt())
}.generatingFixtureFor {
  repeat(100) {
    test("with fresh random)" { freshStateHolder ->
      //your test logic here, always with a new random number inside state holder
    }
  }
}
the last statement in the lambda is called each time for every test invocation and since it references Random.Default to generate a fresh Int, you will have a fresh instance of your state holder for each test, with new randomness, isolated from each other
sorry. i misread
needs context parameters
d
yup... 🤷🏼‍♂️
b
the actual logic is less than 30 LoC, so you can easily copy it out, and rewrite to wrap it into a function taking two lambdas as an extension on TestSuite and you're done
d
I'll try to take a look, thanks!
b
the only reason for context parameters is the ability to emulate the use of bare lambdas. that could also just be encapsulated to generate a wrapper object. Since this is 0.1.x, it's probably worth considering to get rid of context parameters. would you do me a favour: can you try the datatest module on a project without context parameters enabled and see if it works?
with a little bit of extra syntax, it can be made to work without context parameters:
Copy code
generatingFixture(random::nextFloat).apply { 
        repeat(10) {
            test("Generated test with random float") {
                it shouldBeGreaterThan 0.5f
            }
        }
    }
I'll publish this variant tomorrow as an alternative to what's currently available. Naming things is hard. If you have a more apt name in mind, please do drop it.
d
First, instead of apply, you can maybe have a lambda as a second param apart from the return value. Second, maybe withFixtureFrom or withFixtureUsing? It makes it a bit more clear that youre putting that fixture into the current context like stdlib with... but im not sure thats better...
Or withNewFixtureUsing?
b
I'd like to contrast it to
testFixture
, making it apparent from the name that it is generating new fixtures and not just creating a fixture for the enclosing test scope. How about this syntax (let's discuss the name separately):
Copy code
generatingFixture(random::nextFloat) - {
  repeat(10) {
      test("Generated test with random float") {
        it shouldBeGreaterThan 0.5f
      }
   }
}
It's the same as used by
withData
and
checkAll
for nesting in the other addon modules, which would be very consistent
PR is open, deprecating bare lambdas
(also means: no more context params needed)
👍🏼 1
d
Yeah, that doesn't seem too bad, I'm still a bit hesitant about the naming... generating means that it's currently generating something, or does it mean that that lambda is the generator?
b
lambda is the generator and inside the scope it will be used to generate a new fixture
d
so what's the name "generating"? How should someone looking at the code understand it?
test code should ideally be readable by someone that doesn't know all the technical details involved.
b
it will give you a scope that is generating fresh fixtures for each test inside the scope
but I agree. not 100% happy with the name
d
maybe withFixtureGenerator or something like that?
b
just
fixtureGenerator
?
but then again
withFixtureGenerator
would fit
withData
. I guess I agrre with you
The more i think about it, the more I like
withFixtureGenerator
If you don't have any objections, I'd prepare a release, then you can try it out
d
I think this is clearer than the context parameter version in the first place... I think it's good 👍🏼!
b
yeah; also does not cause issues with bare lambdas following each other
it's on maven central
d
wow that was quick 👌🏼!
b
Well, not really. maven central is extremely laggy and unreliable using the legacy endpoint and it takes ages. Plus, I forgot to tell you ;-)