Configuring Faker¶
Default Configuration¶
Faker
can be configured through the FakerConfig
class, either by passing an instance of FakerConfig
directly, or through the faker
builder DSL.
If no FakerConfig
instance is passed to Faker
constructor and configuration is not set through the faker
builder DSL, then default configuration will be used with the following values:
locale == "en"
random
is seeded with a pseudo-randomly generated number.uniqueGeneratorRetryLimit
is set to100
Deterministic Random¶
Faker supports seeding of it's PRNG (pseudo-random number generator) through FakerConfig
to provide deterministic output of repeated function invocations.
val config = fakerConfig { random = Random(42) }
val faker = Faker(config)
val city1 = faker.address.city()
val name1 = faker.name.name()
val otherConfig = fakerConfig { random = Random(42) }
val otherFaker = Faker(otherConfig)
val city2 = otherFaker.address.city()
val name2 = otherFaker.name.name()
assertEquals(city1, city2)
assertEquals(name1, name2)
FakerConfig.Builder configBuilder = FakerConfig.builder();
Faker faker = new Faker(configBuilder.withRandom(Random(42)).build());
String city1 = faker.getAddress().city();
String name1 = faker.getName().name();
Faker otherFaker = new Faker(configBuilder.withRandom(Random(42)).build());
String city2 = otherFaker.getAddress().city();
String name2 = otherFaker.getName().name();
assertEquals(city1, city2);
assertEquals(name1, name2);
Alternatively a randomSeed
property can be used instead of passing an instance of java.util.Random
:
Info
randomSeed
config property has precedence over random
property, and the latter will be ignored if randomSeed
is specified.
FakerConfig config = FakerConfig.builder()
.withRandom(Random(123))
.withRandomSeed(42)
.build();
Faker faker = new Faker(config);
String city1 = faker.getAddress().city();
FakerConfig otherConfig = FakerConfig.builder()
.withRandom(Random(42))
.build();
Faker otherFaker = new Faker(otherConfig);
String city2 = otherFaker.getAddress().city();
assertEquals(city1, city2);
Locale¶
By default Faker
uses en_US
-localized dict files to generate data:
When needed, Faker
can be configured to use a custom localized dictionary file instead:
Info
The locale
configuration property should be set following the .yml
dict file name, that is a -
should be used as a delimiter between "language" and "country" values instead of _
, i.e. nb-NO
instead of nb_NO
.
Using a non-default locale will replace the values in some of the providers with the values from localized dictionary.
Info
Note that if the localized dictionary file does not contain a category (or a parameter in a category) that is present in the default locale, then non-localized (en
) value will be generated instead.
val faker = Faker() // uses default 'en' locale
faker.gameOfThrones.cities() // => Braavos
val config = fakerConfig {
locale = "nb-NO"
}
val localizedFaker = Faker(config)
// 'game_of_thrones' category is not localized for `nb-NO` locale
// and non-localized value is returned instead
localizedFaker.gameOfThrones.cities() // => Braavos
Info
A list of all locales, and their corresponding dictionary files, can be found on the Available Locales page.
Random Class Instance¶
RandomClassInstance can also be configured from the FakerConfig
.
Info
This configuration takes the least precedence and will be overridden by config set on the faker.randomProvider
level or on the randomClassInstance
function level.
See Random Class Instance Configuration for more details.