• Stars
    star
    103
  • Rank 333,046 (Top 7 %)
  • Language
    Python
  • Created almost 14 years ago
  • Updated almost 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Expecter Gadget: better expectations (assertions) for Python.

BASICS

Expecter Gadget helps you to write assertions. Never again will you forget which is expected and which is actual!

Basic expectations are easy:

>>> from expecter import expect
>>> expect('some' + 'thing') == 'something'
expect('something')
>>> expect(1) > 100
Traceback (most recent call last):
...
AssertionError: Expected something greater than 100 but got 1

Just read the expectations like a sentence. "expect(2) == 1 + 1" reads as "Expect 2 to equal 1 + 1". Obviously, the expectation is about 2, and it's being compared to 1 + 1. No ambiguity!

EXCEPTIONS

Expectations about exceptions use the "with" statement. Everything is good if the expected exception is raised:

>>> from __future__ import with_statement
>>> with expect.raises(KeyError):
...     {}[123]

If it's not raised, Expecter Gadget will raise an AssertionError:

>>> with expect.raises(KeyError):
...     pass
Traceback (most recent call last):
...
AssertionError: Expected an exception of type KeyError but got none

Exceptions that don't match the expected one will not be swallowed, so your test will error as you expect:

>>> from __future__ import with_statement
>>> with expect.raises(NameError):
...     {}[123]
Traceback (most recent call last):
...
KeyError: 123

CUSTOM EXPECTATIONS

You can add a custom expectation with the add_expectation method. You give it a predicate that should return true if the expectation succeeds and false if it fails. All expectation objects will grow a method with the name of your predicate method (so don't use a lambda). Appropriate exception messages will be generated when your predicate fails:

>>> import expecter
>>> def can_meow(thing):
...     return thing == 'kitty'
>>> expecter.add_expectation(can_meow)
>>> expect('kitty').can_meow()
>>> expect('puppy').can_meow()
Traceback (most recent call last):
...
AssertionError: Expected that 'puppy' can_meow, but it can't

API DOCUMENTATION

See http://expecter-gadget.readthedocs.org/en/latest/

More Repositories

1

dotfiles

~grb. Things in here are often interdependent. A lot of stuff relies on scripts in bin/.
Vim Script
1,901
star
2

selecta

A fuzzy text selector for files and anything else you need to select. Use it from vim, from the command line, or anywhere you can run a shell command.
Ruby
1,345
star
3

base

The universal Base class you've always wanted.
Ruby
436
star
4

raptor

An experimental web framework.
Ruby
367
star
5

readygo

A Ruby benchmarking tool accurate to sub-nanosecond time scales
Ruby
222
star
6

static-path

TypeScript
206
star
7

destroy-all-software-extras

Extra material for Destroy All Software Screencasts
Ruby
202
star
8

serveit

ServeIt, a synchronous server and rebuilder of static content like blogs, books, READMEs, etc.
Ruby
156
star
9

dingus

A record-then-assert test double library.
Python
98
star
10

do_not_want

(UNMAINTAINED) Do Not Want: Stops ActiveRecord from doing things you probably didn't want
Ruby
83
star
11

sucks-rocks

Does it suck? Or does it rock?
Ruby
58
star
12

pycomplexity

Scripts to show cyclomatic complexity of Python code in Vim and Emacs.
Python
46
star
13

cls

Cls: terse syntax for your classes
Ruby
40
star
14

python-mock-comparison

A comparison of Python's mocking and other test double libraries
Python
27
star
15

rubies

Ruby
24
star
16

blocks

Python
16
star
17

mote

A very experimental spec runner for Python. Beware: it is incomplete and may change drastically.
Python
12
star
18

LuaSpec

An executable Lua specification so I remember what I've learned.
Lua
12
star
19

explicit_import

Explicit imports for Ruby on a per-class/module basis
Ruby
9
star
20

prest

A REST client library for Python extracted sloppily from BitBacker
Python
5
star
21

the-march-of-progress

Find all the progress indicators from running applications. This does not solve an actual problem.
Ruby
3
star