This project has been archived by its maintainers, and is no longer receiving any updates.
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 |
|
Development |
|
Discussion |
To be decided, see issue #15 of the pytest-nodev repository. |
Download |
|
Code quality |
|
nodev website |
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:
Alessandro Amici - @alexamici
Sponsors:
License
nodev.specs is free and open source software distributed under the terms of the MIT license.
Metadata
Release files for nodev.specs 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| nodev.specs-0.3.1.tar.gz | 6.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nodev.specs-0.3.1-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 15.1 kB
Release files / nodev.specs-0.3.1.tar.gz
| Download URL | nodev.specs-0.3.1.tar.gz |
|---|---|
| Size | 6.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
209506e6c73be3c0a007039f97ad4b83cb96443f4e876732e0b07c5a9cee54de
|
|
BLAKE2b-256 checksum How to use checksums |
c0514e85802fbdec9cb65f44a8f1a73a7b804b7ae957ff45ac2d2759a536ea63
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / nodev.specs-0.3.1-py2.py3-none-any.whl
| Download URL | nodev.specs-0.3.1-py2.py3-none-any.whl |
|---|---|
| Size | 8.3 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
588fc8dd84bf14d8b78f14228b8211d9117314484ca36905cca8821629f00919
|
|
BLAKE2b-256 checksum How to use checksums |
ee9146c6857b7d32b8b8083215dc7da8e41ea69afe2d8f2baa358f68848e8036
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |