Unit and functional tests are a key part of any continuous integration pipeline. Here are some tools that make testing easy for Salesforce.
There are few topics in the Salesforce developer community that draw more eyebrow raises than unit testing. By now, most Salesforce developers acknowledge and embrace the benefit of unit testing.
Most agree that unit tests actually save you time in the long run. As Kevin Poorman notes in the above video, the average lifespan of a software project is 12 years. The average lifespan of a developer at a corporation is about 2 years and 3 months. So most of the time spent on a software project goes to maintenance rather than actual development of new software. As unglamorous as it sometimes seems, unit testing is very helpful for the maintenance of a code base. Unit tests serve as a type of documentation that can help developers that come to a project later on understand what the code is trying to do.
Unit tests are also crucial for Continuous Integration. Running automated tests allows you to deploy your code with confidence much faster than you could if you were manually testing everything.
Still, despite the nearly universally acknowledged and unassailable wisdom behind writing unit tests, Force.com doesn’t make it very easy to do actually do unit testing. Though there is a requirement that you have 75% test coverage on deployed Apex code, unit testing on Force.com presents a number of unique challenges that developers on other platforms don’t face.
First, Salesforce doesn’t let you mock your classes or objects the way you might with another environment. It’s difficult to do stubs. And as a cloud platform, there is no way to run tests locally.
The toughest thing about Salesforce development is how painfully slow unit tests are on the platform. Teams can wait up to 24 hours or more just to run through an entire suite of tests. It’s basically impossible to do Continuous Delivery if this is the case.
There are also some problems specific to Apex as well. Despite the code coverage requirement from Salesforce, Force.com doesn’t make it all that easy to visualize coverage of your classes and objects. Grouping and filtering is difficult as well. How can you put tests together and create suites? How do you set your unit tests up to work with a Continuous Integration server like Jenkins or Bamboo? And how do you use reporting on your unit tests to see what is happening over time?
ApexUnit is an open source unit testing framework created by Salesforce to help address some of the issues (though unfortunately not all) mentioned above. ApexUnit is designed to make managing unit tests easier for Apex just as other popular unit testing frameworks such as TestNG and JUnit do for Java and NUnit does for C#.
ApexUnit is a command line interface (CLI) implemented in Java. It “seamlessly” integrates with CI servers like Jenkins and TeamCity in that you can use the CLI to make build steps. ApexUnit allows you to schedule and run unit tests asynchronously. You can also use it to filter and group tests into suites.
ApexUnit helps with code coverage requirements by providing metrics and test reports in JUnit XML including a feature that lets you drill down into failures for debugging. The code coverage tool provides line by line information about code coverage including helpful red and green highlighting to help see which areas require more coverage. All of this ties nicely into CI tools like Jenkins because of the standard formats used (e.g. JUnit xml reports) and the CLI. Live test logging allows you to have a better picture of what’s happening with your unit tests in real time.
To learn more about ApexUnit check out the GitHub page.
Apex Unit tests are great, but what about full on integration tests to ensure that your code is working end to end? Selenium Webdriver is a useful tool for doing this. Selenium tests are great because they allow you to test your user experience end to end in multiple browsers. Salesforce MVP Jitendra Zaa has a great series of posts on integrating Salesforce with Selenium here that are a great starting point for any Salesforce developer looking to do more than just unit test.
How Blue Canvas helped Catalyst unlock the potential of DevOps for 20 BAs and 10 developers in under a week.
How Blue Canvas improves upon Git Flow and Salesforce DX to bring DevOps best practices to Salesforce teams.