GithubHelp home page GithubHelp logo

pyqcheck's Introduction

Build Status Downloads

What is PyQCheck?

PyQCheck is a Quick Check-like testing framework based on the idea of Haskell Quick Check.

  • This framework is for random data testing.
  • The function's interface was referred from macchiato.js. (from the top page...)

Which python version does this support?

This framework worked on python version 3.3 or later

How to Install

> pip install PyQCheck

or

> easy_install PyQCheck

How to Use PyQCheck

Writing test

  • At method chain
from pyqcheck import PyQCheck, Arbitrary

def eq(x,y):
  return x * y == y * x and x + y == y + x

PyQCheck(verbose=True).add(
  Arbitrary('boolean', 'boolean').property(
    '!(x || y) == !x && !y', lambda x, y: (not(x or y)) == ((not x) and (not y))
  )
).add(
  Arbitrary('integer', 'integer').property(
    'x * y == y * x and x + y == y + x', eq
  )
).run(10).result() # run(10) is test count == 10
  • At decorator
from pyqcheck import PyQCheck, Arbitrary, set_arbitrary

@set_arbitrary('boolean', 'boolean')
def de_morgan(x, y):
  '''
  !(x || y) == !x && !y', lambda x, y: (not(x or y)) == ((not x) and (not y))
  '''
  return (not(x or y)) == ((not x) and (not y))

@set_arbitrary('integer', 'integer')
def eq(x, y):
  '''
  x * y == y * x and x + y == y + x
  '''
  return x * y == y * x and x + y == y + x

PyQCheck(verbose=True).run(10).result() # run(10) is test count == 10

Test Result.

----- PyQCheck test results... -----
label: !(x || y) == !x && !y
success: 10   
failure: 0
verbose:
  ☀  <lambda>(True, True)
  ☀  <lambda>(False, False)
  ☀  <lambda>(False, True)
  ☀  <lambda>(True, True)
  ☀  <lambda>(True, True)
  ☀  <lambda>(True, True)
  ☀  <lambda>(True, True)
  ☀  <lambda>(True, False)
  ☀  <lambda>(False, True)
  ☀  <lambda>(True, False)
-----
label: x * y == y * x and x + y == y + x
success: 10
failure: 0
verbose: 
  ☀  eq(5619883977492185900, 8098677974428651270)
  ☀  eq(1083625604502060169, 8458294345657310737)
  ☀  eq(4669876018772361359, 6247727992957273395)
  ☀  eq(8339760176857203915, 8345011171974202548)
  ☀  eq(7278259878279970866, 4100741748945006135)
  ☀  eq(4817262410454816318, 3084576882465980476)
  ☀  eq(2635121478675656588, 7568822804535567953)
  ☀  eq(6708571901087888356, 1255734967659271542)
  ☀  eq(2208040650061775673, 7460005457506446202)
  ☀  eq(5032528890931210411, 2911935080322536883)
-----

Supports arbitrary data type.

  • PyQString ('string')
  • PyQInteger ('integer')
  • PyQNumber ('number')
  • PyQHiragana ('hiragana')
  • PyQKatakana ('katakana')
  • PyQMomoclo ('momoclo')

Setting arbitrary limit.

from pyqcheck import PyQCheck, Arbitrary

PyQCheck().add(
  Arbitrary(
    ('integer', dict(min=10, max=100)), # range of 10 - 100
    ('integer', dict(min=30)), # range of 30 - max of default
  ).property(
    '10 <= x <= 100 and y >= 30', lambda x, y : 10 <= x <= 100 and y >= 30
  )
)

@set_arbitrary(
  ('string', dict(min=10)),
  ('integer', dict(max=30)))
def repeat(chars, n):
  '''
  (chars * n).split(chars) == n + 1
  '''
  repeat_string = chars * n
  return len(repeat_string.split(chars)) == n + 1

PyQCheck(verbose=True).run(10).result()

Test result.

