The SauceOnDemandTestWatcher (From Sauce-java) is an easy way to report test status for junit tests. This works fine for tests where you create a single WebDriver (and thus, a single Sauce Labs job) per test. But what if you're creating a single WebDriver for the entire class (Say, as part of a Before action), every test will run against the same Sauce Labs session....
Because of how JUnit itself works, every time a test passes or fails, the SauceOnDemandTestWatcher will be called with the test's status. It'll reach out to the Sauce Labs REST API, and update the current Sauce Labs job. But because you're re-using that job for every test, you'll keep changing that one job's status. The status it "finishes" with won't be the status of all the tests in that class, it'll be the status of the last test that ran.
We recommend creating a unique session for each test, not just for a single group of tests; Changing your tests to do that is more in line with best practise. But, if you can't do that, one of our rad users, Meaghan Lewis, has let us share the code she wrote to patch the SauceOnDemandTestWatcher to aggregate status across a class.
In her example, which is below, any single failure will cause the Sauce Labs job to be marked as "failed". Only if every test in a class passes will the Sauce Labs job be moved into the "passed" state.
Her Example
SauceOnDemandTestWatcher resultReportingTestWatcher = new SauceOnDemandTestWatcher(this, authentication) { @Override protected void failed(Throwable e, Description description) { watchedLog+= description.getMethodName(); } @Override protected void succeeded(Description description) { } }; public static boolean areTestsSuccessful(){ boolean pass = true; if(watchedLog != null && !watchedLog.equals("")){ pass=false; } return pass; } public static void updateTestStatus(){ SauceREST client = new SauceREST(System.getenv("SAUCE_USERNAME"), System.getenv("SAUCE_ACCESS_KEY")); Map<String, Object> updates = new HashMap<>(); updates.put("passed", areTestsSuccessful()); Utils.addBuildNumberToUpdate(updates); client.updateJobInfo(BaseJunitTest.sessionId, updates); }