Skip to content

Add repeat for parametrized tests and fuzz test examples - #259

Open
lydell wants to merge 4 commits into
elm-explorations:masterfrom
lydell:repeat
Open

Add repeat for parametrized tests and fuzz test examples#259
lydell wants to merge 4 commits into
elm-explorations:masterfrom
lydell:repeat

Conversation

@lydell

@lydell lydell commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

This PR adds a small convenience function called repeat (and one called testWith, which is test but shaped to fit repeat). This does not unlock anything that wasn’t already possible in user land – it’s all about convenience and teaching.

Me and @Janiczek were chatting, and Martin mentioned that when a fuzz test failed, he was missing a convenient way of adding that case as a regression test. repeat to the rescue!

    repeat "String.words never returns an empty list"
        [ testWith " \n\t" "only whitespace" -- regression test
        , fuzz Fuzz.string "fuzz"
        ]
    <|
        \string ->
            String.words string
                |> Expect.notEqual []

What I really like about this solution (and that I haven’t seen in ad-hoc implementations of this in test suites):

  • You can use skip and only. No commenting out stuff in the list!
  • IDE:s have a chance to find a location for each sub test. Ad-hoc implementations typically take a list of data – instead of using test functions in the list, like above – which means that there isn’t an “anchor” for each test that IDE:s can use. Also, by having this in the package, IDE:s have blessed names to look for (in addition to test, fuzz, describe etc.), whereas user land functions don’t really have that possibility.

@jfmengels

Copy link
Copy Markdown
Contributor

Very interesting!

One thing I would change is the order of arguments for testWith: I would put the title first, that way even if the data is large, the title is at the top like all tests.

-- Currently
testWith
  """Multi
       line
       piece
       of
       data
  """
  "only whitespace"

-- Suggested
testWith "only whitespace"
  """Multi
       line
       piece
       of
       data
  """

@lydell

lydell commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

Yep, I would also like it the other way. But I followed the fuzz order. (Which is wrong, too, IMO. The title should always come first.)

Maybe it’s better to do testWith “correctly” rather than “consistently”?

@jfmengels

Copy link
Copy Markdown
Contributor

I agree about the arguments for the fuzz function being out of order. That said, the title remains near the top in the usual (non-repeat) usage. Maybe that's a possible breaking change to suggest for the next major version?

For testWith, I think it would be better the way I suggested.

@lydell

lydell commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

That said, the title remains near the top in the usual (non-repeat) usage.

I’m not quite following here. Do you have an example?

@jfmengels

Copy link
Copy Markdown
Contributor

What I meant is that when defining a fuzz test such as

fuzz
  myFuzzer
  "title"
  <|
    \data -> ...

the title is still "near" the top of the test definition. Not as high as it could be (fuzz "title" myFuzzer) though.

@lydell

lydell commented Aug 18, 2026

Copy link
Copy Markdown
Contributor Author

Btw, is it confusing that testWith and fuzzWith aren’t really the same? testWith is testWithValue while fuzzWith is fuzzWithOptions.

@jfmengels

Copy link
Copy Markdown
Contributor

Right, that's a good point. testWithValue could be a better name indeed.

@lydell

lydell commented Aug 24, 2026

Copy link
Copy Markdown
Contributor Author

Maybe that's a possible breaking change to suggest for the next major version

Btw, I found an issue about this: #97

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants