Functional Testing 03: But Wait! Not Too Lazy!

But Wait! addendum to tutorials are optional side topics, not necessary to continue progress through the tutorials, but that may skip ahead in concepts in order to expand on a topic.

In Functional Testing 03: Let’s Be Lazy you’ll notice I may have taken the laziness too far. In the tutorial I used the dreaded “copy and paste” method to create a second test. To those of us that have many, many tests this sends shivers down our spines. Our signin tests only had two tests but if we had wanted to run a hundred different signin tests copy and paste would not be the best solution.

So let’s take a little time to understand how we could remove the copy/paste.

Functional Testing 03: But Wait! Not Too Lazy!

    1. In whatever location you have saved your tutorial JMeter test plan file (.jmx) create a new text file. Edit the text file and enter these values:

      Save the file and rename it to credentials.csv. Our download my data file here. This file is going to be our test data for our signin tests. We have five users and five passwords that we want to test.
    2. In JMeter expand Signin Tests and delete Signin Test 2. Already feels a little better right? We’ve eradicated the results of our copy and pasting.
    3. Expand and rename Signin Test 1 to “Signin Test ${username}“.
      This will print the transaction controller name with the user name in the test. Makes reading the results easier when we are actually going to be using the same Module Controller for every test.
  1. Expand the parameterized controller under Signin Test ${username}.
  2. Drag the Module Controller out of the parameterized controller and drop it in Signin Test ${username}.
  3. Delete the paramterized controller.
    Your test plan should look like this.
  4. Now we will insert an object that will read our test data from our csv file. Right-click on Signin Tests (the thread group) and select Add > Config Element > CSV Data Set Config. Then drag it above Signin Tests ${username}.
  5. In the CSV Data Set Config
    Set Filename to credentials.csv – this is the file with our test data.
    Set Variable Names to username,password – here we tell it to read the first piece of data on each line in to the username variable, and the second piece of data on a line in to the password variable.
    Set Recycle on EOF to False – this tells the config element to not start reading the file from the top againwhen it reaches the end of the file.
    Set Sharing Mode to Current Thread – this makes sure that the variables created by the config object only apply to these tests and not tests in other threads or thread groups.

  6. Now that we have a way of reading the data we will instruct JMeter to loop through the data in the file and run the signin test for each username/password combination.
  7. Right-click Signin Test (the thread group) and select Add > Logic Controller > While Controller.
  8. Drag and drop the CSV Data Set Config and Signin Test ${username} into the While controller. Your test plan should look like this:
  9. In the While Controller we will need to set a condition so that the While loop knows when to finish. When the CSV Data Set Config reaches the end of the data, both variables (username and password) will have the value “<EOF>”. So we will us a bit of javascript in the While Controller’s condition field to trigger off of the username value.
  10. Set the While Controller’s condition field to:
    ${__javaScript(“${username}” != “<EOF>”)}
    Be careful with the syntax. One mistake, like forgetting the S in javaScript is capital (which I did for about 15 minutes while writing this), and it will result in an endless loop.
  11. Already we are ready to give it a test run! Click the View Results Tree and run your tests! You should one Get API Test, and six sign in tests.

    Wait, six signin tests? We only had five username/password combinations. It seems that the While controller checks the value of ${username} after it is read from the data file but also after we run our test! That’s why we have one result labelled “Signin Test <EOF>”.
  12. To handle this we will put in one more check before running our test to see if we have reached the end of the file (more specifically if ${username} = “<EOF>”.
  13. Right-click the While Controller and select Add > Logic Controller > If Controller.

    Drag and drop Signin Test ${username} into the If Controller. You’re test plan should look like this.
  14. In the If Controller set it’s condition to
    “${username}” != “<EOF>”
    Note: be sure to include the quotes around ${username} and <EOF>
    JavaScript is the default for this field so we don’t need the funky JMeter function notation we needed with the While Controller. This will only run the Signin Test if username is not yet EOF.
  15. Go ahead and run your tests again and you should no longer see the EOF test.

There we have it. We have replaced the need to copy and paste test cases and instead replaced it with a much more manageable data file. We could even have tools that create this data for us… perhaps by mining our test cases…? That would be incredible! But I digress…


Tutorial Test Plan

Leave a Reply

Your email address will not be published. Required fields are marked *