Red Argyle Logo

Patterns
The Salesforce Blog with Tailored Goodness

Unit Tests: Run Them All or Risk Running Into Deployment Issues

If you follow along with my blog posts, you might know that I put a lot of energy in to the constant updating and improvement of our Quality Assurance process here at Red Argyle. Updates are based on my own experiences and research for current industry standards. There’s an important step in the process that I haven’t yet written about: Running All Unit Tests. Salesforce built the testing functionality for a reason, and it should be leveraged at many different times in the application development lifecycle.

 

Let’s face the facts. We’ve all done it…we’ve altered or added some new code to one of our classes or triggers then forgotten to run the unit tests. This could mean we have failing tests or that our code coverage is low. Or maybe we ran the tests for the classes we updated and those are passing. The devs say they’re good, and we try to deploy. Then–KABOOM!–failures with unit tests that may or may not be related to the code you’ve changed.

 

This happened to us in one of our recent deployments. We ran our tests, but not all tests. Ours passed, but there were failures we didn’t find until we tried to validate the changeset. Talk about frustrating. Our deployment was almost put on hold because we had to contact the developers who were handling that codebase in the org and wait for them to fix the failing unit tests. We could have avoided all of this if we had just clicked “Run All Tests” before we uploaded our first changeset.

 

At Red Argyle, we run all unit tests in all environments when performing an Org Health Check and before deployments, at the very least. Writing and testing the unit tests for the changes you’ve been making in the org is a step that should not be forgotten, but, without running all tests, you risk not catching a broken unit test or low coverage until you try to deploy.

 

Three Ways to Run All Unit Tests

All of the following options return the same results, which means you’ll want to choose the process that feels most intuitive to you. There are other alternatives, like using Mavensmate, but I’m just going to cover the Salesforce options.

 

Option A: Apex Test Execution UI

  1. Go to Setup | Develop | Apex Test Execution.
  2. Click “Select Tests…”
  3. Choose “All Namespaces” from the dropdown, and use the checkbox next to “Class Name” to select all classes.
  4. Click “Run”.
    1. The status of the tests will appear in the list below.
    2. To view older tests, use the “View Test History” link.
Running all unit tests option one

Running all Unit Tests Using the Apex Test Execution UI: Steps 1 & 2

Running all unit tests option one

Running all Unit Tests Using the Apex Test Execution UI: Steps 3 & 4

Running all unit tests with Apex Test Execution UI

Running all Unit Tests Using the Apex Test Execution UI: Status View

 

Option B: Apex Classes “Run All Tests”

  1. Go to Develop | Apex Classes
  2. Click “Run All Tests”.
  3. You will be redirected to the Apex Test Execution UI and the tests will be running.
    1. The status of the tests will appear in the list below.
    2. To view older tests, use the “View Test History” link.

 

How to Run Unit Tests Through Apex Classes

Running all Unit Tests Through Apex Classes

 

Option C: Use the Developer Console

  1. Open the Developer Console.
  2. To start a new test execution run, choose “New Run” in the “Test” menu.
  3. Choose either “My Namespaces” or “All Namespaces”.
  4. Use the checkbox next to Class Name to select all classes.
  5. “Run” the tests.
    1. Open the “Tests” tab to see the currently running or previously run tests.
    2. Look at the “Overall Code Coverage” to see the percentage of code coverage for each class and the entire org.
  6. You can rerun a test by clicking on a previously run test in the “Tests” tab and then choosing “Rerun” in the “Tests” menu.

 

Running all Unit Tests Through Developer Console

Running all Unit Tests Through Developer Console

 

Let our experience guide yours. I think I am going to amend the Red Argyle deployment plan template with this addition: In big, bold letters, positioned right at the top, a note to myself and to my team to “Run All Tests.”

 

And lastly, here are some handy resources to help you learn more about unit tests:

Force.com Cookbook: Automated unit test code

GitHub: Code to natively automate testing with emailable results

Salesforce Trailhead: Module on Apex testing