Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Property Testing library in Ruby

License

NotificationsYou must be signed in to change notification settings

Qqwy/ruby-prop_check

Repository files navigation

PropCheck allows you to do Property Testing in Ruby.

GemRuby RSpec tests build statusMaintainabilityRubyDoc

It features:

  • Generators for most common Ruby datatypes.
  • An easy DSL to define your own generators (by combining existing ones, as well as completely custom ones).
  • Shrinking to a minimal counter-example on failure.
  • Hooks to perform extra set-up/cleanup logic before/after every example case.

It requiresno external dependencies, and integrates well with all common test frameworks (see below).

What is PropCheck?

PropCheck is a Ruby library to create unit tests which are simpler to write and more powerful when run, finding edge-cases in your code you wouldn't have thought to look for.

It works by letting you write tests that assert that something should be true forevery case, rather than just the ones you happen to think of.

A normal unit test looks something like the following:

  1. Set up some data.
  2. Perform some operations on the data.
  3. Assert something about the result

PropCheck lets you write tests which instead look like this:

  1. For all data matching some specification.
  2. Perform some operations on the data.
  3. Assert something about the result.

This is often called property-based testing. It was popularised by the Haskell libraryQuickCheck.PropCheck takes further inspiration from Erlang'sPropEr, Elixir'sStreamData and Python'sHypothesis.

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 never crash.
  • If you remove an object, you can no longer see it
  • 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:

  • Finalize the testing DSL.
  • Testing the library itself (against known 'true' axiomatically correct Ruby code.)
  • Customization of common settings
  • Filtering generators.
  • Customize the max. of samples to run.
  • Stop after a ludicrous amount of generator runs, to prevent malfunctioning (infinitely looping) generators from blowing up someone's computer.
  • Look into customization of settings from e.g. command line arguments.
  • Good, unicode-compliant, string generators.
  • Filtering generator outputs.
  • Before/after/around hooks to add setup/teardown logic to be called before/after/around each time a check is run with new data.
  • Possibility to resize generators.
  • #instance generator to allow the easy creation of generators for custom datatypes.
  • Builtin generation ofSets
  • Builtin generation ofDates,Times andDateTimes.
  • Configuration option to resize all generators given to a particular Property instance.
  • A simple way to create recursive generators
  • A usage guide.

Nice-to-haves

  • Stateful property testing. If implemented at some point, will probably happen in a separate add-on library.

Installation

Add this line to your application's Gemfile:

gem'prop_check'

And then execute:

$ bundle

Or install it yourself as:

$ gem install prop_check

Usage

Using PropCheck for basic testing

Propcheck exposes theforall method.It takes any number of generators as arguments (or keyword arguments), as well as a block to run.The value(s) generated from the generator(s) passed to theforall will be given to the block as arguments.

Raise an exception from the block if there is a problem. If there is no problem, just return normally.

G=PropCheck::Generators# testing that Enumerable#sort sorts in ascending orderPropCheck.forall(G.array(G.integer))do |numbers|sorted_numbers=numbers.sort# Check that no number is smaller than the previous numbersorted_numbers.each_cons(2)do |former,latter|raise"Elements are not sorted!#{latter} is <#{former}"iflatter >formerendend

Here is another example, using it inside a test case.Here we check ifnaive_average indeed always returns an integer for all arrays of numbers we can pass it:

# Somewhere you have this function definition:defnaive_average(array)array.sum /array.lengthend

The test case, using RSpec:

require'rspec'RSpec.describe"#naive_average"doG=PropCheck::Generatorsit"returns an integer for any input"doPropCheck.forall(G.array(G.integer))do |numbers|result=naive_average(numbers)expect(result).tobe_a(Integer)endendend

The test case, using MiniTest:

require'minitest/autorun'classNaiveAverageTest <MiniTest::Unit::TestCaseG=PropCheck::Generatorsdeftest_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)endendend

The test case, using test-unit:

require"test-unit"classTestNaiveAverage <Test::Unit::TestCaseG=PropCheck::Generatorsdeftest_that_it_returns_an_integer_for_any_inputPropCheck.forall(G.array(G.integer))do |numbers|result=naive_average(numbers)assert_instance_of(Integer,result)endendend

The test case, using only vanilla Ruby:

# And then in a test case:G=PropCheck::GeneratorsPropCheck.forall(G.array(G.integer))do |numbers|result=naive_average(numbers)raise"Expected the average to be an integer!"unlessresult.is_a?(Integer)end

When running this particular example PropCheck very quickly finds out that we have made a programming mistake:

ZeroDivisionError:(after6successfulpropertytestruns)Failedon:`{    :numbers => []}`Exceptionmessage:---dividedby0---(shrinkingimpossible)---

