Setting Pass/Fail using Python for an automated test in Sauce Labs? Setting Pass/Fail using Python for an automated test in Sauce Labs? selenium selenium

Setting Pass/Fail using Python for an automated test in Sauce Labs?


I did it this way, first you need to import some things

# These next imports for reporting Test status to Sauce Labsimport sysimport httplibimport base64try:    import jsonexcept ImportError:    import simplejson as json

Then you need this config

#Config to connect to SauceLabs REST APIconfig = {"username": "yourusernamehere",          "access-key": "youraccesskeyhere"}

Then you put your tests. At the end, before your TearDown you need to include

# Curl call to SauceLabs API to report Job Result    def set_test_status(self, jobid, passed):        base64string = base64.encodestring('%s:%s' % (config['username'], config['access-key']))[:-1]        body_content = json.dumps({"passed": passed})        connection = httplib.HTTPConnection("saucelabs.com")        connection.request('PUT', '/rest/v1/%s/jobs/%s' % (config['username'], jobid),                       body_content,                       headers={"Authorization": "Basic %s" % base64string})        result = connection.getresponse()        return result.status == 200

Then in your tearDown you need to include some kind of if logic. I did it this way (and it works)

def tearDown(self):    # sys.exc_info should be (None, None, None) if everything is OK, it fills with some values if something went wrong    # This if reports to Sauce Labs the outcome of the Test where True = Pass and False = Failed    if sys.exc_info() == (None, None, None):        self.set_test_status(self.driver.session_id, True)    else:        self.set_test_status(self.driver.session_id, False)    self.driver.quit()    self.assertEqual([], self.verificationErrors)

That did the trick for me


You can use Sauce labs REST API to mark your test pass/failed. You can find some example code given here