Tutorial

Installing grappa

Please, see installation section.

Importing grappa

For should assertion style, use:

from grappa import should

For expect assertion style, use:

from grappa import expect

Basic assertions

Equality assertions:

'foo' | should.be.equal.to('foo')

'foo' | expect.to.be.equal.to('foo')
[1, 2, 3] | should.be.equal.to([1, 2, 3])

[1, 2, 3] | expect.to.be.equal.to([1, 2, 3])

Value type assertions:

1.54 | should.be.a('float')

1.54 | expect.to.be.a('float')
'foo' | should.be.a('string')

'foo' | expect.to.be.a('string')
[1, 2, 3] | should.be.a('list')

[1, 2, 3] | expect.to.be.a('list')

Measure length:

iter([1, 2, 3]) | should.have.length.of(3)

iter([1, 2, 3]) | expect.to.have.length.of(3)

Custom message errors

[1, 2, 3] | should.have.length.of(2, msg='list must have 2 items')
'hello world!' | should.have.contain.word('planet', msg='planet word is mandatory')

Negation assertions

'foo' | should.not_be.equal.to('bar')

'foo' | expect.to_not.be.equal.to('bar')
[1, 2, 3] | should.not_be.have.length.of('bar')

'foo' | expect.to_not.be.equal.to('bar')

Context based assertion for DRYer code

with should({'foo': 'bar'}):
    should.be.a(dict)
    should.have.length(1)
    should.have.key('foo').that.should.be.equal.to('bar')

with expect({'foo': 'bar'}):
    expect.to.be.a(dict)
    expect.to.have.length(1)
    expect.to.have.key('foo').to.be.equal('bar')

Testing exceptions

(lambda: x) | should.raises(NameError)

(lambda: x) | expect.to.raises(NameError)
(lambda: x) | should.do_not.raises(RuntimeError)

(lambda: x) | expect.to_not.raises(RuntimeError)

Conditional assertions

all assertion composition, equivalent to and operator.

You can define N number of composed assertions.

{'foo': True} | should.all(should.have.key('foo'), should.have.length.of(1))

{'foo': True} | expect.all(expect.to.have.key('foo'), expect.to.have.length.of(1))

any assertion composition, equivalent to or operator. You can define N number of composed assertions.

{'foo': True} | should.any(should.have.key('bar'), should.have.length.of(1))

{'foo': True} | expect.any(expect.to.have.key('bar'), expect.to.have.length.of(1))

Composing assertions

Using which/that attribute operators for chained assertions:

{'foo': True} | should.have.key('foo').which.should.be.true

{'foo': True} | expect.to.have.key('foo').that.expect.to.be.true

Using | for multiple assertions composition (equivalent to all/and composition):

{'foo': True} | should.be.a('dict') | should.have.key('foo') | should.have.length.of(1)

{'foo': True} | expect.to.be.a('dict') | expect.to.have.key('foo') | expect.to.have.length.of(1)

Chained assertions

Using > operator for chained assertion instead of which/that operators for assertion composition:

{'foo': True} | should.have.key('foo') > should.be.true

{'foo': True} | expect.to.have.key('foo') > expect.to.be.true

More complex chained assertions:

(object
    | should.have.property('foo')
    > should.be.a('tuple')
    > should.have.length.of(3)
    > should.be.equal.to(('foo', 'bar', 'baz')))
(dictionary
    | should.have.key('foo')
    > should.be.a('list')
    > should.have.length.of(3)
    > should.be.equal.to(['foo', 'bar', 'baz']))

How to compose assertions

See operators composition section.