Skip to main content

Test-driven source code search for Python.

Project description

pytest-nodev is a simple test-driven search engine for Python code, it finds classes and functions that match the behaviour specified by the given tests.

How does “test-driven code search” work?

To be more precise pytest-nodev is a pytest plugin that helps you execute feature specification tests on all objects in the Python standard library and in all the modules you have installed.

Who are pytest-nodev users?

Python developers who’ve got better things to do than reinvent existing wheels.

I need to write a `parse_bool` function that robustly parses a boolean value from a string. Here is the test I intend to use to validate my own implementation once I write it.:

def test_parse_bool():
    assert not parse_bool('false')
    assert not parse_bool('FALSE')
    assert not parse_bool('0')

    assert parse_bool('true')
    assert parse_bool('TRUE')
    assert parse_bool('1')

Show me how I search for a ready-made implementation with pytest-nodev.

First, install the latest version of pytest-nodev from the Python Package Index:

$ pip install pytest-nodev

Then copy your specification test to the test_parse_bool.py file and decorate it with pytest.mark.candidate as follows:

@pytest.mark.candidate('parse_bool')
def test_parse_bool():
    assert not parse_bool('false')
    assert not parse_bool('FALSE')
    assert not parse_bool('0')

    assert parse_bool('true')
    assert parse_bool('TRUE')
    assert parse_bool('1')

Finally, instruct pytest to run your test on all functions in the Python standard library:

$ py.test test_parse_bool.py --candidates-from-stdlib
======================= test session starts ==========================
platform darwin -- Python 3.5.0, pytest-2.8.7, py-1.4.31, pluggy-0.3.1
rootdir: /tmp, inifile: setup.cfg
plugins: nodev-1.0.0, timeout-1.0.0
collected 3259 items

test_parse_bool.py xxxxxxxxxxxx[...]xxxxxxxxXxxxxxxxx[...]xxxxxxxxxxxx

============================== 1 hit =================================

test_parse_bool.py::test_parse_bool[distutils.util:strtobool] HIT

==== 3258 xfailed, 1 xpassed, 27 pytest-warnings in 45.07 seconds ====

In less than a minute pytest-nodev collected more than 3000 functions from the standard library and run your specification test on all of them and you’ve got a HIT. The strtobool function in the distutils.util module passes the test, so now you should thoroughly review it and if you like it you may use it in your code, no need to write your own implementation.

Wow! Does it work so well all the times?

To be honest strtobool is a little known gem of the Python standard library that is just perfect for illustrating all the benefits of test-driven code search. Here are some of them in rough order of importance:

  • a function imported is a one less function coded—and tested, documented, debugged, ported, maintained…

  • it’s battle tested code—lot’s of old bugs have already been squashed

  • it’s other people code—there’s an upstream to report new bugs to

  • it gives you additional useful functionality—for free on top of that

  • it’s in the Python standard library—no additional dependency required

BIG FAT WARNING!

A lot of functions called with the wrong set of arguments may have unexpected consequences ranging from slightly annoying, think os.mkdir('false'), to utterly catastrophic, think shutil.rmtree('/', True). Serious use of pytest-nodev, in particular using --candidates-from-all, require running the tests with operating-system level isolation, e.g. as a dedicated user or even better inside a dedicated container. The User’s guide documents how to run pytest-nodev safely and efficiently.

Project resources

Documentation

http://pytest-nodev.readthedocs.org

Support

https://stackoverflow.com/search?q=pytest-nodev

Development

https://github.com/nodev-io/pytest-nodev

Discussion

To be decided, see issue #15

Download

https://pypi-hypernode.com/pypi/pytest-nodev

Code quality

Build Status on Travis CI Build Status on AppVeyor Coverage Status on Coveralls

nodev website

http://nodev.io

Contributing

Contributions are very welcome. Please see the CONTRIBUTING document for the best way to help. If you encounter any problems, please file an issue along with a detailed description.

Authors:

Contributors:

Sponsors:

  • B-Open Solutions srl

License

pytest-nodev is free and open source software distributed under the terms of the MIT license.

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

pytest-nodev-0.9.8.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

pytest_nodev-0.9.8-py2.py3-none-any.whl (16.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pytest-nodev-0.9.8.tar.gz.

File metadata

File hashes

Hashes for pytest-nodev-0.9.8.tar.gz
Algorithm Hash digest
SHA256 5173cb216fd513a417b6204ed07002cfccb1bd48a1ea855d6a5983e9e57ac6de
MD5 a819cab856322f74cbf76302200d0b63
BLAKE2b-256 49063b0f2f8feb343f04801f1980a6fa693d4b07d95d51186b8605659970f2b7

See more details on using hashes here.

File details

Details for the file pytest_nodev-0.9.8-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_nodev-0.9.8-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4fe0803976083da040e76ca1a620796bcf402e217401c9dbeecd1e6c7f8657f4
MD5 d4659af2f8f3d7dcdfcbaa17774a6010
BLAKE2b-256 639354a25472b9450e346cbab9c46ccd4c4040898292d76be156e2e88330e536

See more details on using hashes here.

Supported by

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