Finding Out What a Trailhead Hands-on Test is Doing
I recently completed the Apex Web Services Superbadge Unit. During this I was finding the specification of the API that I was meant to be building somewhat lacking in detail. It was not particularly clear on exactly what it wanted to be returned. As the tests failed over and over I thought to myself “wouldn’t it be easier if I could see what was being tested”. Turns out you can.
This will work for any if the hands-on tests in Trailhead where Trailhead executes Apex to drive the test. Not all tests do this. Some simply interact with records using the API. However if Apex is being executed we can capture that. To do so go to Setup and search for Debug. Go to Debug logs and add a User Debug for your user. Run the test again.
Take a look at the logs (reload the logs page). You’ll see an operation at something like “services/data/v30.0/tooling/executeAnonymous/”. Maybe a few in a row if a few tests executed one after another. Open the one that failed and at the top you’ll see the Apex that was run. Something like this (this is from the Get Started with Apex unit):
Execute Anonymous: List<String> strings = StringArrayTest.generateStringArray(10); System.assertEquals(strings.size(),10); System.assertEquals(strings[0],'Test 0');
Now you know what it’s doing it’s easier to write your code to actually do what it wants. Note this is a terrible test. You can write code that does not meet the required specifications and pass this unit. Even better for the Superbadge unit I mentioned: they also want test classes. Take the various tests the unit performs and you’ve basically got your test class written!