https://github.com/robstoll/tutteli-spek-extensions
Extensions for the spek framework such as TempFolder
https://github.com/robstoll/tutteli-spek-extensions
kotlin spek testing
Last synced: 6 months ago
JSON representation
Extensions for the spek framework such as TempFolder
- Host: GitHub
- URL: https://github.com/robstoll/tutteli-spek-extensions
- Owner: robstoll
- License: apache-2.0
- Archived: true
- Created: 2018-02-26T10:29:46.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2023-09-04T17:22:00.000Z (almost 3 years ago)
- Last Synced: 2025-10-24T03:53:34.032Z (8 months ago)
- Topics: kotlin, spek, testing
- Language: Kotlin
- Size: 692 KB
- Stars: 2
- Watchers: 1
- Forks: 4
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
[](https://search.maven.org/artifact/ch.tutteli.tutteli-spek-extensions/tutteli-spek-extensions/1.2.1/jar)
[](http://opensource.org/licenses/Apache2.0)
[](https://github.com/robstoll/tutteli-spek-extensions/actions?query=workflow%3AUbuntu+branch%3Amaster)
[](https://github.com/robstoll/tutteli-spek-extensions/actions?query=workflow%3AWindows+branch%3Amaster)
[](https://sonarcloud.io/dashboard?id=robstoll_tutteli-spek-extensions)
[](https://sonarcloud.io/dashboard?id=robstoll_tutteli-spek-extensions)
# Tutteli spek extension
A set of [Spek](http://spekframework.org/) extensions such as [MemoizedTempFolder](#MemoizedTempFolder).
# Installation
tutteli-spek-extensions is published to maven central.
```
repositories { mavenCentral() }
dependencies {
testImplementation("ch.tutteli.spek:tutteli-spek-extensions:1.2.1")
}
```
# Features
## MemoizedTempFolder
`memoizedTempFolder` provides -- similar to TemporaryFolder in junit4 -- utility methods to create temp files and folders and takes care of deleting them.
Specify a `memoizedTempFolder` within a group like scope near to the test you are going to use the tempFolder (default `CachingMode` is per `TEST`, so each test gets its own temporary directory)
```kotlin
import memoizedTempFolder
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
object MySpec: Spek({
describe("...") {
val tempFolder by memoizedTempFolder()
it ("...") {
val file = tempFolder.newFile("test.txt")
}
}
})
```
Pass a `CachingMode` if required (see [Caching modes @ spekframework.org](https://www.spekframework.org/core-concepts/#caching-modes))
For instance:
```kotlin
import ch.tutteli.atrium.api.fluent.en_GB.*
import ch.tutteli.atrium.verbs.expect
import memoizedTempFolder
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.spekframework.spek2.lifecycle.CachingMode
object MySpec: Spek({
describe("...") {
val tempFolder by memoizedTempFolder(CachingMode.SCOPE)
it ("test1") {
val file = tempFolder.newFile("test.txt")
file.delete()
}
it("test2"){
expect(file).exists() // would fail
}
}
})
```
And you can use the second argument of `memoizedTempFolder` for additional setup:
```kotlin
import ch.tutteli.atrium.api.fluent.en_GB.*
import ch.tutteli.atrium.verbs.expect
import memoizedTempFolder
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.spekframework.spek2.lifecycle.CachingMode
object MySpec: Spek({
describe("...") {
val tempFolder by memoizedTempFolder(CachingMode.TEST) {
val f = newDirectory("folderWithinTempFolder")
newSymbolicLink("link", f)
}
it ("test1") {
expect(tempFolder.resolve("folderWithinTempFolder")).exists()
expect(tempFolder.resolve("link")).exists()
}
}
})
```
There are a few other utility methods defined on `MemoizedTempFolder`: `newDirectory`, `newSymbolicLink`,
`resolves` and `withinTmpDir`.
Tutteli spek extension works best in combination with [Niok](https://github.com/robstoll/niok)
which enhances `Path` with methods like `createDirectories`, `setAttribute`, `writeLines` and many more (not only useful in tests but also in production code).
With Niok in place, more complicated setup can be defined easily:
```kotlin
import memoizedTempFolder
import ch.tutteli.niok.*
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import org.spekframework.spek2.lifecycle.CachingMode
object MySpec: Spek({
describe("...") {
val tempFolder by memoizedTempFolder(CachingMode.TEST) {
withinTmpDir {
val subDir = resolve("dir1/dir2/dir3").createDirectories()
subDir.resolve("a.txt").writeLines(listOf("a", "b", "c"))
}
}
}
})
```
And if you like to assert certain properties of a Path, then we recommend using [Atrium](https://github.com/robstoll/atrium).
# License
tutteli-spek-extensions is licensed under [Apache 2.0](https://opensource.org/licenses/Apache2.0).