README.md in prop_check-0.17.0 vs README.md in prop_check-0.18.0

- old
+ new

@@ -38,13 +38,13 @@ It works by generating arbitrary data matching your specification and checking that your assertions still hold in that case. If it finds an example where they do not, it takes that example and shrinks it down, simplifying it to find the smallest example that still causes the problem. Writing these kinds of tests usually consists of deciding on guarantees that your code should have -- properties that should always hold true, regardless of wat the world throws at you. Some examples are: -- Your code should not throw an exception, or only a particular type of exception. +- Your code should never crash. - If you remove an object, you can no longer see it -- If you serialize and then deserializea value, you get the same value back. +- If you serialize and then deserialize a value, you get the same value back. ## Implemented and still missing features Before releasing v1.0, we want to finish the following: @@ -105,11 +105,11 @@ PropCheck.forall(G.array(G.integer)) do |numbers| sorted_numbers = numbers.sort # Check that no number is smaller than the previous number sorted_numbers.each_cons(2) do |former, latter| - raise "Elements are not sorted! #{latter} is < #{former}" if latter < former + raise "Elements are not sorted! #{latter} is < #{former}" if latter > former end end ``` @@ -120,37 +120,58 @@ # Somewhere you have this function definition: def naive_average(array) array.sum / array.length end ``` -```ruby -# And then in a test case: -G = PropCheck::Generators -PropCheck.forall(numbers: G.array(G.integer)) do |numbers:| - result = naive_average(numbers) - unless result.is_a?(Integer) do - raise "Expected the average to be an integer!" - end -end -# Or if you e.g. are using RSpec: -describe "#naive_average" do - include PropCheck +The test case, using RSpec: +``` ruby +require 'rspec' + +RSpec.describe "#naive_average" do G = PropCheck::Generators it "returns an integer for any input" do - forall(numbers: G.array(G.integer)) do |numbers:| - result = naive_average(numbers) + PropCheck.forall(G.array(G.integer)) do |numbers| + result = naive_average(numbers) + expect(result).to be_a(Integer) end end end ``` -When running this particular example PropCheck very quickly finds out that we have made a programming mistake: +The test case, using MiniTest: +``` ruby +require 'minitest/autorun' +class NaiveAverageTest < MiniTest::Unit::TestCase + G = PropCheck::Generators + def test_that_it_returns_an_integer_for_any_input() + PropCheck.forall(G.array(G.integer)) do |numbers| + result = naive_average(numbers) + + assert_instance_of(Integer, result) + end + end +end +``` + +The test case, using only vanilla Ruby: ```ruby +# And then in a test case: +G = PropCheck::Generators + +PropCheck.forall(G.array(G.integer)) do |numbers| + result = naive_average(numbers) + + raise "Expected the average to be an integer!" unless result.is_a?(Integer) +end +``` + +When running this particular example PropCheck very quickly finds out that we have made a programming mistake: +```ruby ZeroDivisionError: (after 6 successful property test runs) Failed on: `{ :numbers => [] @@ -268,10 +289,30 @@ although above are the most generally useful ones. [PropCheck::Generator documentation](https://www.rubydoc.info/github/Qqwy/ruby-prop_check/master/PropCheck/Generator) [PropCheck::Generators documentation](https://www.rubydoc.info/github/Qqwy/ruby-prop_check/master/PropCheck/Generators) + +## Usage within Rails / with a database + +Using PropCheck for unit tests in a Rails, Sinatra, Hanami, etc. project is very easy. +Here are some simple recommendations for the best results: +- Tests that do not need to use the DB at all are usually 10x-100x faster. Faster tests means that you can configure PropCheck to do more test runs. +- If you do need to use the database, use the [database_cleaner](https://github.com/DatabaseCleaner/database_cleaner) gem, preferibly with the fast `:transaction` strategy if your RDBMS supports it. To make sure the DB is cleaned around each generated example, you can write the following helper: +``` ruby + # Version of PropCheck.forall + # which ensures records persisted to the DB in one generated example + # do not affect any other + def forall_with_db(*args, **kwargs, &block) + PropCheck.forall(*args, **kwargs) + .before { DatabaseCleaner.start } + .after { DatabaseCleaner.clean } + .check(&block) + end +``` +- Other setup/cleanup should also usually happen around each generated example rather than around the whole test: Instead of using the hooks exposed by RSpec/MiniTest/etc., use the before/after/around hooks exposed by PropCheck. + ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). @@ -284,18 +325,18 @@ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). ## Code of Conduct -Everyone interacting in the PropCheck project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/prop_check/blob/master/CODE_OF_CONDUCT.md). +Everyone interacting in the PropCheck project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Qqwy/ruby-prop_check/blob/master/CODE_OF_CONDUCT.md). ## Attribution and Thanks I want to thank the original creators of QuickCheck (Koen Claessen, John Hughes) as well as the authors of many great property testing libraries that I was/am able to use as inspiration. I also want to greatly thank Thomasz Kowal who made me excited about property based testing [with his great talk about stateful property testing](https://www.youtube.com/watch?v=q0wZzFUYCuM), as well as Fred Herbert for his great book [Property-Based Testing with PropEr, Erlang and Elixir](https://propertesting.com/) which is really worth the read (regardless of what language you are using). -The implementation and API of PropCheck takes a lot of inspiration from the following pre-existing libraries: +The implementation and API of PropCheck takes a lot of inspiration from the following projects: - Haskell's [QuickCheck](https://hackage.haskell.org/package/QuickCheck) and [Hedgehog](https://hackage.haskell.org/package/hedgehog); - Erlang's [PropEr](https://hex.pm/packages/proper); - Elixir's [StreamData](https://hex.pm/packages/stream_data); - Python's [Hypothesis](https://hypothesis.works/).