Skip to main content

pyproptest package

Project description

Pyproptest - property based testing made simple (maybe)

Overview:

This package was created to allow Python users to utilise property based testing in a similar manner to Haskell's QuickCheck package.

Property based testing allows for a more complete 'proving' of a program's correctness by ensuring the overall behaviour of functions is correct. This is in contrast to testing individual test cases - a unit testing approach. Property based testing is, however, not particularly well suited to testing very stateful programs.

Thinking Correctly:

E.g. Testing that a sorting function works as intended.

In this case, to ensure that the function mysort is behaving correctly, we may want to test that is has the following properties:

  • the items in the resulting list are in sorted order (kind of obvious - we want the sorting algorithm to sort things)
  • every unique value from the input list appears in the output list the same number of times (no values are disappearing/being duplicated)
  • the resulting list has the same number of items as the original list (ensure that new items aren't introduced)

If these properties hold, it can be concluded that the sorting algorithm is performing correctly.

Usage:

The general structure of a test within a class is as follows:

@staticmethod
def prop_equalLength():
    def test(i):
        return len(i) == len(sorted(i))
    return ([bg.intListArb(10,-100,100)], test)
  • prop_equalLength is the function for producing the test with generators
  • test is the test to be run
  • [bg.intListArb(10,-100,100)] is the list of generators used to produce the test function's inputs (i)

or, using the decorator pattern:

@staticmethod
@bg.test([bg.intListArb(10,-100,100)])
def prop_equalLength(i):
     return len(i) == len(sorted(i))
  • prop_equalLength is the test function
  • bg.test decorator specifies generators (bg refers to pyproptest.basicGenerators)
  • [bg.intListArb(10,-100,100)] is the list of generators used to produce the test function's inputs (i)

The following is an implementation of some tests for ensuring that the sorting function sorted works correctly. The methodology follows that described in the previous section:

import pyproptest.basicGenerators as bg
import pyproptest.testing as pytest

class sortingTests:
    @staticmethod
    @bg.test([bg.intListArb(10,-100,100)])
    def prop_equalLength(i):
        return len(i) == len(sorted(i))

    @staticmethod
    @bg.test([bg.intListArb(10,-100,100)])
    def prop_sortedResult(i):
        res = sorted(i)
        return all([res[i] <= res[i + 1] for i in range(len(res) - 1)])
    
    @staticmethod
    @bg.test([bg.intListArb(10,-100,100)])
    def prop_containsSameElements(i):
        res = sorted(i)
        ifreq = {x:i.count(x) for x in i}
        return ifreq == {x:res.count(x) for x in res}


testerObj = pytest.tester(classes = [sortingTests])
testerObj.runTests()

This produces output:

testing sortingTests:
testing prop_containsSameElements:		ran 100 test(s), with 0 failure(s)
testing prop_equalLength:		ran 100 test(s), with 0 failure(s)
testing prop_sortedResult:		ran 100 test(s), with 0 failure(s)
==================================================
0 test(s) failed (100.0%):
==================================================

Signifying that all of the tests passed, and the sorted() function has all of the properties which we want it to have.

Defining new generators:

Generators for producing the arbitrary input to property testing functions take the form of a wrapper function which returns a generator. For generating arbitrary integers, the following generator - available in basicGenerators, can be used:

def intArb(minBound = -sys.maxsize, maxBound = sys.maxsize):
    def gen():
        while True:
            yield random.randint(minBound, maxBound)
    return gen

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyproptest-0.0.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyproptest-0.0.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file pyproptest-0.0.2.tar.gz.

File metadata

  • Download URL: pyproptest-0.0.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for pyproptest-0.0.2.tar.gz
Algorithm Hash digest
SHA256 1f607e439594737a04cfadb79c29d6f045afa109b95560fa5561af2036ed37c0
MD5 26e9abc2cc96a5aaec319b3b4be4a5ec
BLAKE2b-256 fb877497ea0ed83ca21c7b310ebda969809f110a0bd5f497c9c250e6e0dc7a31

See more details on using hashes here.

File details

Details for the file pyproptest-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pyproptest-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for pyproptest-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 19c16eec3bf3a8f7d2e88fc3d8234dca575ed377923af05eb9ef2409499ecebf
MD5 0379a618ccb2d4390ce7f230f5bbddb7
BLAKE2b-256 ff4121dfbcfa09e2b9545c1f6621d9ae505d2cb732aeabe9b76124ad308d56b3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page