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

SatelliteQE / robottelo / 9372
37%

Build:
DEFAULT BRANCH: master
Ran 22 Mar 2017 12:47PM UTC
Jobs 4
Files 0
Run time 6s
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

pending completion
9372

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

Jobs
ID Job ID Ran Files Coverage
1 9372.1 22 Mar 2017 12:47PM UTC 0
40.52
Travis Job 9372.1
2 9372.2 22 Mar 2017 12:47PM UTC 0
40.51
Travis Job 9372.2
3 9372.3 22 Mar 2017 12:47PM UTC 0
40.51
Travis Job 9372.3
4 9372.4 22 Mar 2017 12:47PM UTC 0
40.51
Travis Job 9372.4
Source Files on build 9372
Detailed source file information is not available for this build.
  • Back to Repo
  • Travis Build #9372
  • 11550504 on github
  • Prev Build on master (#9367)
  • Next Build on master (#9387)
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