We use TestNG and Selenium WebDriver to test our web application.
Now our problem is that we often carry out several tests that must be performed in a certain order, for example:
- enter the application
- enter some data
- To change the data
- check the display is correct.
Now it’s obvious that these tests should be performed in this exact order.
At the same time, we have many other tests that are completely independent of the list of tests above.
So, we would like to somehow put the tests into “groups” (not necessarily groups in the sense of TestNG), and then run them so that:
- tests within the same group always work together and in the same order
- but different test "groups" in general can run in any order
The second point is important because we want to avoid dependencies between tests in different groups (therefore, different test "groups" can be used and developed independently).
Is there a way to achieve this with TestNG?
The solutions we tried
- First, we simply put the tests that belong together in the same class, and used
dependsOnMethods to get them to work in the correct order. This was used for testing in TestNG V5, but in V6 TestNG sometimes alternates tests from different classes (subject to the order imposed by dependsOnMethods ). There seems to be no way to tell TestNG "Always run tests from the same class together." - We examined the ability to record a method interceptor. . However, this has the disadvantage that running tests from within the IDE becomes more complicated (because the interceptor will not directly access the test in the class). Additionally, tests using
dependsOnMethods cannot be ordered by the interceptor, so we would have to stop using this. We will probably have to create our own annotation to indicate the order, and we would like to use the standard features of TestNG as much as possible. - TestNG docs suggest using a
preserve-order to order tests. This looks promising, but only works if you list each test method separately, which seems redundant and difficult to maintain.
Is there a better way to achieve this?
I am also open to any other suggestions on how to handle tests that are built on top of each other, without having to impose a general order on all tests.
PS
alanning replies that we could just keep all the tests independent by doing the necessary setup in each test. This is basically a good idea (and some tests do this), but sometimes we need to test the full workflow, and each step depends on all the previous steps (as in my example). To do this with "independent" tests will mean repeating the same multi-stage setup over and over again, and this will make our already slower tests even slower. Instead of three tests:
- Test 1: login to the application
- Test 2: enter some data
- Test 3: edit the data
we would get
- Test 1: login to the application
- Test 2: enter the application, enter some data
- Test 3: enter the application, enter some data, edit the data, etc.
In addition to an unnecessary increase in testing time, this also seems unnatural - you need to model the workflow as a series of tests.
If there is no other way, this is probably the way we do it, but we are looking for a better solution without repeating the same installation calls.