-
Notifications
You must be signed in to change notification settings - Fork 349
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
Test randomness #125
Comments
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 1 week if no further activity occurs. Thank you for your contributions. |
@localheinz also noticed we could use the unique generator to exhaust the resource pool. Problem is that this will consume a lot of RAM though. Im not a fan of just looping hundreds of times. I fact we know for sure we can not guarantee this 100% at this point however also the risk of issues is pretty low right? |
Any test that tests that asserts that a provider, given a specific seed of the generator, returns a specific value is bound to fail when the randomization engine changes - see #691, for example. The question is, what are we going to do when the randomization engine changes? Fix all the failing tests by adjusting the expectations? Seems painful. For example, I do not see any value in asserting that On a separate note, the |
You should differentiate between what you are testing for in a library like this. Because indeed:
Not just no value, it's downright wrong to test like that, as you are testing something that, when it fails, does not imply anything is wrong with the code. If it returns It does however make sense to test repeatable seeded determinism. |
Summary
This topic was brought up by @krsriq in #82 and partially addressed in #90. This issue is to discuss possible solutions to make sure our tests are properly checking the library behavior.
Versions
fakerphp/faker
main
Self-enclosed code snippet for reproduction
Tests are now seeded with
1
to assure test result consistency but that opens another potential issue:Faker/test/Faker/Provider/PaymentTest.php
Lines 36 to 39 in 0d72e9f
In this code,
$this->faker->creditCardType
will always returnMasterCard
because generator is seeded with1
before each test method execution so removing the last element in array will still make the test pass every time.Possible solutions
While testing random data generation in a reproducible way is difficult, we should make sure our tests are working properly.
Retry testsI have tried experimenting with PHPUnit
--repeat 100
flag to repeat the test several times butsetUp
(and thereforeseed(1)
) is called before each repeat too.Mark tests that require seeding
Another approach would be to introduce a custom
@seed <int>
annotation for test methods or classes that explicitly require seeding before test to get specific results:https://github.com/FakerPHP/Faker/blob/main/test/Faker/Provider/ja_JP/InternetTest.php and https://github.com/FakerPHP/Faker/blob/main/test/Faker/Provider/uk_UA/PersonTest.php
This approach together with
--repeat 100
flag in one of our test matrix should allow us to make sure all the tests are actually making sure that generated data is always correct.Other notes
The text was updated successfully, but these errors were encountered: