Skip to content

Instantly share code, notes, and snippets.

@amphro
Created December 11, 2013 00:46
Show Gist options
  • Save amphro/7903218 to your computer and use it in GitHub Desktop.
Save amphro/7903218 to your computer and use it in GitHub Desktop.
Run an org's namespace tests from JS (untested). NOTE: This only works with v30.0.
//This example assumes you have a sendRequest method, that can send an AJAX call to Salesforce with Auth header all set up
function runNamespacedTest(namespace) {
function getTestIdsFromApexClasses(classes) {
var ids = [];
for (var i = 0; i < classes.length; i++) {
var rec = classes[i];
var st = rec.SymbolTable;
var mods = st.tableDeclaration.modifiers;
var modLen = mods.length;
while (modLen--) {
if (mods[modLen] === 'TEST') {
log('Found test: '+st.name);
ids.push(rec.Id);
}
}
}
return ids;
}
var ns = namespace || '';
sendRequest("/services/data/v30.0/tooling/query/?q=SELECT%20Id%2C%20SymbolTable%20FROM%20ApexClass%20WHERE%20NamespacePrefix%20%3D%20'"+ns+"'", function(response) {
var ids = getTestIdsFromApexClasses(JSON.parse(response).records);
if (ids.length > 0) {
sendRequest("/services/data/v30.0/tooling/runTestsAsynchronous/?classids="+encodeURIComponent(ids.join(',')), function(testRunId) {
//Do something with the testResult if you want, or view it in the dev console
});
}
});
}
runNamespacedTest();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment