Skip to main content

nodev helpers to write specification tests.

Project description

nodev.specs helps you write robust tests that describe the abstract behaviour of your code leaving many implementation details out of your tests.

The general idea is best explained with an example, let’s write a specification test for the following function skip_comments that returns the non-comment part of every line in the input file:

def skip_comments(stream):
    return [line.partition('#')[0] for line in stream]

The simplest unit test may look like the following:

def test_skip_comments():
    assert skip_comments(['# comment']) == ['']
    assert skip_comments(['value # comment']) == ['value ']
    assert skip_comments(['value 1', '', 'value 2']) == ['value 1', '', value 2']

Such a unit test is much more tied to current skip_comments implementation than it needs to be and the test will need update every time a tiny feature is added, like turning the function into a generator:

def skip_comments(stream):
    for line in stream:
        yield line.partition('#')[0]

This can be fixed by re-writing the test in more generic way:

def test_skip_comments():
    assert '' in skip_comments(['# comment'])
    assert 'value ' in skip_comments(['value # comment'])
    assert 'value 1' in skip_comments(['value 1', '', 'value 2'])
    assert 'value 2' in skip_comments(['value 1', '', 'value 2'])

Let’s re-write the test making use of the nodev.specs.FlatContainer helper:

def test_skip_comments():
    assert '' in FlatContainer(skip_comments(['# comment']))
    assert 'value ' in FlatContainer(skip_comments(['value # comment']))
    assert 'value 1' in FlatContainer(skip_comments(['value 1', '', 'value 2']))
    assert 'value 2' in FlatContainer(skip_comments(['value 1', '', 'value 2']))

Now you can choose to skip empty lines returning the current line index instead:

def skip_comments(stream):
    for index, line in enumerate(stream):
        value = line.partition('#')[0]
        if value:
            yield index, value

Or return also the comment for every line:

def skip_comments(stream):
    for index, line in enumerate(stream):
        value, sep, comment = line.partition('#')
        if value:
            yield index, value, sep + comment

The nodev test needs no update because it makes almost no assumption on the details of the return value.

Project resources

Support

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

Development

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

Discussion

To be decided, see issue #15 of the pytest-nodev repository.

Download

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

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:

Sponsors:

License

nodev.specs 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

nodev.specs-0.3.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

nodev.specs-0.3.0-py2.py3-none-any.whl (8.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file nodev.specs-0.3.0.tar.gz.

File metadata

  • Download URL: nodev.specs-0.3.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for nodev.specs-0.3.0.tar.gz
Algorithm Hash digest
SHA256 97e8e4e30096add23bf3ac6c28977981d7915789d12133854fe3ca2aef9ea366
MD5 98fc9db9507ac4672a1b235b9ed6ba5a
BLAKE2b-256 0a67aae5da30af6fb58f09fa0637ac2663f98bffbf0fab84bfd4268afe6d00bf

See more details on using hashes here.

File details

Details for the file nodev.specs-0.3.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for nodev.specs-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3a6bbe733fdc427f60043931ab1f72772d87df04a265151c117ddbbe2e69864d
MD5 e33fe69e9cff389fee2650832db254d4
BLAKE2b-256 f974308c33aa890ebe3e758b714fec2d550b137f35e19e53883ce03a7a17a22c

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