{"id":20619027,"url":"https://github.com/tessapower/seeded_random_tests","last_synced_at":"2025-03-06T19:45:07.428Z","repository":{"id":188839764,"uuid":"381906508","full_name":"tessapower/seeded_random_tests","owner":"tessapower","description":"Repo for a talk I gave on randomized testing using RSpec and FFaker.","archived":false,"fork":false,"pushed_at":"2022-11-27T01:33:56.000Z","size":1055,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-17T05:07:52.809Z","etag":null,"topics":["presentation","random-number-generator","randomized-testing","talk","testing"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tessapower.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-07-01T04:15:19.000Z","updated_at":"2022-11-27T01:32:15.000Z","dependencies_parsed_at":"2023-08-17T05:12:05.267Z","dependency_job_id":null,"html_url":"https://github.com/tessapower/seeded_random_tests","commit_stats":null,"previous_names":["tessapower/seeded_random_tests"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tessapower%2Fseeded_random_tests","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tessapower%2Fseeded_random_tests/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tessapower%2Fseeded_random_tests/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tessapower%2Fseeded_random_tests/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tessapower","download_url":"https://codeload.github.com/tessapower/seeded_random_tests/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242277652,"owners_count":20101536,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["presentation","random-number-generator","randomized-testing","talk","testing"],"created_at":"2024-11-16T12:10:15.382Z","updated_at":"2025-03-06T19:45:07.404Z","avatar_url":"https://github.com/tessapower.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Seeded Random Tests\n\nRepo for a talk I gave on randomized testing using RSpec and FFaker.\n\nSee also: [tessapower/my-pseudo-rand](https://github.com/tessapower/my-pseudo-rand).\n\nSometimes tests that make use of random data fail. In those cases, we want to be able reproduce the tests that failed\nwith the same data. This can be problematic if we're using a random data generator that generates new data on-the-fly.\nEach time we run tests, *new* random data is generated, so we cannot simply rerun the tests that failed. Not being able\nto reproduce tests makes them non-deterministic, which can lead to many an unproductive hour tracking down Heisenbugs.\n\nStill, we want to be able to benefit from using a random data generator to test our application, while also being\nable to reproduce tests if something fails. In other words, we want our random testing to be deterministic.\n\nThe good news is that random data generators are not truly random—they are *pseudo-random*—and we can easily reproduce\ntests to use the same random data in the exact same order. \n\nWhat follows is a short and sweet demonstration of how to set up our testrunner\n[RSpec](https://github.com/tessapower/seeded_random_tests#rspec) with\n[FFaker](https://github.com/tessapower/seeded_random_tests#ffaker) to achieve just that.\n\n---\n\n### A Brief \u0026 Important Sidenote: Random vs. Pseudo-Random\n\nSimply put, a Random **Data** Generator works by using a Random **Number** Generator (RNG) to decide what value to\nreturn next. Given a seed (starting value), an RNG will give you a sequence of random numbers.\n\nThis key insight here is: the sequence of numbers is not random, but *pseudo-random*, and the outcome of a\ndeterministic program.\n\nThough it's impossible for us to distinguish random numbers from pseudo-random numbers, we can reliably reproduce the\nlatter given we know the seed that was used to start the sequence.\n\nFor a better understanding and intuition on Pseudo-Random Number Generators, take a look at a working example\n[here](https://github.com/tessapower/my-pseudo-rand).\n\n---\n\n## Installation\n\n**Prerequisites:** Ruby (2.7)\n\nClone or fork this repository.\n\n```bash\n# Navigate to the root directory\ncd ./seeded_random_tests\n# Install the required Gems\nbundle install\n```\n\n## Configuration\n\nAfter adding `RSpec` to a project, running `rspec --init` generates a boilerplate `RSpec` configuration file called\n`spec_helper.rb`. We need to uncomment the following line in this file so tests can be run from the command line\nwith the `--seed` flag and will execute in the exact same order:\n\n```ruby\nconfig.order = :random\nKernel.srand config.seed\n```\n\nWe also need to add the following lines inside the `Rspec.configure do |config|` block to tell `FFaker` to use the same\nseed as `RSpec` and print out the seed once the test suite has finished:\n\n```ruby\nconfig.before(:all)  { FFaker::Random.seed=config.seed }\nconfig.before(:each) { FFaker::Random.reset! }\n```\n\nAnd with that, our tests will return the same data every time we run `Rspec` with the `--seed xxxxx` flag.\n\n## Run the Tests\n\n```bash\n# Run all tests\nbundle exec rspec\n\n# Run a specific test file\nbundle exec rspec ./spec/greeter_spec.rb\n\n# Run a specific test in a specific test file\nbundle exec rspec ./spec/greeter_spec.rb:16\n```\n\n![Greeter Test Run](./assets/greeter-test-run-52157.png)\n\n## Rerun the Same Tests with `--seed`\n\n```bash\nbundle exec rspec --seed 52157\n```\n![Greeter Test Rerun](./assets/greeter-test-rerun-52157.png)\n\n## Can We Reproduce Tests that were run in a Remote Environment?\n\n**Yes**—copy the seed that appears in the logs of the remote test run (GitHub Actions, CircleCi, Bitrise, etc.):\n\n![Remote Test Run](./assets/remote-test-run.png)\n\nRun the test command in your terminal with the `--seed xxxxx` flag:\n\n![Rerun of Remote Test Run Locally](./assets/run-remote-testrun-locally.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftessapower%2Fseeded_random_tests","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftessapower%2Fseeded_random_tests","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftessapower%2Fseeded_random_tests/lists"}