----- PyQCheck test results... -----
label: 10 <= x <= 100 and y >= 30
success: 10   
failure: 0
verbose:
  ☀  <lambda>(73, 2056916856135336406)
  ☀  <lambda>(41, 1673657213508719924)
  ☀  <lambda>(86, 1591870911858630638)
  ☀  <lambda>(82, 7062489949342175354)
  ☀  <lambda>(63, 4626992076240878338)
  ☀  <lambda>(43, 7448218345050658578)
  ☀  <lambda>(73, 412908156141536339)
  ☀  <lambda>(45, 3541728769292566383)
  ☀  <lambda>(36, 8830667218769458940)
  ☀  <lambda>(73, 5267004395616289964)
-----
label: (chars * n).split(chars) == n + 1
success: 10
failure: 0
verbose: 
  ☀  repeat('f87YTMsjoob', 10)
  ☀  repeat('SeFwvHNrmLAQozfKq3JE', 17)
  ☀  repeat('N7rM3F9lraN', 23)
  ☀  repeat('npHHcqBCGWrnHy2Uy', 27)
  ☀  repeat('Xhv9XXynr9x0VBvmt', 5)
  ☀  repeat('raxB8IobOeFxNnfM4Mk', 1)
  ☀  repeat('W9uDeBi6bbA', 5)
  ☀  repeat('qSxlbKKN8DM', 16)
  ☀  repeat('0ystvLvvNdcQa2uRH', 13)
  ☀  repeat('638ssJbQBgnZ65Ohfy', 23)
-----

PyQCheck supports multi process.

from decimal import Decimal, getcontext
from pyqcheck import PyQCheck, Arbitrary

getcontext().proc = 60

PyQCheck(process=2).add(
  Arbitrary('integer', 'integer', 'integer').property(
    'x + y + z == z + y + x',
    lambda x, y, z : x + y + z == z + y + x
  )
).add(
  Arbitrary('number', 'number', 'number').property(
    'Decimal(x) + Decimal(y) + Decimal(z) == Decimal(z) + Decimal(y) + Decimal(x)',
    lambda x, y, z : Decimal(x) + Decimal(y) + Decimal(z) == Decimal(z) + Decimal(y) + Decimal(x)
  )
).add(
  Arbitrary(
    ('string', dict(max=10))
  ).property(
    'len(chars) <= 10',
    lambda chars : len(chars) <= 10
  )
).run(100).result()

Able to catch errors.

  • Sample
from pyqcheck import PyQCheck, Arbitrary

def ten_or_less(n):
  if n > 10:
    raise ValueError
  return True

PyQCheck(verbose=True).add(
  Arbitrary(('integer', dict(max=30))).property(
    'n <= 10 == True', ten_or_less, (ValueError,)
  )
).run(10).result()
  • Result
----- PyQCheck test results... -----
label: n <= 10 == True
success: 4
failure: 0
exceptions:
  ValueError: 6
verbose:
  ☀  ten_or_less(6)
  ☃  ten_or_less(27)
  ☃  ten_or_less(27)
  ☀  ten_or_less(5)
  ☃  ten_or_less(21)
  ☀  ten_or_less(10)
  ☃  ten_or_less(18)
  ☀  ten_or_less(2)
  ☃  ten_or_less(18)
  ☃  ten_or_less(12)
-----

Can create original arbitrary class.

from pyqcheck import PyQCheck, ArbitraryAbstraction, set_arbitrary

# Class
class CountryArbitrary(ArbitraryAbstraction):
  COUNTRIES = [
    'JAPAN',
    'GERMANY',
    'USA',
    'UK',
    'AUSTRALIA'
  ]

  def __init__(self):
    pass

  def generate(self):
    '''
    generate of random length array.
    '''
    import random
    return lambda : [random.choice(CountryArbitrary.COUNTRIES) for x in range(10)]

@set_arbitrary(CountryArbitrary)
def thisCountryIHaveEverBeenTo(country_name):
  '''
  issubset({'JAPAN', 'GERMANY', 'USA', 'UK', 'AUSTRALIA'})
  '''
  return set(country_name).issubset({'JAPAN', 'GERMANY', 'USA', 'UK', 'AUSTRALIA'})

PyQCheck(verbose=True).run(10).result()

License

MIT License

Copyright (c) <2012> Keiji Matsuzaki [email protected]

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Bitdeli Badge

pyqcheck's People

Contributors

futoase avatar gab-km avatar bitdeli-chef avatar

Stargazers

