Skip to main content

pyproptest package

Project description

Pyprop - 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)], test)
  • prop_equalLength is the function for producing the test with generators
  • test is the test to be run
  • [bg.intListArb(10)] 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 pyprop.basicGenerators as bg
import pyprop.testing as pytest

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

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


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.

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.1.tar.gz (5.4 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.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyproptest-0.0.1.tar.gz
  • Upload date:
  • Size: 5.4 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.1.tar.gz
Algorithm Hash digest
SHA256 118040a9d0e413f3baf922386f2b01adf576d7774ef7bb9a982d69b968a5ee7e
MD5 7e42cc6157561f2a17391aaa7c9a9812
BLAKE2b-256 aaf58b673003512071356b0232d29742eadd9aaeda43614b78feb46fda79c832

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyproptest-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d0c570369f0bc83c1382f506a20a5cd6fa56c6b7c199abd320b77d9980d844e5
MD5 e2beb7436cf7abea21ba94e47de58b03
BLAKE2b-256 6594ed35b4b51fe86269362740f6eed200906d4d4e114340b1884d0e98800bd6

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