How to Write Test Cases for Automation in 2024

5/5 - (1 vote)

How to Write Test Cases for Automation Imagine this:- You’ve been tasked with writing test cases for automation and you’re not sure where to start. Sound familiar? Don’t worry, we’ve all been there! So, let’s embark on this journey together, shall we?

How to Write Test Cases for Automation

How to Write Test Cases for Automation

Let’s begin with the basics. A test case is a set of conditions used to determine whether a system behaves as expected. When it comes to writing test cases for automation, the aim is to create these conditions for an automated testing system.

Why are Test Cases Crucial in Automation?

You might wonder, Why are test cases so important? In the realm of automation, they serve as the road map for the tester, ensuring that the automated testing system performs as intended. Without well-constructed test cases, we might miss out on identifying potential bugs and errors. And we wouldn’t want that, would we?

Subscribe to Our LinkedIn Newsletter

The Anatomy of a Test Case

Before we delve into the art of writing test cases, let’s get familiar with their components. Typically, a test case includes the following key elements:

  1. Test Case ID: A unique identifier for each test case.
  2. Test Case Description: A brief explanation of what the test case is about.
  3. Preconditions: Any prerequisites that need to be met before the test case is executed.
  4. Test Steps: The sequence of actions to be performed in the test case.
  5. Expected Result: The outcome we anticipate when the test case is successfully executed.
  6. Actual Result: The result that we actually obtain when the test case is executed.
  7. Pass/Fail: A note on whether the test case passed or failed based on the actual result.

The Journey Begins: Crafting Your First Test Case

How to Write Test Cases for Automation
How to Write Test Cases for Automation

So, how do you go about creating your first test case? Here’s a step-by-step roadmap to guide you through this journey.

Step 1: Understand the Requirements

  • Read all available system documentation.
  • Consult with the developers for deeper insights.
  • Use the system to gain first-hand experience.

Step 2: Define the Scope

  • Identify the key features that need testing.
  • Understand and acknowledge any limitations or areas that won’t be tested.
  • Prioritize based on factors like risk, importance, and frequency of use.

Step 3: Write the Test Case

  • Assign a unique ID to each test case.
  • Write a clear and concise description.
  • List any preconditions that must be met before testing.
  • Outline the detailed steps to be followed during the test.
  • Specify the expected results and leave room for actual results.

Step 4: Review and Refine

  • Check the test case for clarity and comprehensibility.
  • Review the logical flow of the test steps.
  • Verify that the expected results align with the system’s intended behavior.

Example – How to Write Test Cases for Automation

Example of an automation test case for a login feature on a website. Here are the details:

Test Case ID: TC001

Description: Verify that a user is able to successfully log in using valid credentials.

Test Steps:

  1. Navigate to the login page of the website.
  2. Enter a valid username in the username field.
  3. Enter a valid password in the password field.
  4. Click on the login button.

Expected Result: The user should be successfully logged in and redirected to the homepage of the website.

Automation Steps:

  1. Use a tool or script to automatically navigate to the login page (e.g., Selenium WebDriver can be used to automate browser actions).
  2. Use the tool or script to automatically enter the valid username and password into the respective fields.
  3. Use the tool or script to automatically click on the login button.
  4. Use assertions to verify that the user is redirected to the homepage after successful login.

This is a simple example of an automation test case. The specifics of how you write the automation script will depend on the automation tool you’re using and the programming language you’re writing the script in.

Example – How to Write Test Cases for Automation using gherkin

Let’s take an example of an automation test case for a login feature on a website. Here are the details:

How to Write Test Cases for Automation

In Gherkin language, each line in the scenario starts with a keyword: Given, When, Then, And.

  • Given is used to describe the initial state or context.
  • When is used to describe an action.
  • Then is used to describe the expected outcome or result.
  • And is used to continue any of the above statements.

Remember, the specifics of your Gherkin script will depend on the exact requirements and behaviors of the system you’re testing.

