As guide by Elements of Test Plan:

Thread group elements are the beginning points of any test plan. All controllers and samplers must be under a thread group. Other elements, e.g. Listeners, may be placed directly under the test plan, in which case they will apply to all the thread groups

In simple words, Thread Group contains all your main requests/sampler/element need to perform your tests. In some cases, you need to do something before running your tests, or you have to do something after running your tests. At that time, you should think about the setUp and tearDown Thread Group.

Via this post, let’s find out what setUp and tearDown Thread Group can do, and what is the exact purpose of these elements.

1. Usage

You can add setUp and tearDown Thread Groups by:

Test Plan > Add > Threads (Users) > setUp/tearDown Thread Group

These elements are the special type of Thread Group that can be utilized to:

– setUp Thread Group: perform Pre-Test Actions.
– tearDown Thread Group: perform Post-Test Actions.

The behavior of these threads is exactly like a normal Thread Group element. The differences are:

setUp Thread Group: will be executed BEFORE the test proceeds to the executing of regular Thread Groups.
tearDown Thread Group: will be executed AFTER the test proceeds to the executing of regular Thread Groups.

No matter where these elements are, they will always execute as the rule which describes above.

setUp_tearDown_1.png

Let see some example below for more detail:

Example 1: I create a Test Plan, it looks like:

  • Test Plan
    • setUp Thread Group
      • setUp Sampler
    • Thread Group
      • Sampler
    • tearDown Thread Group
      • tearDown Sampler
    • View Result Tree

Run the test, and this is the result:

setUp_tearDown_run_example_1.png
You can see, the requests under setUp will be executed first, then the requests come from normal Thread Group and the request in tearDown will be executed in the last.

Example 2: I will change the order of the Test Plan:

  • Test Plan
    • tearDown Thread Group
      • tearDown Sampler
    • Thread Group
      • Sampler
    • setUp Thread Group
      • setUp Sampler
    • View Result Tree

Run the test again

setUp_tearDown_run_example_2.png

So, even the tearDown display first, and setUp is in the last of Test Plan tree, but while running, the order is still the same: setUp –> normal thread group –> tearDown.

Note: In case we have many setUp Thread Groups, many Thread Groups, and many tearDown Thread Group. They still run with that order. And with each kind of Thread Group, for example, setUp, it will run in order the element display in the Test Plan Tree, display first run first.

tearDown Thread Group

We should pay a little bit more attention to this Thread Group. By default, this thread group won’t run if the test is gracefully shutdown.

Gracefully shutdown means the entire test is stopped at the end of any current samples. It means the pending samplers are still run until done.

Refer Thread Group post for more detail

So, if you want to make it run in this case, ensure you check option Run tearDown Thread Groups after shutdown of main threads on Test Plan element.

tearDown-run-after-main-threads.png

I will modify the example above, let it run with 10 threads, 10 loops, and run the test in both of cases checked and unchecked the option above.

First, run without checking the option

run-without-options.gif

And the tearDown Thread Group didn’t run.

Second, I will run again, and check the option

run-with-options.gif

You can see, now the tearDown Thread Group ran.

NOTE: If Test Plan is stopped, tearDown will not run even if the option is checked.

And while running in non-ui mode, the test will shutdown gracefully, not stopped.

2. Purpose

2.1 setUp Thread Group

As said from the beginning of the post, this thread group can be utilized to perform Pre-Test Actions. Make sure everything will be done before running the test. Below is some cases which we can consider to use setUp Thread Group.

  • Create a list of users that need to be run in your tests.
  • Get the data from the database and store into the .csv files or JMeter variables and use them during the test.
  • Send the email or any kind of notification to notify that the test has been started.
  • Perform some logic based on the user input via command line. For example, if property server_test=1 will lead the test plan run with hostname A and port A1. And if property server_test=2, means you want to run with hostname B and port B1.
  • etc.

2.2 tearDown Thread Group

The same as setUp Thread Group, I believe sometimes you need to do some actions after running the test. I point out some cases which might useful when using this thread group

  • Deleting users that were created in the setUp Thread Group.
  • Cleaning up the system, deleting the data which were created during the test.
  • Send the email or any kind of notification to notify that the test has been stopped.
  • Collect some reporting stuff on response data.
  • etc.

Hope this post can help you all have the overview about the setUp/tearDown Thread Group and can apply them in an appropriate way. If you have any stuck or want to discuss more this topic, please leave the comment below.

Advertisements