Skip to content

Instantly share code, notes, and snippets.

@amphro
Created December 11, 2013 00:45
Show Gist options
  • Save amphro/7903207 to your computer and use it in GitHub Desktop.
Save amphro/7903207 to your computer and use it in GitHub Desktop.
Run an org's namespace tests from SOAP (untested). NOTE: This only works with v30.0. To work with v29.0, you must use the Apex API (wsdl).
SoapConnection conn = // Get the tooling soap connection;
String namespace = "";
QueryResult qr = conn.query("SELECT Id, SymbolTable FROM ApexClass WHERE NamespacePrefix = '"+namespace+"'");
List<String> ids = new ArrayList<String>();
SObject[] classes = qr.getRecords();
for (int i = 0; i < classes.length; i++) {
ApexClass rec = (ApexClass)classes[i];
SymbolTable st = rec.getSymbolTable();
String[] mods = st.getTableDeclaration().getModifiers();
int modLen = mods.length;
for (int modIndex = 0; modIndex < mods.length; modIndex++) {
if (mods[modIndex].equals("TEST")) {
ids.add(rec.getId());
}
}
}
if (ids.size() > 0) {
RunTestsRequest rtr = new RunTestsRequest();
String[] idsArray = ids.toArray(new String[]{});
rtr.setClasses(idsArray);
RunTestsResult result = conn.runTests(rtr);
// Do what you need to do with the results
//If you want to get the results in setup or the dev console as well, do the following:
//NOTE: You need Apache Commons in your path to use the following line.
//conn.runTestsAsynchronous(StringUtil.join(",", idsArray););
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment