Test Slack Apps

Write unit tests to verify whether your Apex code is working properly. Unit tests are class methods that don’t take arguments or commit data to the database.

Slack app testing includes several files.

  • The Apex class to be tested
  • The Apex unit tests
  • ApexClass metadata file
  • ApexTestSuite metadata file

In your SFDX project, create an Apex unit test in the classes folder.

1|_ classes/
2    |_ MyAction.cls
3    |_ MyAction.cls-meta.xml
4    |_TestMyAction.cls
5    |_TestMyAction.cls-meta.xml
6|_ slackapps/
7|_ testSuites/
8    |_MyActionTestSuite.testSuite-meta.xml
9|_ viewdefinitions/

Write a Test 

Create a test class and annotate it with @isTest. Similarly, annotate your class methods with @isTest.

Name your test class using the format TestYourActionName, like TestAppHomeOpened or TestCreateRecordDispatcher.

Declare your test harness variables to store the Slack state and user session. To set up your test harness and simulate user interactions, use the Slack.TestHarness class.

1@isTest
2public class TestMyAction {
3
4  // Test harness variables
5  private static Slack.App slackApp;
6  private static Slack.TestHarness testHarness;
7  private static Slack.TestHarness.State slackState;
8  private static Slack.TestHarness.UserSession userSession;
9
10  static {
11      // Set up test harness and user session
12      slackApp = Slack.App.ApexSlackApp.get();
13      testHarness = new Slack.TestHarness();
14      slackState = testHarness.getNewSlackState();
15      userSession = slackState.createUserSession();
16  }
17
18   // Methods for testing
19   @isTest static void test1() {
20      // Implement test code
21   }
22
23   @isTest static void test2() {
24      // Implement test code
25   }
26
27}

For more information, see Testing Examples.

Tip

Add the ApexClass Metadata File 

The ApexClass metadata file represents an Apex class, which you include for each test class.

TestMyAction.cls-meta.xml:

1<?xml version="1.0" encoding="UTF-8"?>
2<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3    <apiVersion>54.0</apiVersion>
4    <status>Active</status>
5</ApexClass>

Add the ApexTestSuite Metadata File 

The ApexTestSuite metadata file represents a suite of Apex test classes to include in a test run.

MyActionTestSuite.testSuite-meta.xml:

1<?xml version="1.0" encoding="UTF-8"?>
2<ApexTestSuite xmlns="http://soap.sforce.com/2006/04/metadata">
3    <testClassName>TestMyAction</testClassName>
4    <testClassName>TestMyOtherAction</testClassName>
5</ApexTestSuite>

See Also 

What Are Apex Unit Tests?

Beta Feature

This feature is not generally available. It is not part of your purchased Services. This feature is subject to change, may be discontinued with no notice at any time in SFDC’s sole discretion, and SFDC may never make this feature generally available. Make your purchase decisions only on the basis of generally available products and features. This feature is made available on an AS IS basis and use of this feature is at your sole risk.