vibssingh posted: " HOME The general practices say that automated tests should be able to run independently and with no specific order as well as the result of the test should not depend on the results of previous tests. But there are situations where a specific order of" QA Automation Expert
The general practices say that automated tests should be able to run independently and with no specific order as well as the result of the test should not depend on the results of previous tests. But there are situations where a specific order of test execution can be justified, especially in integration or end to end tests. The test methods don't follow a specific order by default to execute the tests. The test cases need not necessarily execute in the order in which they have been written.
There are different ways or modes to set the order of execution for the test cases. This article shows how to control the JUnit 5 test execution order via the following MethodOrderer classes:
DisplayName - sorts test methods alphanumerically based on their display names
MethodName - sorts test methods alphanumerically based on their names and formal parameter lists
Alphanumeric - sorts test methods alphanumerically based on their names and formal parameter lists. This is deprecated since JUnit Version 5.7 onwards
OrderAnnotation - sorts test methods numerically based on values specified via the @Order annotation
Random - orders test methods pseudo-randomly and supports configuration of a custom seed
Custom Order - Custom ordering sequence can be implemented by the interface MethodOrderer and providing it as the argument to @TestMethodOrder.
It sorts test methods alphanumerically based on their display names. Test Method can be anything annotated with @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, or @TestTemplate.
@TestMethodOrder(MethodOrderer.DisplayName.class)
@TestMethodOrder is a type-level annotation that is used to configure a MethodOrderer for the test methods of the annotated test class or test interface.
MethodOrderer defines the API for ordering the test methods in a given test class.
Test Method - It is any method annotated with @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, or @TestTemplate.
DisplayName.class - MethodOrderer that sorts methods alphanumerically based on their names using String.compareTo(String). If two methods have the same name, String representations of their formal parameter lists will be used as a fallback for comparing the methods.
An example of sorting the tests based on their display names.
We can see that the test methods are sorted alphanumerically based on their display name starting from A to E. The output of the above program
2. MethodName
This annotation sorts methods alphanumerically based on their names using String.compareTo(String). If two methods have the same name, String representations of their formal parameter lists will be used as a fallback for comparing the methods.
This sorts test methods numerically based on values specified via the @Order annotation. Any methods that are assigned the same order value will be sorted arbitrarily adjacent to each other. When any method is not annotated with @Order, it will be assigned the default order value which will effectively cause them to appear at the end of the sorted list.
No comments:
Post a Comment