https://github.com/mike-neck/ktcheck
ktcheck is a test framework for Kotlin working on JUnit platform, with Given-When-Then style.
https://github.com/mike-neck/ktcheck
bdd-framework junit-platform kotlin kotlin-testing test test-framework testing
Last synced: about 2 months ago
JSON representation
ktcheck is a test framework for Kotlin working on JUnit platform, with Given-When-Then style.
- Host: GitHub
- URL: https://github.com/mike-neck/ktcheck
- Owner: mike-neck
- License: mit
- Created: 2020-03-20T11:13:39.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-20T14:11:16.000Z (almost 6 years ago)
- Last Synced: 2024-01-24T09:30:20.900Z (about 2 years ago)
- Topics: bdd-framework, junit-platform, kotlin, kotlin-testing, test, test-framework, testing
- Language: Kotlin
- Homepage:
- Size: 209 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ktcheck
ktcheck is a test framework for Kotlin working on JUnit platform, with Given-When-Then style.
---
Install
---
Use maven or Gradle.
### Maven
`pom.xml`
```xml
run.ktcheck
ktcheck
v0.1.0
maven-surefire-plugin
2.22.2
maven-failsafe-plugin
2.22.2
```
### Gradle
`build.gradle`
```groovy
dependencies {
testImplementation 'run.ktcheck:ktcheck:v0.1.0'
}
test {
useJUnitPlatform()
}
```
`build.gradle.kts`
```kotlin
dependencies {
testImplementation("run.ktcheck:ktcheck:v0.1.0")
}
test {
useJUnitPlatform()
}
```
Usage
---
### Via JUnit Platform
#### 1. Import ktcheck API
After creating new Kotlin file, add ktcheck APIs.(of course you can import via IDE's auto completion.)
```kotlin
import run.ktcheck.KtCheck
import run.ktcheck.Given
```
#### 2. Create kotlin object, which implements `KtCheck`
```kotlin
object YourTest: KtCheck
```
#### 3. Implement `KtCheck` with `Given` class using kotlin delegation property `by`.
- In a `Given` phrase, write description and function which returns a condition object.
- In a `When` phrase, write description and function which takes the condition object and returns an action result.
- In a `Then` phrase, write description and function which takes the condition object and the action result, and returns an assertion result.
```kotlin
object YourTest: KtCheck
by Given("TimeService with fixed clock", { TimeService(fixedClock) })
.When("get current time from TimeService#now", { timeService -> timeService.now() })
.Then("it should be fixed time", { _, instant -> instant shouldBe fixedInstant })
```
To create `Assertion` object, use these functions.
- `run.ktcheck.assertion.NoDep.shouldBe`
- `run.ktcheck.assertion.NoDep.shouldNotBe`
- `run.ktcheck.assertion.NoDep.should` and `run.ktcheck.assertion.Matcher`
If the condition object is not needed, a simple asserting function is available.
- `run.ktcheck.assertion.NoDep.expect`
- `run.ktcheck.assertion.NoDep.expectNull`
- `run.ktcheck.assertion.NoDep.expectNotNull`
```kotlin
object YourTest: KtCheck
by Given("TimeService with fixed clock", { TimeService(fixedClock) })
.When("get current time from TimeService#now", { timeService -> timeService.now() })
.Then("it should be fixed time", expect(fixedInstant))
```
#### 4. Run test via JUnit Platform
```shell session
$ ./mvnw test
```
```shell session
$ ./gradlew test
```
### From main program
#### 1. Import ktcheck API
After creating new Kotlin file, add ktcheck APIs.(of course you can import via IDE's auto completion.)
```kotlin
import run.ktcheck.Given
```
#### 2. Create and run `KtCheck` object in main program from `Given` class.
- Create `KtCheck` object in main program from `Given` class.
- Run `KtCheck` using `runStandalone()` function.
- If test fails, it will throws `Unsuccessful` exception.
```kotlin
import java.time.Clock
import java.time.OffsetDateTime
import java.time.ZoneOffset
fun main() {
val fixedInstant = OffsetDateTime.of(2020, 1, 2, 15, 4, 5, 6, ZoneOffset.UTC).toInstant()
val fixedClock = Clock.fixed(fixedInstant, ZoneOffset.UTC)
val check = Given("TimeService with fixed clock") { TimeService(fixedClock) }
.When("get current time from TimeService#now") { timeService -> timeService.now() }
.Then("it should be fixed time") { _, instant -> instant shouldBe fixedInstant }
check.runStandalone()
}
```
project dependencies
---
