grappa is library/framework agnostic, so you can use it with your preferred testing framework, such as nose, pytest, behave

This page provides some useful information and configuration tips for a better and more friendly integration between grappa and testing framework.


Friendly error traceback

pytest, by default, provides a custom error reporting based on the traceback analysis, pretty much as grappa does, but unfortunately in a less friendly way for the developer.

You can partially or completely disable this in pytest behavior in order to have more friendly errors generated by grappa.

In order to do that you should simply pass the following flag to:

$ pytest --tb=native

Or alternatively, you can simply partially disable it:

$ pytest --tb=short

The difference would be from this:

________________ test_expect_context_manager ________________
tests/ in test_expect_context_manager
grappa/ in __call__
    return self._fn(expected, *args, **kw)
grappa/ in wrapper
    return self.test._trigger()
grappa/ in _trigger
    raise err
E   AssertionError: Oops! Something went wrong!
E     The following assertion was not satisfied
E       subject "foo" should be equal "bar"
E     What we expected
E       a value that is equal to "foo"
E     What we got instead
E       an value of type "str" with data "foo"
E     Where
E       File "tests/", line 277, in test_expect_context_manager
E       269|      with pytest.raises(AssertionError):
E       270|          10 |, 10)
E       271|
E       272|      with pytest.raises(AssertionError):
E       273|          None |, 10)
E       274|
E       275|
E       276|  def test_expect_context_manager():
E       277| >    should('foo').be.equal('bar')
E       278|
E       279|      with should('foo'):
E       280|'foo')
E       281|          should.have.length.of(3)
E       282|          should.have.type('string')
E       283|
E       284|      with should('foo'):

Into this:

Traceback (most recent call last):
  File "tests/", line 277, in test_expect_context_manager
  File "grappa/", line 16, in __call__
    return self._fn(expected, *args, **kw)
  File "grappa/", line 60, in wrapper
    return self.test._trigger()
  File "grappa/", line 145, in _trigger
    raise err
AssertionError: Oops! Something went wrong!

  The following assertion was not satisfied
    subject "foo" should be equal "bar"

  What we expected
    a value that is equal to "foo"

  What we got instead
    an value of type "str" with data "foo"

    File "tests/", line 277, in test_expect_context_manager

    269|      with pytest.raises(AssertionError):
    270|          10 |, 10)
    272|      with pytest.raises(AssertionError):
    273|          None |, 10)
    276|  def test_expect_context_manager():
    277| >    should('foo').be.equal('bar')
    279|      with should('foo'):
    281|          should.have.length.of(3)
    282|          should.have.type('string')
    284|      with should('foo'):

Dependency injection using fixtures

In order to minimize boilerplate across tests, you can create a pytest fixture that would be automatically injected via across test functions/methods in your test suites:

First, create a file in the directory where your test files lives with the following content:

# tests/
import pytest
from grappa import should as _should, expect as _expect

def should():
    return _should

def expect():
    return _expect

Then, from a test file module, you can simply declare a test function that accepts a fixture argument, such as:

# tests/

def test_should_fixture(should):
    'foo' | should.have.length.of(3) |'foo')

def test_expect_fixture(expect):
    'foo' | |'foo')