Python wrap cases
Simple library for generate test cases with parameters.
What is this?
This library helps to generate tests with parameters.
Let’s write some tests for this function:
import re
def clear_start_end_dash(string):
return re.sub(r'^[\s\-]*-|-[\s\-]*$', '', string)
We may write something like this:
from unittest import TestCase
class ClearStartEndDashTest(TestCase):
def test_remove_first_dash(self):
result = clear_start_end_dash('-my string')
self.assertEqual(result, 'my string')
def test_remove_all_first_dashes(self):
result = clear_start_end_dash('- -- --my string')
self.assertEqual(result, 'my string')
def test_remove_last_dash(self):
result = clear_start_end_dash('my string-')
self.assertEqual(result, 'my string')
def test_remove_all_last_dashes(self):
result = clear_start_end_dash('my string-- -- -- - ')
self.assertEqual(result, 'my string')
def test_keep_dash_at_center(self):
result = clear_start_end_dash('my-string')
self.assertEqual(result, 'my-string')
It’s good, but we spent a lot of time to write those absolutely the same test functions.
So let’s decrease the number of duplicate functions:
from unittest import TestCase
class ClearStartEndDashDryTest(TestCase):
def test_remove_dash(self):
cases = (
('-my string', 'my string'),
('- -- --my string', 'my string'),
('my string-', 'my string'),
('my string-- -- -- - ', 'my string'),
('my-string', 'my-string')
)
for string, expected_result in cases:
result = clear_start_end_dash(string)
self.assertEqual(result, expected_result)
This code has a few problems:
Easy to write but difficult to read.
We can’t use test fixture (setUp, tearDown) with each case.
If some case fails, the other cases won’t run.
If test test_remove_dash fails, it won’t help us find out what happened.
Look how easy we may solve these problems using this library:
from unittest import TestCase
from python_wrap_cases import wrap_case
@wrap_case
class ClearStartEndDashWrapTest(TestCase):
@wrap_case('-my string', 'my string')
@wrap_case('- -- --my string', 'my string')
@wrap_case('my string-', 'my string')
@wrap_case('my string-- -- -- - ', 'my string')
@wrap_case('my-string', 'my-string')
def test_remove_dash(self, string, expected_result):
result = clear_start_end_dash(string)
self.assertEqual(result, expected_result)
This code generates 5 tests, that works like a simple test functions.
Console output:
test_remove_dash_u'- -- --my string'_u'my string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
test_remove_dash_u'-my string'_u'my string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
test_remove_dash_u'my string-'_u'my string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
test_remove_dash_u'my string-- -- -- - '_u'my string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
test_remove_dash_u'my-string'_u'my-string' (tests.example.test_simple_test.ClearStartEndDashWrapTest) ... ok
Installation
pip install python_wrap_cases
Free software: BSD license
Documentation: https://python_wrap_cases.readthedocs.org.
History
0.1.0 (2015-06-26)
First release on PyPI.
0.1.2 (2015-06-26)
Fix generators import
0.1.3 (2015-06-26)
Add some docs
0.1.4 (2015-06-29)
ReadMe add semicolon
Fix pypi readme
0.1.5 (2015-07-01)
README remove ::;
0.1.6 (2015-07-01)
Add tests fot python 3.2
0.1.7 (2015-07-10)
Add six dependency
0.1.8 (2015-08-21)
Add func generator
Add range generator
Fix problem with iterator in custom generator
Add new API for declaration wrapped TestCase. (added wrap_case decorator without parameters)
Metadata
Release files for python_wrap_cases 0.1.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| python_wrap_cases-0.1.8.tar.gz | 20.2 kB | Without provenance | Details for python_wrap_cases-0.1.8.tar.gz |
Release files / python_wrap_cases-0.1.8.tar.gz
| Download URL | python_wrap_cases-0.1.8.tar.gz |
|---|---|
| Size | 20.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
924ef09da7934d743440c1de9f76bb530d79f6b4e0af23b3c8c372452c0026c8
|
|
BLAKE2b-256 checksum How to use checksums |
a9a22542622e32244f016e83115677be96344cba9a17dea6461fb7545b6b6956
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for python_wrap_cases 0.1.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.