junit-team / junit5

✅ The 5th major version of the programmer-friendly testing framework for Java and the JVM
https://junit.org
Eclipse Public License 2.0
6.43k stars 1.49k forks source link

Add Kotlin `Sequence` support for `@TestFactory` methods #3376

Open hanszt opened 1 year ago

hanszt commented 1 year ago

Overview

It would be nice if you could write something like this in Kotlin when working with JUnit Jupiter.

        @TestFactory
        fun `dynamic tests returned as Kotlin sequence`() = generateSequence(0) { it + 2 }
                .map { dynamicTest("$it should be even") { assertTrue(it % 2 == 0) } }
                .take(10)

Currently you have to add .toIterable() as an extra line to make it work with JUnit Jupiter.

The Kotlin Sequence class is an example of an iterator providing class, but it is not an Iterable. That's why it does not work as return type for @TestFactory methods at the moment.

A benefit of adding this at this low level is that, Kotlin Sequences, or any other Iterator or Spliterator providing class can now also be used in other JUnit Jupiter methods where an object from an annotated method needs to be converted to a stream.

I have an implementation for it ready. see:

Suggestion

All tests still pass.

I would like to contribute to JUnit 5 and make a pull request for this branch, but I don't have push permission yet for a feature branch in the JUnit 5 project.

Could you please be so kind to take a look at the suggestion I provided?

Thanks in advance! I'm awaiting your response :)

Deliverables

marcphilipp commented 1 year ago

Team decision: Add convention-based conversion to Stream that looks for methods called iterator that return Iterator.

hanszt commented 1 year ago

Thanks for the feedback. I implemented the requested changes in this pullrequest