---
schema_version: "1"
id: 648ae9153d6a836bdd0195fb2df2d7933af17a67cbd0d69c7cc987df3ec32c34
title: MUnit Before and After Scopes
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/munit/latest/before-after-scopes-reference.md
canonical_url: https://docs.mulesoft.com/munit/latest/before-after-scopes-reference
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 0753bc3ab9696d2986b2d40e35b08af665849fffdbc0f7ed1e96b2679a046c42
description: MuleSoft Documentation Site
release: "3.7"
latest: true
---
---
product: MUnit
version: 3.7
is-latest-version: true
---

# MUnit Before and After Scopes

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

![logo acb active](/mulesoft/current/en-us/_/img/icons/anypoint-code-builder.svg "Anypoint Code Builder")Anypoint Code
Builder

![logo studio active](/mulesoft/current/en-us/_/img/icons/studio.svg "Anypoint Studio")Anypoint Studio

MUnit Before and After scopes let you run setup and teardown code at the suite or test level in a
MUnit test suite. Use Before scopes to prepare shared state and After scopes to clean it up, so each
test starts and ends in a controlled state.

## Before Suite Scope

The MUnit Before Suite scope runs once before all tests in the suite.

For example, suppose you have an MUnit Test Suite file with four tests. The code inside an MUnit
Before Suite runs just once, before your four tests.

> [!NOTE] You can only use one Before Suite scope per MUnit test suite.

Use a Before Suite scope to set up state that every test in the suite shares, such as seeding an
in-memory database, starting a server utility, or storing values that the tests read. In the
example, the `jobtitlelookup.csv` file creates the table and its initial contents when the database
server starts, and the Before Suite adds one shared row that all tests in the suite query:

Before Suite example

```xml
<dbserver:config name="MUnit_DB_Server_Config">
  <dbserver:connection csv="jobtitlelookup.csv" database="DATABASE_NAME"
    connectionStringParameters="MODE=MySQL" />
</dbserver:config>

<munit:before-suite name="before-suite" description="Adds a shared row for the whole suite">
  <dbserver:execute config-ref="MUnit_DB_Server_Config"
    sql="INSERT INTO jobtitlelookup VALUES ('Support Engineer','10','SUP');" />
</munit:before-suite>

<munit:test name="munit.test" description="Test">
  <!-- Every test in this suite can query the row added by the before-suite -->
</munit:test>
```

> [!NOTE] The `before-suite` scope is located outside the MUnit test, so the insert runs once before
> the first test instead of once per test.

When you place a before-suite in the Anypoint Studio canvas, the scope locates outside the MUnit
test.

## After Suite Scope

The MUnit After Suite scope runs once after all tests in the suite complete.

For instance, let’s suppose you have an MUnit Test Suite File with four tests. The code inside an
MUnit After Suite, runs just once, after all your tests.

> [!NOTE] You can only use one After Suite scope per MUnit test suite.

Use an After Suite scope to tear down whatever the Before Suite created, so the next suite run
starts from a clean state. The example removes only the row that the Before Suite added, leaving the
contents loaded from the CSV file untouched:

After Suite example

```xml
<munit:before-suite name="before.suite" description="Adds a shared row for the whole suite">
  <dbserver:execute config-ref="MUnit_DB_Server_Config"
    sql="INSERT INTO jobtitlelookup VALUES ('Support Engineer','10','SUP');" />
</munit:before-suite>

<munit:test name="munit.test" description="Test">
  <!-- some more mule code here -->
</munit:test>

<munit:after-suite name="after.suite" description="Removes the data created for the suite">
  <dbserver:execute config-ref="MUnit_DB_Server_Config"
    sql="DELETE FROM jobtitlelookup WHERE JOBTITLEID='SUP';" />
</munit:after-suite>
```

> [!NOTE] The `after-suite` scope is located outside the MUnit test, so the cleanup runs once after
> the last test completes.

When you place an after-suite in the Anypoint Studio canvas, the scope locates outside the MUnit
test.

## Before Test Scope

The MUnit Before Test scope runs before each individual test in the suite.

For instance, let’s suppose you have an MUnit Test Suite file with four tests. The code inside an
MUnit Before test runs before each of your four tests; it runs four times.

> [!NOTE] You can only use one Before Test scope per MUnit test suite.

