Sub topic 7
Creating and running flow tests
This chapter assumes you can build a record-triggered flow in Flow Builder, and that you have used the Debug button from the earlier chapters. You also need a sandbox or Developer Edition org where you can create and activate flows. No Apex is needed here. The next chapter covers Apex tests.
What a flow test is
A flow test is a saved set of inputs and expected results for a record-triggered flow. You describe the record that starts the flow, and for update flows you also describe the changed values. You then list assertions, which are conditions that must be true after the flow runs. Salesforce runs the flow against your inputs and reports pass or fail for each assertion.
Debugging and testing answer different questions:
| Row | Debug run | Flow test |
|---|---|---|
| Purpose | Explore why a flow behaves a certain way | Confirm that behavior stays correct |
| Repeatable without re-entering values | No, you re-enter or reselect the record | Yes, inputs are stored with the test |
| Pass/fail result | No, you read the element trace yourself | Yes, per assertion |
| Can be run after every change | Manually | Yes, from Flow Builder or the flow's test results |
| Stored with the flow | No | Yes, as a `FlowTest` metadata component |
Use the debugger to find a problem. Then write a test so the problem cannot return unnoticed.
What flow tests cover
Flow tests are available for record-triggered flows, both before-save (fast field updates) and after-save (actions and related records). They do not cover screen flows, scheduled flows, or other flow types. For those, use debug runs, or use Apex tests where the flow can be started from code (see the next chapter).
Check the current release notes and limits for details, because this feature has changed between releases. As documented at the time of writing, a flow test exercises the flow's main path. It does not wait for scheduled paths or asynchronous paths to execute.
The lifecycle of a test run
For an update trigger, the test builds the record in its initial state and then applies the updated values. The flow therefore sees a real before and after, so $Record__Prior and "is changed" entry conditions behave as they do in production.
A worked example flow
This chapter uses one small flow so each test step is concrete.
The flow is a before-save, record-triggered flow named Opportunity_Set_Next_Step:
- Object: Opportunity.
- Trigger: A record is updated.
- Entry condition:
StageNameequalsClosed Won, and the "Only when a record is updated to meet the condition requirements" option is selected. - Action: An Assignment element sets
$Record.NextSteptoSend contract.
Build and save this flow before you go on. You do not need to activate it to create a test, but the flow must be saved.
Creating a test
You create tests inside Flow Builder, with the flow open.
- In the Flow Builder toolbar, open the Tests menu and choose New Test. If the option is unavailable, check that the flow is a record-triggered flow and that you have saved it.
- Enter a Test Label and a Test API Name. Use a label that states the scenario, such as
Closed Won sets next step. A name that says onlyTest 1becomes hard to read as the list grows. - Add a Description with the business rule being checked.
- Under Initial Record Values, set the fields of the record as it exists before the change. For this flow, set
StageNametoProspecting,Nameto a test name, andCloseDateto a date. Add every field that the object requires, or the test record cannot be built. - Under Updated Record Values (shown for update triggers), set
StageNametoClosed Won. - Under Assertions, add conditions that must hold afterward. Assert that
$Record.NextStepequalsSend contract. Write an error message that says what was expected, for exampleNextStep should be set when the stage is Closed Won. - Save the test.
Each test checks one scenario. If you want to cover several scenarios, create several tests rather than one test with many unrelated assertions.
Choosing what to assert
An assertion is a condition built from a resource, an operator, and a value. Match the assertion to the kind of flow you are checking.
| Flow behavior | What to assert |
|---|---|
| Before-save field update | The field on `$Record` has the expected value |
| Entry condition should not match | The field the flow would have changed is still unchanged |
| Decision outcome | A flow variable or the record field set on that branch has the branch's value |
| Formula or calculation | The result field or variable equals the expected number or text |
Write positive and negative tests in pairs. The positive test confirms the flow does its job when the entry condition is met. The negative test confirms the flow stays out of the way when it is not met. Our example needs two tests:
Closed Won sets next step: initialProspecting, updatedClosed Won, expectNextStepequalsSend contract.Other stage leaves next step empty: initialProspecting, updatedNegotiation/Review, expectNextStepis null.
For the second test, assert with the operator that checks a field is null, or compare to an empty value. This test is what catches a change that accidentally widens the entry condition.
Boundary and edge tests
Entry conditions often fail at the edges. Think about what changes between initial and updated values:
- Already in the target state. Start the record at
Closed Wonand update a different field. With "only when a record is updated to meet the condition requirements" enabled, the flow should not run again. A test that assertsNextStepstays empty proves it. - Empty values. Leave an optional field blank in the initial record and see whether a decision or formula handles the null.
- Several conditions. If a decision checks an amount threshold, create tests just below, at, and just above the threshold.
- Create versus update. If the trigger is "created or updated", write separate tests for each.
Running tests
You can run tests from two places.
- From Flow Builder. Open the Tests menu, choose Run Tests, and select the tests to run. The results panel lists each test and each assertion.
- From the flow's test results. In Setup, open the Flows list, open the flow, and view test runs. This is where you look up earlier results.
A passing test shows each assertion as passed. A failing test shows the assertion that failed, with your error message and the actual value found. If the flow itself throws an error during the test, such as a required field missing or a limit exceeded, the test fails with the flow error instead of an assertion result. That makes a flow test a useful check for fault conditions as well as logic mistakes.
When a test fails
Work from the message to the cause:
- Read the failed assertion and compare expected and actual values.
- If the actual value is empty, the flow probably did not start. Check the entry conditions against the initial and updated values in the test. A typo in a picklist value is a common cause.
- If the actual value is wrong but not empty, open the flow and use a debug run with the same record values to follow the elements one by one.
- If the test reports a flow error rather than an assertion failure, treat it as a runtime failure. Look for missing required fields in the test record and for validation rules that reject your values.
- Fix the flow or fix the test, whichever one is wrong, and run the tests again.
Distinguish a bad test from a bad flow. When requirements change on purpose, update the test. Do not loosen assertions only to make a failing test pass.
Keeping tests with the flow
Each flow version has its own relationship to tests. When you save a new version, rerun the tests against it before you activate it. A reasonable routine is:
- Edit the flow and save a new version.
- Run all tests in Flow Builder.
- Fix failures.
- Activate the version.
Tests are stored as FlowTest metadata, so you can retrieve them and deploy them between orgs with your normal release process. With the Salesforce CLI, you can retrieve all of them into a project:
sf project retrieve start --metadata FlowTestDeploying tests to production together with the flow lets you run the same checks in each environment. Confirm the test deployment behavior against current documentation for your org's release.
Limits of flow tests
Know what a flow test cannot tell you, so you do not rely on it too much:
- It exercises one triggering record, so it does not show how the flow behaves with 200 records in one transaction. Bulk behavior needs the Apex tests covered in a later chapter.
- It does not test screen flows or user interaction.
- It does not prove that other automations on the object, such as triggers or other flows, will cooperate with yours in production.
- It does not wait for scheduled or asynchronous paths.
Treat flow tests as the fast first layer. They are quick to write and need no code, which makes them the right tool for checking decisions, entry conditions, and field values. Add Apex tests for bulk data and for flows that start from code.
Practice
Exercise
A before-save flow sets Case Priority to High when Origin changes to Phone. You write one test: initial Origin is Email, updated Origin is Phone, and the assertion checks that Priority is High. The test passes. Name one more test you should add, and say what it checks.
Show answer
Add a negative test. For example, start with Origin Email, update Origin to Web, and assert that Priority is not High (unchanged from its initial value). This confirms the entry condition does not match other origins. You could also add a test where Origin is already Phone and another field changes, to check that the flow does not run again when the record already meets the condition.
Exercise
A flow test fails and the result shows the flow error "Required fields are missing" rather than a failed assertion. What is the most likely cause, and where do you fix it?
Show answer
The test's initial record values do not include a field that the object requires, so the test record cannot be created. Fix it in the test by adding values for all required fields in the Initial Record Values. The flow itself is probably not at fault.
Exercise
Why does a flow test not replace a bulk test with 200 records?
Show answer
A flow test runs the flow for a single triggering record. Problems such as queries or DML inside loops only exceed governor limits when many records are processed in one transaction, so they do not appear in a single-record test. A bulk Apex test is needed to expose them.