Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Fixtures for Kotlin providing generated values for unit testing

License

NotificationsYou must be signed in to change notification settings

appmattus/kotlinfixture

Repository files navigation

Getting started

Include the following dependency in yourbuild.gradle.kts file:

build.gradle.kts
testImplementation("com.appmattus.fixture:fixture:<latest-version>")

Simply create a fixture and invoke it with the type to be generated:

val fixture= kotlinFixture()// Generate a list of stringsval aListOfStrings= fixture<List<String>>()// Nulls are supportedval sometimesNull= fixture<Int?>()// Create instances of classes// Optional parameters will be randomly used or overriddendata classADataClass(valvalue:String ="default")val aClass= fixture<ADataClass>()// Abstract classes will pick a sub-class at random// This could be a Byte, Double, Long, Float, Int or Shortval anyNumber= fixture<Number>()// Pick randomly from a listval randomStringFromTheList= fixture(listOf("Cat","Dog","Horse"))val anotherRandomIntFromAList= fixture(1..5)

You can also generate an infinite sequence of a type, which you can thenfilter:

val fixture= kotlinFixture()val intSequence= fixture.asSequence<Int>()// Standard Kotlin sequence functions can then be applied before using// the sequence through an iterator for access to the next() function.// For example, you can filter valuesval oddIterator= intSequence.filter { it.absoluteValue.rem(2)==1 }.iterator()val oddNumber= oddIterator.next()val anotherOddNumber= oddIterator.next()// Or, ensure it returns only distinct valuesenumclassXYZ {X,Y,Z }val enumIterator= fixture.asSequence<XYZ>().distinct().iterator()val aDistinctValue= enumIterator.next()val anotherDistinctValue= enumIterator.next()
⚠️

The sequence can hang indefinitely if the applied operators prevent the generation of new values. For example:

  • distinct will hang if we exhaust all available values. A good practice is to add atake(count) which will throw aNoSuchElementException if we try to generate more values.

  • filter that can never be fulfilled e.g.filter { false }

Configuration options

Everything can be customised, seeconfiguration options for more details.

Advanced engine customisation is also possible if the above options are not enough.

Kotest integration: property based testing

The library providesKotlinFixture powered property based testing forKotest.

SeeKotest integration for more details.

Java Faker integration: pretty data

Generate values with a closer match to real data usingJava Faker.

SeeJava Faker integration for more details.

Generex integration: regex to random string

To generate a random string from a regular expression, look no further than the Generex integration.

SeeGenerex integration for more details.

Related projects

Please take a look at the featurecomparison with related projects.

Contributing

Please fork this repository and contribute back usingpull requests.

All contributions, large or small, major features, bug fixes, additionallanguage translations, unit/integration tests are welcome.

License

License

Copyright 2021 Appmattus Limited

Licensed under the Apache License, Version 2.0 (the "License"); you maynot use this file except in compliance with the License. You may obtaina copy of the License athttps://www.apache.org/licenses/LICENSE-2.0.

Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.


[8]ページ先頭

©2009-2025 Movatter.jp