https://github.com/blindpirate/junit5-unroll-extension
JUnit 5 Unroll Extension for Kotlin
https://github.com/blindpirate/junit5-unroll-extension
data-driven-tests junit5 junit5-framework kotlin spock
Last synced: about 1 month ago
JSON representation
JUnit 5 Unroll Extension for Kotlin
- Host: GitHub
- URL: https://github.com/blindpirate/junit5-unroll-extension
- Owner: blindpirate
- License: apache-2.0
- Created: 2018-03-26T00:30:50.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-08-02T02:06:58.000Z (almost 7 years ago)
- Last Synced: 2025-04-06T17:07:27.630Z (about 2 months ago)
- Topics: data-driven-tests, junit5, junit5-framework, kotlin, spock
- Language: Kotlin
- Homepage:
- Size: 97.7 KB
- Stars: 8
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JUnit 5 Unroll Extension for Kotlin
[](https://travis-ci.org/blindpirate/junit5-unroll-extension)
[](http://www.apache.org/licenses/LICENSE-2.0.txt)JUnit 5 Unroll Extension is a JUnit 5 extension which supports parameterized tests in kotlin. TL;DR:
```
import com.github.blindpirate.junit.extension.unroll.Param
import com.github.blindpirate.junit.extension.unroll.Unroll
import com.github.blindpirate.junit.extension.unroll.where
import java.lang.Mathclass Math {
@Unroll
fun `max number of {0} and {1} is {2}`(
a: Int, b: Int, c: Int, param: Param = where {
1 _ 3 _ 3
7 _ 4 _ 7
0 _ 0 _ 0
}) {
assert(Math.max(a, b) == c)
}
}
```yields the following result:
```
max number of {0} and {1} is {2}(int, int, int, com.github.blindpirate.Param) ✔
├─ max number of 0 and 0 is 0 ✔
├─ max number of 1 and 3 is 3 ✔
└─ max number of 7 and 4 is 7 ✔
```## How to use
Maven:
```
com.github.blindpirate
junit5-unroll-extension
0.1.2
test```
Gradle:
```
repositories {
jcenter()
}dependencies {
testCompile 'com.github.blindpirate:junit5-unroll-extension:0.1.2'
}
```- Add configuration to your `pom.xml` or `build.gradle`.
- Add `@Unroll` to your test method. Note `@Unroll` can't be used together with `@Test` or `@ParamerizedTest`.
- Put the arguments in the `where` function as shown above.## Why Unroll Extension
This extension is greatly inspired by [Spock](http://spockframework.org/). If you have used [Spock](http://spockframework.org/),
you may be impressed by its powerful [Data Driven Test](http://spockframework.org/spock/docs/1.0/data_driven_testing.html):```
class Math extends Specification {
def "maximum of two numbers"(int a, int b, int c) {
expect:
Math.max(a, b) == cwhere:
a | b | c
1 | 3 | 3
7 | 4 | 7
0 | 0 | 0
}
}
```Other frameworks have similar features:
[JUnit 4 Parmeterized Tests](https://github.com/junit-team/junit4/wiki/parameterized-tests):
```
@RunWith(Parameterized.class)
public class FibonacciTest {
@Parameters
public static Collection data() {
return Arrays.asList(new Object[][] {
{ 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
});
}@Parameter // first data value (0) is default
public /* NOT private */ int fInput;@Parameter(1)
public /* NOT private */ int fExpected;@Test
public void test() {
assertEquals(fExpected, Fibonacci.compute(fInput));
}
}
```[JUnit 5 Parameterized Tests](https://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests):
```
@ParameterizedTest
@ValueSource(ints = { 1, 2, 3 })
void testWithValueSource(int argument) {
assertTrue(argument > 0 && argument < 4);
}
```However, you can hardly add anything other than strings and numbers into `@ValueSource`. Extra `ArgumentsProvider`
and `ArgumentConverter` make things much more complex.With this extension, you can have your parameters as they are.