The Quality Blog

Test automation recommendations – part 2

Written by Alisa Hrustić | Jun 27, 2024 2:41:18 PM

In our last article, we discussed how important it is to follow the most beneficial and crucial recommendations while developing or maintaining test automation solutions. The benefits are large and well worth the time and effort required to acquire them.

In this article, we will continue with recommendations. 

As previously said, there are numerous suggestions and practices that should be followed when developing AT solutions.


•    Follow AAA pattern

The AAA (Arrange, Act, Assert) principle is a common and effective pattern used in writing automated tests. It helps structure test code clearly and logically, making it easier to understand and maintain.  It is strongly recommended to use the AAA pattern wherever possible.  This strategy can be difficult to apply in more complex cases, but it should be used in general.  This pattern is also known as the Given-When-Then pattern, which is a concept within BDD.

Here's a breakdown of the AAA principle:

  1.  Arrange: Set up the context and initialize the objects and variables needed for the test. This includes preparing any prerequisites and configuring the environment.
  2. Act: Execute the action or the behavior that you want to test. This step focuses on invoking the method or functionality that you are testing.
  3. Assert: Verify that the outcome is as expected. This step checks the results of the action against the expected values, ensuring that the system behaves as intended.

Example:




•     Soft Assertions

Test validation frequently consists of many assertions that verify various parts of the AUT functionality. If you have more than one assertion in your test, you might consider utilizing soft assertions. For example, if you have four assertions and the test fails at the second assertion, the subsequent lines will not be validated. 

This is not an effective method, because the test may fail on the other assertion as well, which in your test is not reached since the test failed on assertion before this one. Instead, examine all validations and ensure that everything else works as expected. This can be accomplished by utilizing soft assertions. As a result, regardless of which assertion fails, all assertions will be assessed, and the execution will end with complete results.

Example: 


 
In this scenario, if the test fails on line 18, line 19 will still be evaluated. At the end of the execution, you will know what succeeded and what didn't.

•     Retry mechanism

Regardless of how stable the tests are, some of them will fail randomly. There could be a variety of causes for this that are unrelated to the application's operation or the stability of the automation framework, such as the current unavailability of a service, a server restart, etc. In such circumstances, we will lose time investigating the failure, and as a result, we are going to rerun the test only without any specific update. Implementing a retry mechanism is extremely helpful in this case. This technique will re-run the failed test automatically.

The retry technique should only be utilized in flaky and unstable tests. Otherwise, test execution time could be increased excessively without real requirements. Some testing frameworks, such as NUnit, provide this functionality, and using it is simple. RetryAttribute is used on a test method to specify that it should be rerun if it fails, up to a maximum number of times.

Example:


 
If your testing framework does not provide the retry mechanism, you can develop it yourself. The usage should be the same.

•    UI tests headless running

If there is no reason (visual testing or debugging), use headless mode to perform the tests, especially if they are executed on pipelines. 

Running tests in headless mode provides various advantages, such as:

Performance & Speed - Headless mode is often faster because it does not involve rendering the UI. This can result in quicker test execution, which is especially useful in a CI/CD pipeline. 

Resource utilization- Headless tests use fewer system resources (CPU and memory) because they do not require the rendering of a GUI. This allows you to run more tests concurrently, maximizing the use of your testing environment. 

Automation & Integration- Headless tests may easily be integrated into CI/CD pipelines and performed in situations without a display server (such as servers or Docker containers).

Stability - Headless mode might sometimes be more stable since it eliminates some GUI rendering issues (for example, timing issues caused by animations or pop-ups).

 
•    Test Reporting

Test reporting in test automation is essential for giving real-time feedback, assuring accuracy and consistency, helping debugging, optimizing resources, and enabling data-driven decision-making. Automated reports increase the overall efficiency and efficacy of the testing process, resulting in improved software quality and faster delivery. 

Test reporting in test automation is critical for various reasons that improve the overall efficacy and efficiency of the testing process.

•   Enable debugging tests on remote machines

Debugging tests is an important component of the automation engineer's daily work. It is useful for investigating test failures, analyzing results, experimenting with new data and test methods, etc.

We frequently see situations in which automation tests pass and behave stable on local machines but fail and become unstable when executed on remote machines (for example, as part of continuous testing).

In cases like this it is very challenging to analyze and fix the tests.

For this purpose, it is recommended to establish debugging on the remote machines from our solution started locally. With this strategy, the test will be run on the machine where it fails, but we will be able to correct it locally because we will have access to all the data in debug mode.

Missed the first part of the blog? Check it out. 👇

Interested in exploring our services? Our Test Automation method and tools enhance delivery speed while ensuring stability, adapting to your workflow or introducing new methods. We offer flexible subscription services that provide access to a range of Test Automation expertise, tailored to your specific needs for a fixed monthly fee. Check out our services, or reach out if you have any questions.