Angus H. avatar  avatar Harai Akihiro avatar raul avatar Hiroshi Hatake avatar Hoshinoma Araki avatar  avatar Sergey Miryanov avatar Yusuke SUGAMIYA avatar  avatar Kazuhiro Mizushima avatar

Watchers

Kazuhiro Mizushima avatar James Cloos avatar

pyqcheck's Issues

Propose new features with refactoring

Target: Extensions for PyQCheck

I'd like to add new features from another QuickCheck libraries (ex. scalacheck, etc) into PyQCheck. The features are like following:

for_all function

for_all function allows us to create property a little easier than we are writing now.

from pyqcheck import PyQCheck, for_all

PyQCheck(verbose=True).add(
  for_all(
    ('boolean', 'boolean'),
    '!(x || y) == !x && !y',
    lambda x, y: (not(x or y)) == ((not x) and (not y))
  )
).run(10).result()

This is equivalent to the code below:

from pyqcheck import PyQCheck, Arbitrary

PyQCheck(verbose=True).add(
  Arbitrary('boolean', 'boolean').property(
    '!(x || y) == !x && !y', lambda x, y: (not(x or y)) == ((not x) and (not y))
  )
).run(10).result()

from_gen function

Gen class and from_gen function allows us to create our own arbitrary with generator syntax in Python.

import random
import sys
from pyqcheck import PyQCheck, Gen, from_gen

def gen_int(min_int=1, max_int=None):
  min_int = min_int if isinstance(min_int, int) else 1
  max_int = max_int if max_int is not None and isinstance(max_int, int) else sys.maxsize
  while True:
    yield random.randint(min_int, max_int)

PyQCheck(verbose=True).add(
  from_gen([Gen(gen_int), Gen(gen_int)]).property(
    'x * y == y * x and x + y == y + x',
    lambda x, y: x * y == y * x and x + y == y + x
  )
).run(10).result()

We can limit these ranges.

from pyqcheck import PyQCheck, Gen, from_gen

def gen_int(min_int=1, max_int=None):
  min_int = min_int if isinstance(min_int, int) else 1
  max_int = max_int if max_int is not None and isinstance(max_int, int) else sys.maxsize
  while True:
    yield random.randint(min_int, max_int)

PyQCheck().add(
  from_gen(
    [Gen(gen_int, {"min": 10, "max": 100}), # range of 10 - 100
     Gen(gen_int, {"min": 30})]             # range of 30 - max of default
  ).property(
    '10 <= x <= 100 and y >= 30',
    lambda x, y : 10 <= x <= 100 and y >= 30
  )
).run(10).result()

from_gen_and_shrink function

from_gen_and_shrink function allows us to create our own arbitrary like from_gen, which can set a shrinker.
The shrinker is a procedure to minimize our test cases as we want.

import random
import sys
from pyqcheck import PyQCheck, Gen, from_gen_and_shrink

def gen_int(min_int=1, max_int=None):
  min_int = min_int if isinstance(min_int, int) else 1
  max_int = max_int if max_int is not None and isinstance(max_int, int) else sys.maxsize
  while True:
    yield random.randint(min_int, max_int)

PyQCheck(verbose=True).add(
  from_gen_and_shrink(
    [Gen(gen_int, {"min": 10, "max": 100})],
    lambda x: [x-2, x-1, x, x+1, x+2]        # limit test cases within 2 if failed
  ).property(
    '15 <= x <= 95,
    lambda x: 15 <= x <= 95
  )
).run(10).result()

It may result like this:

----- PyQCheck test results... -----
label: 15 <= x <= 95
success: 7
failure: 3
shrinks: 2 times
verbose:
  ☀  <lambda>(19)
  ☀  <lambda>(29)
  ☀  <lambda>(59)
  ☀  <lambda>(85)
  ☀  <lambda>(72)
  ☀  <lambda>(40)
  ☂  <lambda>(14)
  ☀  <lambda>(16)
  ☂  <lambda>(13)
  ☂  <lambda>(12)
-----

How to go: step by step

The features above are, however difficult to implement because of PyQCheck's current structure. For example, arbitrary module grabs almost all like a dictator or God.

So I would like to resolve this proposition step by step as following:

  1. Refactoring (or something to redesign) PyQCheck modules without changing PyQCheck APIs as much as we can
  2. Then, implementing the features

If you like it, I will try to do the job.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.