random-data-generator


License

License

Categories

Categories

Data
GroupId

GroupId

com.danielasfregola
ArtifactId

ArtifactId

random-data-generator_2.11
Last Version

Last Version

2.8
Release Date

Release Date

Type

Type

jar
Description

Description

random-data-generator
random-data-generator
Project URL

Project URL

https://github.com/DanielaSfregola/random-data-generator
Project Organization

Project Organization

com.danielasfregola
Source Code Management

Source Code Management

https://github.com/DanielaSfregola/random-data-generator

Download random-data-generator_2.11

How to add to project

<!-- https://jarcasting.com/artifacts/com.danielasfregola/random-data-generator_2.11/ -->
<dependency>
    <groupId>com.danielasfregola</groupId>
    <artifactId>random-data-generator_2.11</artifactId>
    <version>2.8</version>
</dependency>
// https://jarcasting.com/artifacts/com.danielasfregola/random-data-generator_2.11/
implementation 'com.danielasfregola:random-data-generator_2.11:2.8'
// https://jarcasting.com/artifacts/com.danielasfregola/random-data-generator_2.11/
implementation ("com.danielasfregola:random-data-generator_2.11:2.8")
'com.danielasfregola:random-data-generator_2.11:jar:2.8'
<dependency org="com.danielasfregola" name="random-data-generator_2.11" rev="2.8">
  <artifact name="random-data-generator_2.11" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.danielasfregola', module='random-data-generator_2.11', version='2.8')
)
libraryDependencies += "com.danielasfregola" % "random-data-generator_2.11" % "2.8"
[com.danielasfregola/random-data-generator_2.11 "2.8"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.11.8
com.github.alexarchambault : scalacheck-shapeless_1.14_2.11 jar 1.2.3

provided (1)

Group / Artifact Type Version
org.scala-lang : scala-reflect jar 2.11.8

test (2)

Group / Artifact Type Version
org.specs2 : specs2-core_2.11 jar 4.5.1
org.specs2 : specs2-mock_2.11 jar 4.5.1

Project Modules

There are no modules declared in this project.

random-data-generator

Build Status License Scala Steward badge Chat

A library to generate random data for test purposes, using ScalaCheck and scalacheck-shapeless.

This library has been presented at Scalar 2017: have a look at the slides and the video of the presentation.

Setup

Supported Scala versions: 2.12+

Scala JS is also supported!

If you don't have it already, make sure you add the Maven Central as resolver in your SBT settings:

resolvers += Resolver.sonatypeRepo("releases")

Also, you need to include the library as your dependency:

libraryDependencies += "com.danielasfregola" %% "random-data-generator" % "2.9"

Do you wanna faster compilation times? Have a look at random-data-generator-magnolia - experimental but crazy fast thanks to Magnolia!

Usage

Extends the trait RandomDataGenerator to add the function random to your scope. Once the trait has been extended, you can just use the random function as following:

import com.danielasfregola.randomdatagenerator.RandomDataGenerator

object MyApp extends RandomDataGenerator {

  case class Example(text: String, n: Int)

  val example: Example = random[Example]
  // Example(ਈ䈦㈾钜㔪旅ꪔ墛炝푰⡨䌆ᵅ퍧咪, 73967257)
}

Alternatively, you can import RandomDataGenerator as object:

import com.danielasfregola.randomdatagenerator.RandomDataGenerator._

case class Example(text: String, n: Int)

val example: Example = random[Example]
// Example(巵腉밞鵾Վ뎠꿷덊,2147483647)

Have a look at the tests for more examples on how to use the library and on how to generate manual instances of Arbitrary[T] when needed.

Seed Selection

At the beginning of each test session, a seed is selected and used across all the tests. The select seed is communicated in the logs. The log message looks something like the following:

[info] [RandomDataGenerator] Generating random data using seed 6260565278463862333

Fix your Seed

When investigating bugs or test failures, it can be useful to reproduce the same generated data of a specific session.

For every session, a seed is selected and communicated in the logs. The log message will look similar to the following:

[info] [RandomDataGenerator] Generating random data using seed 6260565278463862333
[info] [RandomDataGenerator] Replicate this session by setting RANDOM_DATA_GENERATOR_SEED=6260565278463862333

To generate the same data again, all you need to do is specify an environment variable indicating the seed number to use:

export RANDOM_DATA_GENERATOR_SEED=6260565278463862333

Once you are done, remember to remove the environment variable:

unset RANDOM_DATA_GENERATOR_SEED

When a fix seed variable is detected, in the logs you will see something similar to the following:

[info] [RandomDataGenerator] Variable RANDOM_DATA_GENERATOR_SEED detected: setting 6260565278463862333 as seed

otherwise, the following message will appear:

[info] [RandomDataGenerator] No variable RANDOM_DATA_GENERATOR_SEED detected: setting seed to random number

Multiple Random Instances

Fixing the seed at the beginning of each session has an important side effect: when calling the function random[T], we always get the same instance back. However, sometimes we do need multiple instances of the same case class within the same test.

To generate multiple instances of the same case class use the random[T](n: Int) function as following:

import com.danielasfregola.randomdatagenerator.RandomDataGenerator._

val examples: Seq[Example] = random[Example](2)
// List(Example(ਈ䈦㈾钜㔪旅ꪔ墛炝푰⡨䌆ᵅ퍧咪, 73967257), Example(᭞㩵᭟뛎Ժ䌑讵蓐ꍊꎼꙐ涌㰑袽,1736119865))

Improve the Compilation Time

First, have a look at random-data-generator-magnolia: although the project is still sperimental, has increased impressive speedup in the compilation by using Magnolia's type class derivation.

random-data-generator heavily uses Shapeless, so its compilation time can be slow at times -- but think of all the magic that the compiler is doing for you!

To improve the compilation time, you can cache your implicit Arbitrary instances using shapeless.cachedImplicit:

import shapeless._

object CachedArbitraries {
    implicit val arbA: Arbitrary[A] = cachedImplicit
    implicit val arbB: Arbitrary[B] = cachedImplicit
}

For more information on what it is and on how to use it have a look here.

Snapshot Versions

To use a snapshot version of this library, make sure you have the resolver for maven central (snapshot repositories) in your SBT settings:

resolvers += Resolver.sonatypeRepo("snapshots")

Then, add the library as your dependency:

libraryDependencies += "com.danielasfregola" %% "random-data-generator" % "2.10-SNAPSHOT"

Versions

Version
2.8
2.7
2.6
2.5
2.4
2.3
2.2
2.1
2.0
1.4
1.3
1.2
1.1
1.0