How to Write Test Scripts

One of the early steps in the software testing phase is to outline and create test scripts, test cases, and test scenarios. But are you aware of how to begin and continue this task to generate a perfect test suite? 

This article will give you a thorough guide on how to write test scripts.

Test scripts vs test cases vs test scenarios

Test scripts vs test cases vs test scenarios

Test Script 

A Test Script includes a set of instructions or a short program that is capable of testing a part of a function or an entire function of a software system. Some of the popular programming languages used for scripting are, 

  • Java
  • Perl
  • Python
  • Ruby
  • VB script 

By writing scripts for each functionality, we can validate the overall functionality of the application. 

Test Case

A Test Case includes the steps that we need to carry out to test the software system. Test cases are also used to verify the functionality of a specific aspect of the software system. 

In other words, a Test Case can be also referred to as an arrangement of conditions based on the testing expectations. 

A collection of test cases that are used to test software is called a test suite.

Test Scenario

A Test Scenario can be considered as a function that requires and can be tested. Test scenarios include a set of test cases that assist the testing team to figure out the positive and negative characteristics of an application. 

In general, a test scenario provides a high-level idea of what a tester should be testing.

Difference between Test scripts, test cases, and test scenarios

The following table summarizes the difference between a test script, test case, and test scenario.

Test script Test case Test Scenario
A set of instructions A step by step by procedure A one-liner description
Developed in the form of scripting. Developed in the form of a test case template Developed based on test cases
Tells how to test Tells what to test Defines user actions
Includes details about how to test each functionality Includes testing strategy, testing process, preconditions, and expected results. Includes information about what to test in the software system.
Low-level actions Low-level actions High-level actions
More time and resources are needed More time and resources are needed less time and resources are required
Hard to maintain Hard to maintain Easy to maintain

What are the test script creation methods?

In this section, let us see how we can write a test script and the different methods available for that.

There are 3 main approaches a tester can follow to create a test script.

Record/playback method

This approach makes the tester write any code and avoid recording user actions. Coding is required to fix issues or fine-tune the automation behavior. This approach is more manageable than writing an entire test script from the very beginning due to the availability of the entire code.

Usually, this method is popular with simple programming languages such as VBScript.

Given below are the steps that a software tester needs to follow to generate a test script using the Record/playback method.

  • Select a suitable tool for the record/playback method. 
  • Carry out a set of user actions on the application.
  • Record the user actions.
  • Create a test script that can “playback” or repeat the operations automatically. (This is also known as the stimulation part of the test script)
  • Add steps to the script depending on the objects to be tested. (This is also known as the validation part of the test script)

Once the first 4 steps are completed, the tool will,

  • Identify the objects in the user interface that are tested.
  • Allow testers to observe the hierarchy of objects in the application under test. 
  • Allow testers to amend the automated tests by choosing the objects and defining user actions.

Keyword/data-driven scripting method

Both testers and developers are engaged in this method. But their tasks are separated clearly. The testers should define the test using keywords. The testers need to have an understanding of the underlying test code. Developers develop and update the test script code using the keywords provided by the testers. 

Given below are the steps that a software tester needs to follow to generate a test script using theKeyword/data-driven scripting method.

  • Select a suitable tool for the Keyword/data-driven scripting method. 
  • Specify a set of “keywords” that define end-user actions. 
  • A single keyword that describes the user action is considered as the simulation part of the test script.
  • A set of keywords and values of expected data for every component of the UI that is being tested are used for the validation part of the script.
  • Generate a test script that executes the required actions on the application under test.

Following is a list of software testing tools that are suitable for the keyword/data-driven scripting method.

Writing code method

This approach allows us to record or playback to generate a test script. Then the scripts for testing should be developed using a simple programming language such as JavaScript or Ruby. The selected programming language does not need to be the same language used to develop the software system.

Given below are the steps that a software tester needs to follow to generate a test script using theKeyword/data-driven scripting method.

  • Select a suitable tool for the Writing code method. 
  • Automate the simulation part of the test script with the assistance of built-in commands of the testing tool by selecting objects.
  • Locate the UI objects for the validation part.
  • Write code to validate and check the expected result of each UI object.

Following is a list of software testing tools that are suitable for the keyword/data-driven scripting method.

What are the common steps and things to consider when writing test scripts?

  • Provide a unique ID for each test script to identify them separately. 
  • Give a small description for each test script stating the unit, feature, or function that will be tested using that script.
  • Define the assumptions.
  • Define the preconditions and postconditions.
  • Gather test data depending on the variables, preconditions, and test type.
  • Outline the test flow properly. 
  • Execute the test script.
  • Compare the outcomes with the expected results and postconditions.
  • Identify whether the test passed or failed.

Example of a Test script

Let us see how we can create a test script for the following login functionality of a library website.

