https://kotlinlang.org logo
Title
c

Colton Idle

02/04/2022, 2:30 PM
I have a plain old little java library with about 20 unit tests, but now I want to just change one variable for the tests so that I have 40 tests (its the same 20 tests, ran twice, with different inputs). How can you do that with kotlin + gradle?
a

andylamax

02/04/2022, 7:50 PM
writing here so that I get notifications on how to solve this as well
m

Mykola Gurov

02/06/2022, 3:56 PM
Depends on what kinda of these inputs is that - depending ono that you might want different approaches
Also depends on how are you going to run all those tests, do they have to run all together with maven/gradle in the same test suite, or can they be invoked separately
One hacky way would be to use system properties to parameterize tests.
Another option is to refactor the tests into Dynamic tests with
@TestFactory
of Junit5 (I assume, the unit tests are written in Junit?) .
@ParameterizedTest
is also an option, although I personally find it very inconvenient.
In case those 20 tests are in the same class - junit style again - you could make a subclass of that for the alternative parameter. A bit ugly, especially in regards of executing an individual test method from IDE, but sort-of works
c

Colton Idle

02/06/2022, 8:47 PM
So I eneded up using parameterized tests in kotlin + junit4. It wasn't too bad to setup, although a little confusing. BUT I had some issues getting those parameters in @BeforeClass. What I eneded up doing was passing params through cmd line. Like
./gradlew :blah:tests --tests "com.mytests" -Dmyarg=FOO
1
and I basically on CI just run that script twice. each time with diff args. works exactly as i need it. and i learned about parameterized tests which is cool