For testing, it would greatly enhance if there is inbuild option to provide locale as (random, fixed, default) based on a value from config file.
Is your feature request related to a problem? Please describe. For testing a application, there are a few use cases:
- When I want to test my application with as many locales as possible. So say if I have 2 test classes with 10 tests in each; if each of those tests are running in parallel and they pick up a different random locale, that is awesome from a testing point of view.
- When I want to test my application, with a specific locale, because I want to make sure my tests can handle that particular locale. So all 2 classes, with 10 tests in each, all running on same locale.
- I want the flexibility to choose any of these modes and want my tests to be flexibile so that they pick up a "random" locale mode or "fixed" locale mode based on a parameter I set in a config file.
Describe the solution you'd like
I would like an option where a test user can specify in a faker.properties file, what mode user wants for his tests to run with (random, fixed or a default mode for incorrect set properties value). Based on this the faker should create random locale for each test (if random mode is chosen) or fixed locale (if fixed mode) is chosen or a default mode (if wrong value is provided).
Describe alternatives you've considered A clear and concise description of any alternative solutions or features you've considered.
So the way, I solved this right now is by:
- Creating a LocalePicker class, that has 3 methods.
- [ ] A private method that returns a list of all valid "locales" as a List when called.
private static List<String> getLocales() {
// List of available locales. It would have been nice if faker provided this.
List<String> locales = new ArrayList<>();
// Add all available locals in Java faker.
locales.add("bg");
locales.add("ca");
locales.add("ca-CAT");
..... [All countries added here... ]
locales.add("uk");
locales.add("vi");
locales.add("zh-CN");
locales.add("zh-TW");
return locales;
}
- [ ] A second private method that uses the above method to return a random "locale" from the list based on size of List.
private static String getRandomLocale() {
// Pick a random country and return its locale value.
Random random = new Random();
Integer randomCountry = random.nextInt(getLocales().size());
return getLocales().get(randomCountry);
}
- [ ] And a third public method that gives locale based on what the user has chosen in the config file. There are three modes to select from here.
public static String getLocale() {
String locale = TestConfig.getConfig().getString("locale");
String chosenLocale;
if (locale.equals("random")) {
chosenLocale = getRandomLocale();
} else if (getLocales().contains(locale)) {
chosenLocale = locale;
} else {
chosenLocale = "fa";
}
log.info("Chosen locale: {}", chosenLocale);
return chosenLocale;
}
I then get the desired locale by calling it in tests as below:
// Test Data
private Locale locale = new Locale(LocalePicker.getLocale());
private Faker faker = new Faker(locale);
private String firstName = faker.name().firstName(); // Emory
private String lastName = faker.name().lastName(); // Barton
Now it would be nice, if the list of available countries, is already provided by faker, so that it is always up to date and users dont have to create a make shift solution like I did above. Also it would be nice if faker can provide a config to set the parameter mayb in a properties file, so that users can simulate above two modes (random, fixed or default) based on the user choice and the option to use this property as a parameter in Faker constructor.
Additional context Add any other context or screenshots about the feature request here. I am able to better test my application with this random mode still having the flexbility to run my tests in a fixed locale mode, just by changing the config property. I see it takes care of the "pesticide paradox" in testing. https://www.pitsolutions.ch/blog/defect-clustering-and-pesticide-paradox/

Pull request #685 ["Add random locale selection/rotation for internationalization/localization testing"] has been submitted to add this feature.