In order to use robber
, you need to import expect
from the module:
from robber import expect
That's all. You are good to go.
Asserts that actual is equal (==) to expected:
expect(1).to.eq(1)
expect([1, 2]).to.eq([1, 2])
Also:
expect(1) == 1
Asserts that actual is not equal (!=) to expected:
expect(1).to.ne(2)
expect(1).to.not_eq(2)
expect(1).to != 2
expect(1) != 2
Asserts that the target is identical (is) to the expected:
expect(1).to.equal(1)
Asserts that the target is not identical (is) to the expected:
expect({ 'test': 'robber' }).to.not_equal({ 'test': 'robber' })
Asserts that the target is True:
expect(True).to.be.true()
Asserts that the target is False:
expect(False).to.be.false()
Asserts that the target is an instance of expected:
expect(obj).to.be.instanceof(Klass)
Asserts that the target can be matched by a regular expression:
expect('foo').to.match(r'foo')
Asserts that the target can not be matched by a regular expression:
expect('bar').to.not_match(r'foo')
Asserts that the target responds to a method:
expect(obj).to.respond_to('method')
Asserts that the target is truthy:
expect(['test']).to.be.truthy()
Asserts that the target is falsy:
expect([]).to.be.falsy()
Asserts that the target has a length of expected:
expect([1, 2]).to.have.length(2)
expect('str').to.have.length(3)
Asserts that the target is empty:
expect([]).to.be.empty()
expect('').to.be.empty()
Asserts that the target is nonempty:
expect([1, 2, 3]).to.be.not_empty()
expect('foo').to.be.not_empty()
Asserts that the target is a string:
expect('str').to.be.a.string()
Asserts that the target is an integer:
expect('str').to.be.an.integer()
Asserts that the target is floating point number:
expect(1.0).to.be.a.float()
Asserts that the target is a list:
expect([1, 2]).to.be.a.list()
Asserts that the target is a dictionary:
expect({}).to.be.a.dict()
Asserts that the target is a tuple:
expect((1, 2)).to.be.a.tuple()
Asserts that the target is None:
expect(None).to.be.none()
Asserts that the target is above expected:
expect(2).to.be.above(1)
Asserts that the target is below expected:
expect(1).to.be.below(2)
Asserts that the target is within expected:
expect(2).to.be.within(0, 2)
Asserts that the target contains an element, or a key:
expect([1,2,3]).to.contain(2)
expect({'foo': 'bar'}).to.contain('foo')
Asserts that the target does not contain an element, or a key:
expect([1,2,3]).to.not_contain(4)
expect({'foo': 'bar'}).to.exclude('baz')
In order to write more readable assertions, there are a few built-in language chains that you can use:
- to
- be
- a
- an
- have
Writing custom assertion is as easy as extending a base matcher class and adding two methods - matches for matching and failure_message for the error notice:
class Chain(Base):
def matches(self):
expectation = self.actual(None)
chain = getattr(expectation, self.expected)
return expectation is chain
def failure_message(self):
return 'Expected "%s" to have chain "%s"' % (self.actual, self.expected)
expect.register('chain', Chain)
After you register the new matcher, you can use it as expected:
expect(obj).to.have.chain('be')
If you want to have custom failure messages, for assertion or group of assertions, you can simply do:
from robber import failure_message
with failure_message('Something went wrong'):
expect(1).to.eq(2)
$ pip install robber
- Python 2.6, 2.7 (3.2 not tested yet)
- pip
$ nosetests tests/
- close_to matcher
- use magic method to call the matchers
- Python 3.2?
MIT License