Clearly we forgot to handle the case of an empty array being passed to the function.This is a good example of the kind of conceptual bugs that PropCheck (and property-based testing in general)are able to check for.

Shrinking

When a failure is found, PropCheck will re-run the block given toforall to test'smaller' inputs, in an attempt to give you a minimal counter-example,from which the problem can be easily understood.

For instance, when a failure happens with the inputx = 100,PropCheck will see if the failure still happens withx = 50.If it does , it will tryx = 25. If not, it will tryx = 75, and so on.

This means for example that if something only goes for wrong forx >= 8, the program will try:

  • x = 100(fails),
  • x = 50(fails),
  • x = 25(fails),
  • x = 12(fails),
  • x = 6(succeeds),x = 9 (fails)
  • x = 7(succeeds),x = 8 (fails).

and thus the simplified case ofx = 8 is shown in the output.

The documentation of the provided generators explain how they shrink.A short summary:

  • Integers shrink to numbers closer to zero.
  • Negative integers also attempt their positive alternative.
  • Floats shrink similarly to integers.
  • Arrays and hashes shrink to fewer elements, as well as shrinking their elements.
  • Strings shrink to shorter strings, as well as characters earlier in their alphabet.

Builtin Generators

PropCheck comes withmany builtin generators in the PropCheck::Generators module.

It contains generators for:

  • (any, positive, negative, etc.) integers,
  • (any, only real-valued) floats,
  • (any, printable only, alphanumeric only, etc) strings and symbols
  • fixed-size arrays and hashes
  • as well as varying-size arrays, hashes and sets.
  • dates, times, datetimes.
  • and many more!

It is common and recommended to set up a module alias by usingG = PropCheck::Generators in e.g. your testing-suite files to be able to refer to all of them.(Earlier versions of the library recommended including the module instead. But this will make it very simple to accidentally shadow a generator with a local variable namedfloat orarray and similar.)

Writing Custom Generators

As described in the previous section, PropCheck already comes bundled with a bunch of common generators.

However, you can easily adapt them to generate your own datatypes:

Generators#constant / Generator#wrap

Always returns the given value. No shrinking.

Generator#map

Allows you to take the result of one generator and transform it into something else.

>> G.choose(32..128).map(&:chr).sample(1, size: 10, rng: Random.new(42))=> ["S"]

Generator#bind

Allows you to create one or another generator conditionally on the output of another generator.

>> G.integer.bind { |a| G.integer.bind { |b| G.constant([a , b]) } }.sample(1, size: 100, rng: Random.new(42)=> [[2, 79]]

This is an advanced feature. Often, you can use a combination ofGenerators.tuple andGenerator#map instead:

>> G.tuple(G.integer, G.integer).sample(1, size: 100, rng: Random.new(42))=> [[2, 79]]

Generators.one_of

Useful if you want to be able to generate a value to be one of multiple possibilities:

>> G.one_of(G.constant(true), G.constant(false)).sample(5, size: 10, rng: Random.new(42))=> [true, false, true, true, true]

(Note that for this example, you can also useG.boolean. The example happens to show how it is implemented under the hood.)

Generators.frequency

Ifone_of does not give you enough flexibility because you want some results to be more common than others,you can useGenerators.frequency which takes a hash of (integer_frequency => generator) keypairs.

>> G.frequency(5 => G.integer, 1 => G.printable_ascii_char).sample(size: 10, rng: Random.new(42))=> [4, -3, 10, 8, 0, -7, 10, 1, "E", 10]

Others

There are even more functions in theGenerator class and theGenerators module that you might want to use,although above are the most generally useful ones.

PropCheck::Generator documentationPropCheck::Generators documentation

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 thedatabase_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:
# Version of PropCheck.forall# which ensures records persisted to the DB in one generated example# do not affect any otherdefforall_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/test-unit/etc., use the before/after/around hooks exposed by PropCheck.

Development

After checking out the repo, use thejust command runner for common tasks:

  • just setup: Installs dev dependencies
  • just test: Runs the test suite
  • just console: Opens an IRb console with the gem loaded for experimenting.
  • just install: Install the gem on your local machine.
  • just release: Create and push a new release to the git repo and Rubygems. (Be sure to increase the version number inversion.rb first!)

Contributing

Bug reports and pull requests are welcome on GitHub athttps://github.com/Qqwy/ruby-prop_check . This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to theContributor Covenant code of conduct.

License

The gem is available as open source under the terms of theMIT License.

Code of Conduct

Everyone interacting in the PropCheck project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow thecode of conduct.

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 testingwith his great talk about stateful property testing,as well as Fred Herbert for his great bookProperty-Based Testing with PropEr, Erlang and Elixir 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 projects:

About

Property Testing library in Ruby

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors8

Languages


[8]ページ先頭

©2009-2025 Movatter.jp