• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SatelliteQE / robottelo / 9372 / 4
37%
master: 37%

Build:
DEFAULT BRANCH: master
Ran 22 Mar 2017 12:47PM UTC
Files 90
Run time 5s
Badge
Embed ▾
README BADGES
x

If you need to use a raster PNG badge, change the '.svg' to '.png' in the link

Markdown

Textile

RDoc

HTML

Rst

22 Mar 2017 12:42PM UTC coverage: 40.505% (-29.1%) from 69.639%
9372.4

push

travis-ci

rochacbruno
Implemented custom assertNotRaises and assertNotRaisesRegex assertions (#4394)

* Implemented custom assertNotRaises and assertNotRaisesRegex assertions

This commit introduces following 2 helpers:

* assertNotRaises()
* assertNotRaisesRegex()

the purpose of which is to execute some code and in case expected Exception occurred - mark the test as 'failure', not 'error' with no tricky workarounds.

The usage is pretty much similar to `assertRaises` assertion.
Let's assume we want to ensure some host exists in satellite. Before, in API test, we had to do something like this:
```python
try:
    host.read()
except HTTPError:
    self.fail()
```

Now we can simply pass the instruction to `self.assertNotRaises()` method:
```python
self.assertNotRaises(HTTPError, host.read)
```

OR use `assertNotRaises` as a context manager:
```python
with self.assertNotRaises(HTTPError):
    host.read()
```
which allows us to execute a block of code and looks better in general.

Both forms are equal and in case no exceptions were risen - the test will proceed, if expected exception was risen - `AssertionError` will be risen and the test will be marked as 'FAIL', if any other exception occurred - exception won't be caught which will lead to test being marked as 'ERROR'.

But what if just checking Exception presence is not enough and we want to specify the exact HTTP status code (or return code for CLI) which we are expecting to happen? For example, in previous example, we're expecting that in case host is not present - 404 error will be shown, but definitely not 500 error or something else.
For such case, `assertRaises` accepts 2 optional arguments - `http_status_code` and `cli_return_code` for API and CLI accordingly.

So instead of
```python
try:
    host.read()
except HTTPError as error:
    if error.response.status_code == 404:
        self.fail()
    else:
        raise
```

We can now use
```python
self.assertNotRaises(HTTPError, host.read, http_status_code=404)
```
for callable object or we can use assertion as a context manager:
```python
with self.assertNotRaises(HTTPError, http_status_code=404):
    host.read()
```

For CLI, syntax is similar:
```python
with self.assertNotRaises(CLIReturnCodeError, cli_return_code=128):
    Host.info({'id': 1})
```

This way, only expected exception will be caught and only in case response code matches the expected one.

And what if that's still not enough and we want to check the exception message to ensure some pattern is there? The answer is `assertNotRaisesRegex` assertion, which is basically extended `assertNotRaises` assertion which should be used similar to `assertRaisesRegex`.

Example usage with exception and pattern:
```python
self.assertNotRaisesRegex(HTTPError, '404 Client Error', host.read)

with self.assertNotRaisesRegex(HTTPError, '404 Client Error'):
    host.read()
```
Example usage with exception, response code and pattern:
```python
self.assertNotRaisesRegex(
    HTTPError, '404 Client Error', host.read, http_status_code=404)

with self.assertNotRaisesRegex(
        HTTPError, '404 Client Error', http_status_code=404):
    host.read()
```

And, of course, valid regular expression patterns are supported.

Similar to previous examples, if the exception is risen - only in case exception, pattern and response code (if specified) match the expected ones - test will be marked with 'FAIL', all others exceptions won't be handled and will mark the test with 'ERROR' as usual.

* Introduce value handlers for assertNotRaises response code processing

2773 of 6846 relevant lines covered (40.51%)

0.41 hits per line

Source Files on job 9372.4
  • Tree
  • List 0
  • Changed 0
  • Source Changed 0
  • Coverage Changed 0
Coverage ∆ File Lines Relevant Covered Missed Hits/Line
  • Back to Build 9372
  • Travis Job 9372.4
  • 11550504 on github
  • Prev Job for on master (#9367.4)
  • Next Job for on master (#9387.2)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc