Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Faker #313

Open
jogjitu opened this issue Dec 27, 2024 · 4 comments
Open

Add Faker #313

jogjitu opened this issue Dec 27, 2024 · 4 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@jogjitu
Copy link

jogjitu commented Dec 27, 2024

Feature description

Add faker for demo data generation

Describe why this should be added to Spectrum

Fake helps is demo test data generation which can save a lot of time

Additional context

@jogjitu jogjitu added the enhancement New feature or request label Dec 27, 2024
@giulong
Copy link
Owner

giulong commented Dec 27, 2024

Are you talking about this? It seems to be unmaintained, but if you think it's still valid is ok for me.

Can you provide any additional info, such as a code snippet of how you currently use it, or how you would like to use it in Spectrum? Or is the snippet in their README enough?

Faker faker = new Faker();

String name = faker.name().fullName(); // Miss Samanta Schmidt
String firstName = faker.name().firstName(); // Emory
String lastName = faker.name().lastName(); // Barton

String streetAddress = faker.address().streetAddress(); // 60018 Sawayn Brooks Suite 449

Based on this, we could just inject the new Faker() instance in tests classes, so that you don't have to manage it by yourself. Since they have constructors that take Locale and Random instances I would go for a configuration node like this (values here are just an example):

faker:
  locale: en_US
  random: 24

so that the injected Faker is configured accordingly. What do you think?

@jogjitu
Copy link
Author

jogjitu commented Dec 29, 2024

Yes, this makes sense

@giulong
Copy link
Owner

giulong commented Dec 29, 2024

Ok, I'm gonna work on it in the next days. Keep you posted.
If you wanna contribute by implementing it, just let me know and I'll share few considerations.

@giulong giulong added the good first issue Good for newcomers label Dec 29, 2024
@giulong giulong self-assigned this Jan 3, 2025
giulong added a commit that referenced this issue Jan 3, 2025
@giulong
Copy link
Owner

giulong commented Jan 3, 2025

Looking at Javafaker issues, I found many in which they talk about Datafaker as a replacement, for example this one. Basically, Datafaker is a fork whose api is compatible with Javafaker, and is actively maintained, so no CVEs and new features. For completeness, here's the Datafaker's docs.

For the reasons above, I chose Datafaker. Constructors are the same, they take (optionally) a Locale and a Random instance. So, it can be configured in Spectrum via this node:

faker:
  locale: it
  random: 24

Here you can find the currently supported locales. The Spectrum's internal default is this:

faker:
  locale: en
  random: null

Meaning if you need no random seed and the english locale, you can leverage the default and avoid configuring it explicitly.

I added a couple tests to show how to use it. Basically, you just need to leverage the injected faker instance directly:

@Test
void test() {
    String name = faker.name().fullName(); // Miss Samanta Schmidt
    String number = faker.numerify("##");
    String anotherNumber = faker.expression("#{numerify '##'}");
    ...
}

You can find the implementation in PR #325. Tell me if you think something is missing or wrong. Otherwise, if it's ok for you, I'll prepare a new release with it. Please let me know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants