API

grappa provides two different testing styles: should and expect.

should

Example using should style:

from grappa import should

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

expect

Example using expect style:

from grappa import expect

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

For assertion operators and aliases, see operators documentation.

Reference

class grappa.Test(subject=Empty)[source]

Bases: grappa.base.BaseTest

Test represents the test definition in grappa with extensible and dynamic, runtime inferred DSL based on registered operators and third-party plugins.

Parameters:subject (mixed) – subject value to test.
all(*tests)[source]

Composes multiple tests and executes them, in series, once a subject is received.

Conditional composition operator equivalent to all built-in Python function.

Parameters:*tests (grappa.Test) – test instances to run.
any(*tests)[source]

Composes multiple tests and executes them, in series, once a subject is received.

Conditional composition operator equivalent to any built-in Python function.

Parameters:*tests (grappa.Test) – test instances to run.
expect

Alias name to self reference the current instance. Required for DSL API.

should

Alias name to self reference the current instance. Required for DSL API.

grappa.use(plugin)[source]

Register plugin in grappa.

plugin argument can be a function or a object that implement register method, which should accept one argument: grappa.Engine instance.

Parameters:plugin (function|module) – grappa plugin object to register.
Raises:ValueError – if plugin is not a valid interface.

Example:

import grappa

class MyOperator(grappa.Operator):
    pass

def my_plugin(engine):
    engine.register(MyOperator)

grappa.use(my_plugin)
class grappa.Operator(context=None, operator_name=None, fn=None, kind=None, aliases=None, operators=None, suboperators=None)[source]

Bases: object

Operator implements a base class with common logic and required interface that is used by specific operator implementations.

Any operator should inherit from this class.

Dsl

DSL for operator error messages templating

Type:grappa.operators_dsl
Type

support operators types

Type:grappa.operators.OperatorTypes
kind

stores the operator kind

Type:str
operators

operator keywords

Type:list|tuple
aliases

chain attributes aliases for expressivity

Type:list|tuple
information

optional additional help in case of error

Type:list|tuple
subject_message

optional subject message

Type:str|Operator.Dsl.Message
expected_message

optional expected message

Type:str|Operator.Dsl.Message
operator_name

invokation operator name keyword

Type:str
suboperators

optional child operators

Type:list|tuple[grappa.Operator]
Dsl = <module 'grappa.operator_dsl' from '/home/docs/checkouts/readthedocs.org/user_builds/grappa/checkouts/latest/grappa/operator_dsl.py'>
Type

alias of OperatorTypes

aliases = ()
chainable = True
ctx = None
expected = Empty
expected_message = <grappa.operator_dsl.Message object>
information = []
kind = 'matcher'
match(*args, **kw)[source]
observe()[source]

Internal decorator to trigger operator hooks before/after matcher execution.

operator_name = None
operators = ()
run(*args, **kw)[source]

Runs the current operator with the subject arguments to test.

This method is implemented by matchers only.

run_matcher(subject, *expected, **kw)[source]

Runs the operator matcher test function.

subject_message = <grappa.operator_dsl.Message object>
suboperators = []
value = Empty
grappa.attribute(*args, **kw)[source]

Registers a new attribute only operator function in the test engine.

Parameters:
  • *args – variadic arguments.
  • **kw – variadic keyword arguments.
Returns:

function

grappa.operator(name=None, operators=None, aliases=None, kind=None)[source]

Registers a new operator function in the test engine.

Parameters:
  • *args – variadic arguments.
  • **kw – variadic keyword arguments.
Returns:

function

grappa.register(operator)[source]

Registers a new operator class in the test engine.

Parameters:operator (Operator) – operator class to register.
Returns:operator class.
Return type:Operator