Best Practices for Writing Test Cases

Now that we’ve covered the basics, let’s touch upon some best practices for writing test cases.

Keep It Simple

When crafting test cases, remember the KISS principle – Keep It Simple, Stupid. Avoid complex language and jargon. Your test case should be easily understandable to anyone who reads it.

Be Specific

Precision is key when writing test steps and expected results. Vague instructions can lead to confusion and inconsistent results.

Use Realistic Data

Whenever possible, use realistic data in your test cases. This will ensure that your test cases mirror real-world scenarios.

Review Regularly

Regularly review and update your test cases to ensure they remain relevant and effective. As the system under test evolves, so too should your test cases.

Test Cases for Automation – Statistics

Here are some real-time statistics on automation test cases, based on the information available:

  • As per, it’s essential to collect key automation metrics such as flaky tests, automation coverage, and defects

  • Around 33% of companies seek to automate between 50% to 75% of their testing process, while approximately 20% aim to automate more than 75% of the testing process aimultiple.

  • Essential metrics in test automation include total test duration, unit test coverage, path coverage, requirements coverage/test cases by requirement, and the percentage of tests passed or failed sealights.

  • Real-time testing scenarios can involve things like testing an e-commerce website’s performance during a flash sale, where there’s a surge in traffic testsigma.

  • The average percentage of test cases automated ranges from 25-35% zipdo.

  • Real-time dashboards revolutionize test reporting by offering immediate insights into test execution statuses, enabling teams to swiftly react thegreenreport.

  • Automatable Test Cases is a measure of the percentage of test cases that can be automated with respect to the total number of test cases browserstack.

Final Words

So, conlcusion for How to Write Test Cases for Automation is here, Writing test cases for automation might seem like a Herculean task at first, but with practice and patience, it becomes second nature. Remember, the goal is to create a clear, concise guide that ensures the automated testing system functions as expected. So, buckle up, give it a shot, and before you know it, you’ll be a pro at writing test cases for automation. Happy testing!

FAQs – How to Write Test Cases for Automation

1. How are automation test cases written?

Writing automation test cases involves a few key steps. Firstly, you need to understand the system’s requirements by reading documentation, consulting with developers, and using the system yourself. Then, define the scope of your test case, identifying what you’re testing and what you’re not. Next, write the test case, assigning it a unique ID, providing a clear description, listing preconditions, outlining the test steps, and specifying the expected results. Finally, review and refine your test case to ensure it’s clear, concise, and easy to follow.

2. How do you document automated test cases?

Documenting automated test cases often involves creating a structured document that includes information like the test case ID, description, preconditions, test steps, expected results, actual results, and whether the test passed or failed. This document should be easily understandable and accessible to all relevant team members. Tools like Excel, Google Sheets, or dedicated test management software can be used for this purpose.

3. How do you write an automated unit test case?

Unit test cases are written slightly differently from regular test cases, as they focus on testing individual units of code rather than entire systems or features. To write an automated unit test case, you start by identifying the function or method you want to test. Then, you decide on the inputs for the function and the expected output. A unit test framework is then used to write the test case, which involves calling the function with the chosen inputs and verifying that the output matches the expected result.

4. Which test cases to automate with example?

Deciding which test cases to automate depends on several factors including the complexity of the test, how often it will be run, and the potential for human error in manual testing.
For example, suppose you have a login feature on your website. This is a critical feature that needs to be tested frequently, and the test steps are always the same: enter the username and password, click the login button, and verify that the user is logged in successfully. This is a perfect candidate for automation because it’s a repetitive, straightforward test that needs to be performed often.
On the other hand, if you have a feature that’s still changing regularly, or a test that requires human judgement (like verifying that an image looks correct), those might be better suited for manual testing.


Avinash is the Founder of Software Testing Sapiens. He is a blogger and Software Tester who has been helping people to get thier Jobs over a years Now.

Leave a Comment

Copy link