Hacker News new | ask | show | jobs
by GordyMD 4077 days ago
As others have said randomness can have it's place in other testing suits, but when developing unit tests using a TDD (Test Driven Development) approach it really does not fit in at all, and is not the best example.

If you are indeed following an actual test driven approach, you write a test before you write your implementing code. Following this patten your test will define a specific behaviour, and to begin with will fail, as no implementing code exists. You then write your application code to make that test pass. Then you introduce another test to test another case, and so on. In order to follow this approach you need to understand what you are required to build and therefore will have some concept of edge cases and acceptable limitations.

Randomness really does not fall into the mantra of TDD. You usually aim for a set of deterministic tests that will always have the same outcome given the same application code. This ensures that when your tests fail, you focus on your application code, rather than the possibilities of actual tests that your test suite produced and which variation caused it to fail.

So in this example a better would be to test the extremes of how many items this component should be able to hold and then write a test for each specific test case.