Python Assertion Helpers inspired by Shouldly
Project description
Requirements
forbiddenfruit
a version of python with which forbidden fruit will work (must implement the CTypes/CPython Python API)
Python 2.7 or 3.3 (it may work with other versions, such as other 3.x versions, but it has not been tested with these versions)
Assertions
See ASSERTIONS.rst
Example
>>> import should_be.all >>> class Cheese(object): ... crackers = 3 ... >>> swiss = Cheese() >>> swiss.crackers.should_be(4) AssertionError: swiss.crackers should have been 4, but was 3
Installation
The easy way
$ sudo pip install https://github.com/DirectXMan12/should_be.git#egg=ShouldBe
The slightly-less-easy way
$ git clone https://github.com/DirectXMan12/should_be.git
$ cd should_be
$ ./setup.py build
$ sudo ./setup.py install
Extending
Writing your own assertions is fairly easy. There are two core parts of
ShouldBe: BaseMixin
and should_follow.
All assertions should be placed in classes that inherit from BaseMixin
.
BaseMixin
provides the basic utilities for extending built-in objects
with your assertions.
The class which holds your assertions should have a class variable called
target_class
. This is the class on which your assertions will be run.
By default, this is set to object
. If you wish to have your assertions
run on object
, there are a few additional considerations to make (see
warning below).
Then, assertions should be defined as instance methods. Each method should call
self.should_follow
one or more times. Think of should_follow as assertTrue
on steroids. It has the following signature:
should_follow(self, assertion, msg=None, **kwargs)
. Obviously, assertion is
an expression which, when False
, causes should_follow to raise an AssertionError.
So far, pretty normal. msg
is where things get interesting. msg should be
a new-style Python format string which contains only named substitutions. By
default, should_follow
will pass the txt and self keys to the format method,
in addition to any keyword arguments passed to should_follow
. self is, obviously,
the current object. txt
is the code that represents the current object. For instance,
if we wrote (3).should_be(4)
, txt would be ‘(3)’. If we wrote
cheese.variety.should_be('cheddar')
, txt would be ‘cheese.variety’.
Once all of your assertions are written, you can simply write
MyAssertionMixin.mix()
to load your assertions. A setuptools
hook is on the way for autoloading custom assertion mixins
with import should_be.all
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file shouldbe-0.1.2.tar.gz
.
File metadata
- Download URL: shouldbe-0.1.2.tar.gz
- Upload date:
- Size: 13.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f28a330ef849835bd242bb851eb7cbad8e1a7d8b26b3b79c27b485e3badceb9b |
|
MD5 | 5807c5ab1c5dac83f49a0ada243d9e74 |
|
BLAKE2b-256 | f17d79c9e9bf95ae86b73dee2bc4888d35a7b86f3d6b0acbcc22e3154861e84e |