# Assertions

## Where you might need:

* details for the failed assertions
* passed assertions (may not be for everyone)
* number of assertions done on a test suite/scenario

## What do we show:

* Number of Assertions passed/failed in a test suite/case
* Expected Part of the Assertion and Observed Part of the Assertion
* Details of the Failed Assertion (include stack trace)

## To Add Passed Assertions

To save the details of the assertions passed in pytest, please add the enable\_assertion\_pass\_hook as true in your pytest.ini file&#x20;

<figure><img src="/files/1m3CstwxnHxvlMVCJWvc" alt=""><figcaption><p>If not done, no assertions would be saved in TestResults</p></figcaption></figure>

```ini
[pytest]
enable_assertion_pass_hook=true
```

Please add this in `pytest.ini` and this would add details for passed assertions

> *Reference from* [*pytest docs*](https://docs.pytest.org/en/stable/reference/reference.html#pytest.hookspec.pytest_assertion_pass)*.*

## Supported Options:

Following are the features/ways pytest provides to process assertions and how handshakes report it in db/excel

### Assert that a certain exception is raised

```python
# content of test_sysexit.py
import pytest

def f():
    raise SystemExit(1)

def test_mytest():
    with pytest.raises(SystemExit):
        f()
```

Recommended way to write this case:

```python
# content of test_sysexit.py
import pytest
from handshake.reporters.markers import set_info


def f():
    raise SystemExit(1)


@set_info(
    description="This test would raise the SystemExit Exception if not then it is an error"
)
def test_mytest():
    with pytest.raises(SystemExit):
        f()

```

If the test passes, refer to the 'description' of the test. If it fails, the details can be found in both the description and the error field of the test case or suite. These are easily identifiable through the generated reports.

```log
[{"name":"Error","message":"@set_info(\n        description=\"This test would raise the SystemExit Exception if not then it is an error\"\n    )\n    def test_mytest():\n>       with pytest.raises(SystemExit):\nE       Failed: DID NOT RAISE <class 'SystemExit'>\n\ntest_sample.py:13: Failed","stack":""}]
```

{% hint style="info" %}
Currently, I have not observed pytest to send the details of the passed assertion, when the expected exception was raised inside the test case.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rahuls-organization-17.gitbook.io/handshake/help-docs/pytest/how-to/assertions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
