[SalesForce] Run anonymous apex as if it were a test case

Is there any way to run anonymous apex so that it is treated the same way as a unit test?

Sometimes it would be ideal to just try some code out and know that any DML operations that get performed won't be committed. I wouldn't expect callouts to occur or emails to be sent.

This would also allow individual test methods to be run from a class. So if one method in a dozen or so is failing you could re-run it and get a more concise log.

I have considered using transaction control and just doing a rollback, but that doesn't give the full advantage of running as a test.

Best Answer

It's not a complete answer but you can always rollback dml operations yourself:

System.Savepoint sp = Database.setSavepoint();
try {
    //all you anonymous code
} catch(DmlException e) {
    System.debug('DmlException='+e);
} finally {
    Database.rollback(sp);
}

You will still have the problem of callouts and emails being sent, but if these are not a factor it will be sufficient

Related Topic