ProductPromotion
Logo

Kotlin

made by https://0x3d.site

GitHub - xgouchet/Elmyr: A utility to make Kotlin/Java tests random yet reproducible
A utility to make Kotlin/Java tests random yet reproducible - xgouchet/Elmyr
Visit Site

GitHub - xgouchet/Elmyr: A utility to make Kotlin/Java tests random yet reproducible

GitHub - xgouchet/Elmyr: A utility to make Kotlin/Java tests random yet reproducible

Elmyr

Elmyr is a Kotlin library providing tools to generate “random” values, specifically useful for tests

Documentation Status

CircleCI codecov

license Donate

Being an adept of testing code, I write a lot of tests. One thing I noticed is that in my tests, my fake / test data always look the same. My user names are always “Bob” and “Alice”, aged 42 or 69, with userId 4816152342 or 24601, and eating “spam”, “eggs” and “bacon”.

The problem is, the more test I write, the less I'm confident in my fake values, because they're always the same.

This is where Elmyr kicks in, allowing you to create fake/fuzzy data based on a few constraints, making your test data random, and yet reproducible.

Usage

Gradle

    dependencies {
        // Core library
        testCompile("com.github.xgouchet.Elmyr:core:1.4.1")
    
        // Testing Framework Integrations
        testCompile("com.github.xgouchet.Elmyr:junit4:1.4.1")
        testCompile("com.github.xgouchet.Elmyr:junit5:1.4.1")
        testCompile("com.github.xgouchet.Elmyr:spek:1.4.1")
    
        // 
        testCompile("com.github.xgouchet.Elmyr:jvm:1.4.1")
    }

Forging data: the core module

You can create an instance of the Forge class, and from that generate:

  • primitives, with basic constraints
  • Strings matching simple predicates or even Regexes
  • Any Kotlin data class
  • Your own custom data, by implementing the ForgeryFactory interface, then calling the Forge::addFactory method.

ForgeRule for junit4

You can instantiate a ForgeRule instance, which extends the Forge class, add factories to it, and then annotate fields on your test class with @Forgery.

class FooTest {

    @get:Rule
    val forge = ForgeRule()
            .withFactory(FooFactory())
            .withFactory(BarFactory())

    @Forgery
    internal lateinit var fakeBar: Bar

    @Forgery
    lateinit var fakeFooList: List<Foo>

    //…
}

ForgeExtension for junit5

You can add an extension and configure it. In addition to creating forgeries on fields/properties of your test class, you can inject parameters directly on your test methods.

@ExtendWith(ForgeExtension::class)
@ForgeConfiguration(KotlinAnnotationTest.Configurator::class)
internal class FooTest {

    @Forgery
    internal lateinit var fakeBar: Bar

    @Forgery
    lateinit var fakeFooList: List<Foo>

    @Test
    fun testSomething(@IntForgery i: Int, forge:Forge){
        // …
    }
}

spek forgeries

You can create a custom Forge instance with spekForge to be able to add reproducibility to Spek tests.

class CalculatorSpek : Spek({

    val forge = spekForge(
        seeds = mapOf(
            "CalculatorSpek/A calculator/addition/returns the sum of its arguments" to 0x1337L
        )
    )

    describe("A calculator") {
        val calculator by memoized { Calculator() }
        
        describe("addition") {
            it("returns the sum of its arguments") {
                val a = forge.anInt()
                val b = forge.anInt()
                assertEquals(calculator.add(a, b), a + b)
            }
        }
    }
})

Documentation

The full documentation will be coming shortly. The Wiki contains a full reference of all the packages and classes distributed.

Contributing

Contribution is always welcome, to know more, read our Contributing guide.

Release History

Latest Release: 1.4.1 (2024/10/19)

core

  • Add enum forgery through reflexive factory
  • Make reflexive factory available through injection (with @Forgery)
  • Make reflexive factory available via JUnit4 (with @ForgeRule)
  • Make reflexive factory available via JUnit5 (with @ForgeExtension)
  • Add set forgery (thanks @ambushwork)
  • Add list forgery with indexed lambda (thanks @ambushwork)

Donate

This library is completely free to use and modify (as per the License). I try my best to make it as good as possible, but only do this on my free time. If you want to support my work, you can click the Donate button below.

paypal

Meta

Xavier F. Gouchet – @xgouchet

Distributed under the MIT license. See LICENSE.md for more information.

https://github.com/xgouchet/Elymr

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory