Knowledgebase

Test main function


Background

There are 4 parts to a test script. This article describes the main function (default function), also known as VU code. It’s important to note that Virtual Users will iterate over this function as long as the test is running. Each iteration should be considered a new session.


The main test code (aka “VU code”) is called after the “init code” and “setup code” has been run:

1
2
3
4
5
6
7
8
9
10
11
12
13
// 1. init code

export function setup() {
    // 2. setup code
}

export default function(data) {
    // 3. vu code
}

export function teardown(data) {
    // 4. teardown code
}

The “VU code” is put inside the export default function (aka “main”) function.

It’s in the “main” function you’ll be spending most of your time. The most important thing to grasp before writing your test code is how a VU executes the main function.

The main function life-cycle

A VU will execute the main function from start to end in sequence. Nothing out of the ordinary so far, but here’s the important part; once the VU reaches the end of the main function it will loop back to the start and execute the code all over.

As part of this “restart” process, the VU is reset. Cookies are cleared and TCP connections might be torn down, depending on your test configuration options.

Next: Test setup/teardown life-cycle hooks