allankp / pytest-testrail

pytest plugin for integration with TestRail, for creating testruns and updating results
MIT License
96 stars 124 forks source link

Exception when TestRail API returns a 429 HTTP status (too many requests) #33

Closed apallier closed 6 years ago

apallier commented 6 years ago

Sometimes, TestRail API returns a HTTP 429 status (too many requests). In this case, an exception occurred and the publishing is stopped:

Traceback (most recent call last):
  File "L:/pytest-testrail/pytest_testrail/test_api.py", line 13, in <module>
    client.send_post(plugin.ADD_RESULT_URL.format(156, 366), {'status_id': 1, 'version': '1.0.0.0'}, cert_check=True)
  File "L:\pytest-testrail\pytest_testrail\testrail_api.py", line 114, in send_post
    return r.json()
  File "L:\pytest-testrail\venv\lib\site-packages\requests\models.py", line 826, in json
    return complexjson.loads(self.text, **kwargs)
  File "L:\pytest-testrail\venv\lib\site-packages\simplejson\__init__.py", line 516, in loads
    return _default_decoder.decode(s)
  File "L:\pytest-testrail\venv\lib\site-packages\simplejson\decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "L:\pytest-testrail\venv\lib\site-packages\simplejson\decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.scanner.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
apallier commented 6 years ago

Here an example of code to reproduce the problem:

from pytest_testrail import plugin
from pytest_testrail.testrail_api import APIClient

if __name__ == '__main__':

    client = APIClient('https://mydomain.testrail.net/', 
                       'login@mydomain.com', 
                       'fSiezfeRTdmtbmoTOLYt-l6ukZb76UpMdtrYn/OCJ')

    for i in range(100):

        client.send_post(plugin.ADD_RESULT_URL.format(156, 366), 
                         {'status_id': 1, 'version': '1.0.0.0'}, 
                         cert_check=True)

When HTTP 429 status occurs there is no payload in the response so the line return r.json() raises an exception (in file testrail_api.py)