Here's some buckets of common topics that I think we need to cover. (v.1.1 of this answer).
Note: I've established a GitHub repository to collaborate on initial drafts of canonical questions and answers for this project. I will provide push access to any established StackExchange member who's interested in being part of the first drafting phase. Once we have solid first drafts written, they'll become part of the normal Community Wiki editing process.
How do I start writing my first unit test? / How do I unit test this code? / Will you write my unit test?
These questions come up all the time and I personally would love to have a canonical deflection for them to try to stem downvotes, negative interactions, and frustration for the community (myself included).
A good canonical answer could collect our favorite resources for writing unit tests (How to Write Good Unit Tests, Trailhead, and the Developer Blog's Month of Testing series are among my favorites). It would try to be a succinct clearinghouse of where to go to get started, not a treatise, and would emphasize that most code is not "special" in the sense of requiring unique techniques to test. Lastly, it would mention in passing the importance of assertions and that unit tests are more than a vehicle to obtain code coverage.
How is code coverage obtained and calculated? / How do I increase my coverage? / Why can't I cover these lines?
A good canonical answer to these types of question might both link to the first canonical answer above to surface resources, while emphasizing a few points around coverage:
- Coverage is a byproduct of writing high quality unit tests.
- Link to documentation on what lines of code are executable and counted as part of code coverage
- Briefly summarize common mistakes that cause control structures (loops and condition bodies) not to be covered (mismatched test data).
What should I do if my code uses a callout?
Trailhead has an excellent module on testing callouts using mocks. I'm not sure much more is needed in a canonical answer than to point there.
How do I handle code that cannot be executed in test context?
Summarize situations where one should use Test.isRunningTest()
to gate functionality in test context.
Briefly introduce the idea of dependency injection to test behavior of code that uses this type of functionality and link to outside resources.
How does coverage and testing for asynchronous code work?
Briefly summarize and link to documentation regarding the machinery of Test.stopTest()
and Test.startTest()
as they relate to testing asynchronous code. Summarize issue surrounding testing functionality that could fire multiple batch invocations and multi-level asynchrony (schedulable calls batch, etc), and point towards options for structuring tests for this type of code.
Deployment Errors / Coverage Metrics
Briefly summarize and provide links to documentation to cover issues like:
- What are the code coverage requirements?
- How are they applied during a deployment with different test run settings?
- What are some of the techniques one uses to manage coverage during the deployment process?
Topics Beyond
There's a few really large subject areas that seem to me a little more didactic and somewhat less amenable to covering in a canonical answer. Among them would be topics like
- How do I write a good unit test?
- How do I use dependency injection?
- How do I write a good test data factory?
and so forth. I think we can do a lot of good covering some more bite-sized pieces quickly and providing links to existing resources to address those issues in the mean time, but I'd love for this project to grow to address some of that more long-form content.