¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Re: [TDD] Testing a class which utilizes randomness


 

no two strings are ever the same.

Ever? ?Is that even possible?


What does close enough mean? Can they be UUIDs and GUIDs and be "close enough"? Or do they need to be *random* and non-repeating??
?
What I want to test is exactly this behavior - the code which discards the duplicate and forces the string to be regenerated. To test that, I need to force the random string generator to return a duplicate at least once (but finite number of times). To create the test which would generate strings until there's at least one duplicate is unrealistic, because such a test could take a very long time to complete.

How would I write such a test?

by separating concerns.?

You have string generation.
You have duplicate rejection.

You can test duplicate rejection with a mock string generator.

You can test randomness of string generations.... aw, heck, looks like you need stats and algo gurus. Math, not a billion unit tests for the algorithm.
?


--
Tim Ottinger, Sr. Consultant, Industrial Logic
-------------------------------------



Join [email protected] to automatically receive all group messages.