When comparing grappa vs assertpy, the Slant community recommends assertpy for most people. In the question“What are the best assertion libraries for Python? ” assertpy is ranked 1st while grappa is ranked 2nd. The most important reason people chose assertpy is:
There is only one function to import to use assertpy: `assert_that()`. Assertions are made by passing values as function parameters to the appropriate function(s), e.g. `assert_that('hello').is_equal_to('hello')`.
Ranked in these QuestionsQuestion Ranking
Pro Easy-to-understand syntax
Unlike other assertion libraries, grappa’s syntax is extremely verbose and expressive. It utilises English-like expressions and uses the OR operator (
|) for assertion. The assertion methods begin with either
expect. This makes grappa highly readable and easy to understand, even for non-developers.
Examples: to assert that a boolean is true:
True | should.be.true, and to assert that a value is within a range:
3.14 | should.be.within(2, 4).
Pro Ability to chain assertions with > operator
For complex assertions with multiple expressions, grappa allows you to chain them together with the
> operator. For example, to assert that “hello” should be a string and should be equal to “hello”, we can do:
'hello' | should.be.a(str) > should.be.equal.to('hello').
Pro Detailed error reporting
When an assertion fails in grappa, it outputs a detailed error report. The report contains the reason the assertion failed, the expected and received value(s) in plain English, e.g. "What we expected: an object that can be length measured and its length is equal to 4". It also contains the function grappa used to test the assertion (e.g.
len(), for our previous example error message) and provides additional information about the function, along with a link to the Python docs.
Pro HTTP protocol specific assertions
With grappa-http plugin you can write concise and expressive HTTP protocol assertions very easily. Useful for API testing.
Pro Ability to add custom operators
You can easily extend via plugins the assertion operations with custom ones that can assert domain-specific logic.
Pro Simplified assertions for complex data structures
You can use grappa operations to perform non-trivial assertions over multidimensional data structures by using its dict/list browsing operators.
Pro API is straightforward
There is only one function to import to use assertpy:
assert_that(). Assertions are made by passing values as function parameters to the appropriate function(s), e.g.
Pro Compact and Pythonic
Assertions are made through simple Python function calls with idiomatic names.
Pro Supports asserting values beyond normal data types
assertpy has support for asserting values such as datetimes (e.g.
assert_that(x).has_year(1980)), files (e.g.
assert_that('foo.txt').exists()), and objects.
Con Can be too verbose for some people
Some consider grappa to be too verbose with unnecessary syntactic sugar. Compared to other assertion libraries, grappa is the most expressive of them all, which makes it a double-edged sword: the expressiveness makes it easy to read and understand, but each expression is longer to type and can seem redundant — the same assertion can be done with less code without grappa.
Con Syntax is different from regular Python expressions
Because of grappa’s heavy use of Python’s operators to achieve conciseness, its expressions can be unidiomatic and confusing at first glance.