Assertions
Result assertion may help us on testing functions. meiga provide us two functions: assert_success
and access_failure
.
How to assert¶
Following the example given on the getting started section:
import pytest
from tests.unit.doc.example_without_meiga import (
NoSuchKey,
TypeMismatch,
string_from_key,
)
@pytest.mark.unit
class TestExampleWithMeiga:
dictionary = {"key1": "value", "key2": 2}
def should_success(self):
result = string_from_key(dictionary=self.dictionary, key="key1")
result.assert_success(value_is_instance_of=str)
def should_fail_when_key_does_not_exist(self):
result = string_from_key(dictionary=self.dictionary, key="invalid_key")
result.assert_failure(value_is_instance_of=NoSuchKey)
def should_fail_when_type_mismatch(self):
result = string_from_key(dictionary=self.dictionary, key="key2")
result.assert_failure(value_is_instance_of=TypeMismatch)
import pytest
from tests.unit.doc.example_without_meiga import (
NoSuchKey,
TypeMismatch,
string_from_key,
)
@pytest.mark.unit
class TestExampleWithoutMeiga:
dictionary = {"key1": "value", "key2": 2}
def should_return_a_str(self):
value = string_from_key(dictionary=self.dictionary, key="key1")
assert isinstance(value, str)
def should_raises_non_such_key_exception(self):
with pytest.raises(NoSuchKey):
_ = string_from_key(dictionary=self.dictionary, key="invalid_key")
def should_raises_type_mismatch_exception(self):
with pytest.raises(TypeMismatch):
value = string_from_key(dictionary=self.dictionary, key="key2")
assert not isinstance(value, str)
Note
Check the following pytest-based test for more information: tests/unit/test_result_assertions.py
Warning
Before the version 1.7.0
, the only way to assert results is with from meiga.assertions import assert_success, assert_failure
.
import pytest
from meiga.assertions import assert_success, assert_failure
from meiga import Result
@pytest.mark.unit
class TestResultAssertion:
def should_assert_a_success(self):
result = Result(success=5)
assert_success(result, value_is_instance_of=int)
For the shake of compatibility, this way of assertion is also supported.
However, we strongly recommend to use Result class to perform auto assertion avoiding also import from meiga.assertions
module.