testvars - Place-holders in complex test assertions
Sometimes, when writing tests, you want to assert that a data structure computed in a test matches some expected value, but the expected value might have components, such as times or generated ids that can’t be predicted ahead of time. Options in this situation include:
break the assertion into many assertions about various parts of the data structure.
Use mocking/stubbing to control things like time and ids to make unpredictable parts of the data predictable.
Both of these approaches tend to be pretty tedious and potentially error prone.
The tiny testvars package provides an alternative that is much easier to use in many cases. The package provides a Vars class:
>>> import testvars >>> vars = testvars.Vars()
When Vars attributes are compared equal the first time, the comparison succeeds and alse sets their value:
>>> vars.x == 1 True
At that point, the value is set, and you can make assertions against it:
>>> vars.x 1 >>> vars.x > 0 True
Further comparisons are against this set value:
>>> vars.x == 2 False
This is useful when dealing with data structures with unpredictable parts:
>>> expected = {'id': vars.child_id, 'name':"Alex"}
>>> {'id': 32, 'name':"Alex"} == expected
True
It’s also interesting when, while you don’t know what values will be, you can make assertions about how values are related:
>>> expected = {'id': vars.parent_id, 'name':'Cas', ... 'children': [vars.child_id]} >>> expected == {'id': 42, 'name':'Cas', 'children':[32]} True >>> expected == {'id': 42, 'name':'Cas', 'children':[33]} False>>> [vars.y, 2, 3, vars.y] == [9, 2, 3, 9] True>>> [vars.z, 2, 3, vars.z] == [8, 2, 3, 9] False
Changes
1.0.0 (2017-07-07)
Initial release
Metadata
Release files for testvars 1.0.0
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 |
|---|---|---|---|---|
| testvars-1.0.0.tar.gz | 3.5 kB | Without provenance | Details for testvars-1.0.0.tar.gz |
Release files / testvars-1.0.0.tar.gz
| Download URL | testvars-1.0.0.tar.gz |
|---|---|
| Size | 3.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ce3c6bfae70b7be4f96a6b659958a242ac1fa7ac079d97b27693f31591da868e
|
|
BLAKE2b-256 checksum How to use checksums |
eeba8d21aa805bdb7ae8801a0debba66af0dab741a35521bbffefa0b5a635542
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for testvars 1.0.0
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.