Test Cases & Test Suites

Just like many of us these days, my todo list keeps getting longer and longer. One of those things that I never get enough time to do is blog, and contribute articles to the developer community. My current project is an article on test patterns and best practices I desperately want to get wrapped up before Dreamforce

One of the sections in this article shall talk about the difference between test cases and test suites. Wikipedia describes a test case as "a set of conditions or variables under which a tester will determine whether an application or software system is working correctly or not." And a test suite as "a collection of test cases that are intended to be used to test a software program to show that it has some specified set of behaviours."

The difference is subtle but important when we look at building our tests on the Force.com platform. The important is further magnified as more and more people leverage the platform for complete solutions which extend the Salesforce.com product offerings, or invent something completely unique.

But this is a developer blog, so let's jump to the code.

From the docs, a typical test case might look like the following, with generally 1..n testMethods.

public class myClass {
static testMethod void myTest() {
code_block
}
}


This is a great start, but as we start to look at test suites, I might want to test the complete end-to-flow of piece of the application, or user story to borrow a term from Scrum. To support such a framework I often use a single testMethod as a 'wrapper', and then separate the logical (business) functions I want to test into methods, starting with a setup method to initialize all the objects I might need. Below is a snippet from a test suite which should demonstrate the idea (and the difference) of how you can create test suites to model the business process of your app.

/**

  * Contains all the test cases for user stories use cases

  */

@isTest

private class UserStoryTestSuite {

static Theme__c themeSO = null;

static Scrum_Team__c teamSO = null;

static Release__c releaseSO = null;

static Sprint_Backlog__c backlogSO = null;

static testMethod void manageUserStory()

{

setup();

manageADetailedStory();

manageAnEpicStory();

showPrintNotecards();

prioritizeStories();

prioritizeStoriesLegacy();

prioritizeStoriesWithFlex();

completingTasksInAStory();

}

static void setup()

{

teamSO = TestHelper.createScrumTeam('Test Scrum Team');

themeSO = ThemeTestHelper.addTheme(teamSO);

releaseSO = ReleaseTestHelper.createRelease(); 

backlogSO = BacklogTestHelper.createBacklog('Backlog Test', teamSO);

}

//the rest of the class has been removed for brevity.

 The article I mentioned at the start of this post will contain a lot more detail, but for now hopefully this will spur some food for thought.

tagged Bookmark the permalink. Trackbacks are closed, but you can post a comment.
  • http://sfdc-developer.blogspot.com/ Gary B

    I’d recommend this, though with some caveats. When deploying code to your production environment, your test classes (either in the production environment or in your deployment payload) are run. If your setup block takes a long time and is called from all your tests, deployment will take a long time as a result. Therefore, it can pay to write your tests cleverly and test several things in one test. Obviously there is a balance between saving deployment time and good testing practice!
    Reason I mention it is I have some environments with lots of tests and deployments can take around 15 minutes while the tests run. This is especially painful when someone’s changed a workflow or validation rule since the last time you refreshed the sandbox you’ve developed in and your tests fail!
    Outlining my thoughts in a blog post is also on my to-do list – maybe before Christmas…

  • http://www.tehnrd.com Jason

    Using this type of setup method can be incredibly useful when you have multiple tests involve OpporunitiyLineItems. To properly create this object for testing you must create products, price books, and price book entries. A lot of code that could be condensed into one method and then called from many tests.

  • http://quintonwall.com Quinton Wall

    Gary, You have a good point, and this pattern is one way to reduce the overhead of setup methods. In a ‘typical’ test class you may have many testMethods each calling a setup method to initialize variables etc. Using the test suite pattern we only need to call setup once —thus greatly reducing the overhead and making a more realistic, business focused test suite.