Use a Before Test scope to set up state that each test needs individually, such as inserting a
record, creating a file, or setting a variable that the test consumes. The following example inserts
a record and sets the variable that the flow under test reads:

Before Test example

```xml
<munit:before-test name="before.test" description="Creates the record each test needs">
  <dbserver:execute config-ref="MUnit_DB_Server_Config"
    sql="INSERT INTO jobtitlelookup VALUES ('Culinary Team Member','10','HIR');" />
  <set-variable variableName="jobid" value="HIR" />
</munit:before-test>

<munit:test name="munit.test" description="Test">
  <munit:execution>
    <flow-ref name="selectFlow" />
  </munit:execution>
  <munit:validation>
    <munit-tools:assert-equals actual="#[vars.job]" expected="Culinary Team Member" />
  </munit:validation>
</munit:test>
```

> [!NOTE] The `before.test` scope is located outside the MUnit test, so each test starts from the
> same record and variable value without repeating the setup in every test.

When you place a before-test in the Anypoint Studio canvas, the scope locates outside the MUnit
test.

## After Test Scope

The MUnit After Test scope runs after each individual test in the suite completes.

For instance, let’s suppose you have an MUnit Test Suite file with four tests. The code inside an
MUnit After Test runs after each of your four tests; it runs four times.

> [!NOTE] You can only use one After Test scope per MUnit test suite.

Use an After Test scope to undo what the Before Test created, so one test doesn’t leave data behind
that affects the next one. The following example deletes the record that the Before Test inserted:

After Test example

```xml
<munit:before-test name="before.test" description="Creates the record each test needs">
  <dbserver:execute config-ref="MUnit_DB_Server_Config"
    sql="INSERT INTO jobtitlelookup VALUES ('Culinary Team Member','10','HIR');" />
</munit:before-test>

<munit:test name="munit.test" description="Test">
  <!-- some more mule code here -->
</munit:test>

<munit:after-test name="after.test" description="Removes the record created for the test">
  <dbserver:execute config-ref="MUnit_DB_Server_Config"
    sql="DELETE FROM jobtitlelookup WHERE JOBTITLEID='HIR';" />
</munit:after-test>
```

> [!NOTE] The `after.test` scope is located outside the MUnit test, so the cleanup runs after each
> test rather than once after the suite.

When you place an after-test in the Anypoint Studio canvas, the scope locates outside the MUnit
test.

## Before Suite Reference

<table><colgroup><col> <col></colgroup><thead><tr><th>Attribute Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>name</code></p></td><td><p>Defines the name of the Before suite. The name must be unique inside the suite.</p></td></tr><tr><td><p><code>description</code></p></td><td><p>Describes the actions performed.</p></td></tr></tbody></table>

## After Suite Reference

<table><colgroup><col> <col></colgroup><thead><tr><th>Attribute Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>name</code></p></td><td><p>Defines the name of the After suite. The name must be unique inside the suite.</p></td></tr><tr><td><p><code>description</code></p></td><td><p>Describes the actions performed.</p></td></tr></tbody></table>

## Before Test Reference

<table><colgroup><col> <col></colgroup><thead><tr><th>Attribute Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>name</code></p></td><td><p>Defines the name of the Before test. The name must be unique inside the suite.</p></td></tr><tr><td><p><code>description</code></p></td><td><p>Describes the actions performed.</p></td></tr></tbody></table>

## After Test Reference

<table><colgroup><col> <col></colgroup><thead><tr><th>Attribute Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>name</code></p></td><td><p>Defines the name of the After test. The name must be unique inside the suite.</p></td></tr><tr><td><p><code>description</code></p></td><td><p>Describes the actions performed.</p></td></tr></tbody></table>

## See Also

- [MUnit Test Structure Fundamentals](https://docs.mulesoft.com/munit/latest/munit-test-concept)

- [Install the MUnit Database Server Module](https://docs.mulesoft.com/munit/latest/db-util)

- [Configure the MUnit Database Server Utility](https://docs.mulesoft.com/munit/latest/db-server-cookbook)

- [Install the MUnit FTP Server Module](https://docs.mulesoft.com/munit/latest/ftp-util)

- [Enable Flow Sources in MUnit](https://docs.mulesoft.com/munit/latest/enable-flow-sources-concept)