Example- Home page
Example- Login screen
Example- Welcome screen
  • Define how to locate the login fields (i.e. “Email” and “Password” fields). One of the basic methods is to use the CSS element IDs.
  • Load the home page of the website and click on the “login” button which navigates to the login screen. 
  • Check whether the login page loads successfully with the “Email” and “Password” fields. 
  • Enter the email “ben@gmail.com” and password “ben123” and click the “Log in” button.
  • Define how users can locate the title of the Welcome screen once a user successfully logs in. We can use the CSS element ID for this.
  • Check whether the title of the Welcome screen is loaded correctly.
  • Read and verify whether the title text is “Welcome CharlesWelcome to Osprey library! ”.
  • If the title is the expected title, the test has passed. Otherwise, the test has failed.

What are the tips for creating a Test Script?

Given below are some tips that will help us to create perfect scripts for testing.

  • Understandability and transparency

The scripts should be very clear in such a way that all the testers can understand. Each step of the test script must be comprehensible and consistent. This will save the time spent on clarifications and make the testing processes smooth.

  • Simplicity

When creating the scripts, we have to make sure that the script includes only a single action for testers to test. This will help to verify the functionally of each function correctly. Besides, the testers will not fail to miss steps in the software testing process.

  • Logicality

The scripts should be logical and the test paths should be defined from a user’s point of view. In that case, we need to come up with all the various paths that the users would use when dealing with the application.

When to use the test script approach?

There are several reasons why we need to use the test script approach. Some of them are,

  • To ensure that all the steps and functions of the application are tested and the test results are accurate.
  • To minimize the number of errors appearing during the testing process.
  • When there is no way to guarantee the expected result of a function.
  • To verify the user performance.

How to write scripts on ETL testing 

ETL (Extract/Transform/Load) is a process that we execute to extract data from source systems, convert the information into a persistent data type, and load the data into separate storage. 

ETL testing allows us to validate, verify, and qualify data and avoid record duplication and data loss.

In general, ETL scripts for testing are written using SQL and include two main queries. They are,

  • A query to extract data from its source.
  • A query to extract data from the target storage. 

Following are the steps to write ETL scripts for testing.

  • Load the inputs into the database.
  • Create an empty SQL script by giving a name description.
  • Provide comments if required.
  • Add the queries.
  • Run tests.

How to write scripts on API testing. 

API testing checks whether the APIs developed for a software system operate properly with the expected level of security. We can write scripts for API when the server gets a response. 

For example, with the Postman, scripts can be written to test API requests using Javascript. These scripts are also known ad Post-Request scripts. The scripts can be executed for a single request, folder, or collection. 

Following are the steps to follow to add a test script to a request in postman.

  • Go to postman console by pressing ctrl+alt+c.
  • Remove the old logs from the console.
  • Give the URL in the URL text field.
  • Go to the Tests tab and enter the test lines.
  • Click on the send button.
  • See the postman console for the outcome.

The entire testing process cannot be completed only with scripts. However, API testing can be made very simple with Accelatest.

Now that you know everything about writing a test script, let’s focus on writing test cases.

How to write test cases

Let’s see some of the best practices you can follow when writing test cases.

Useful tips when writing a test case

  • Provide strong titles or test case IDs

It is a best practice to give a strong title that aligns with the module and the content in the test case. For instance, using the title as “Login page” when testing the login page is ideal. 

If the tool that you are utilizing does not allow you to provide a title, make sure that you give a unique ID for the test case and reference the IDs in a separate location.

  • Provide a strong description

Provide a strong description of the test case by defining what is included in the test case and the component that is being tested. In addition, we can add other details such as,

  • Test environment
  • Test inputs
  • Assumptions
  • Preconditions

But we need to make sure that the description is short and easy to read when writing test cases.

  • Define the test steps clearly

We have to outline and define the steps in the test cases easily as sometimes the testers who write the test case are not the testers who execute the tests. All the steps must consist of the required details and how the test should be carried out. But avoid including unnecessary details.

  • Define the expected result and outcome

By defining the expected result and outcome, we can verify whether the tests are passing or failing.

  • Make it reusable

If we create reusable test cases, we can make use of them for a long period. Hence the workload for the testing team is minimized and the time spent on creating the test cases is minimized.

Struggling to find the best test case management tool? Try AccelaTest. Or choose one from the Best Free Test Case Management Tools of this year

Example of a Test Case

Let’s see an example of a test case to understand it better.

Title: Home Page 

Description: Any user should be able to view the home page

Precondition: The user must click on the website link to navigate to the home page.

Assumption: Works on any browser

Test Steps:

  • Google search the company.
  • Click the website link.

Expected Result: The home page is loaded with the website title, sign-up, and login buttons.

If you are doing regression testing,  here is a good guide on how to identify the best test cases for regression testing.

Conclusion

Test scripts and test scenarios are essential components to get the “software testing” job done precisely. But we fall into trouble if we begin testing Without proper knowledge about when to use and how to create these. Hope this article has covered every aspect of how to create test scripts together with the test cases and scenarios, and their differences.