Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TestNG & Selenium: Separate tests into "groups", run ordered inside each group

We use TestNG and Selenium WebDriver to test our web application.

Now our problem is that we often have several tests that need to run in a certain order, e.g.:

  • login to application
  • enter some data
  • edit the data
  • check that it's displayed correctly

Now obviously these tests need to run in that precise order.

At the same time, we have many other tests which are totally independent from the list of tests above.

So we'd like to be able to somehow put tests into "groups" (not necessarily groups in the TestNG sense), and then run them such that:

  • tests inside one "group" always run together and in the same order
  • but different test "groups" as a whole can run in any order

The second point is important, because we want to avoid dependencies between tests in different groups (so different test "groups" can be used and developed independently).

Is there a way to achieve this using TestNG?

Solutions we tried

  • At first we just put tests that belong together into one class, and used dependsOnMethods to make them run in the right order. This used to work in TestNG V5, but in V6 TestNG will sometimes interleave tests from different classes (while respecting the ordering imposed by dependsOnMethods). There does not seem to be a way to tell TestNG "Always run tests from one class together".
  • We considered writing a method interceptor. However, this has the disadvantage that running tests from inside an IDE becomes more difficult (because directly invoking a test on a class would not use the interceptor). Also, tests using dependsOnMethods cannot be ordered by the interceptor, so we'd have to stop using that. We'd probably have to create our own annotation to specify ordering, and we'd like to use standard TestNG features as far as possible.
  • The TestNG docs propose using preserve-order to order tests. That looks promising, but only works if you list every test method separately, which seems redundant and hard to maintain.

Is there a better way to achieve this?

I am also open for any other suggestions on how to handle tests that build on each other, without having to impose a total order on all tests.

PS

alanning's answer points out that we could simply keep all tests independent by doing the necessary setup inside each test. That is in principle a good idea (and some tests do this), however sometimes we need to test a complete workflow, with each step depending on all previous steps (as in my example). To do that with "independent" tests would mean running the same multi-step setup over and over, and that would make our already slow tests even slower. Instead of three tests doing:

  • Test 1: login to application
  • Test 2: enter some data
  • Test 3: edit the data

we would get

  • Test 1: login to application
  • Test 2: login to application, enter some data
  • Test 3: login to application, enter some data, edit the data etc.

In addition to needlessly increasing testing time, this also feels unnatural - it should be possible to model a workflow as a series of tests.

If there's no other way, this is probably how we'll do it, but we are looking for a better solution, without repeating the same setup calls.

like image 748
sleske Avatar asked Oct 31 '11 16:10

sleske


People also ask

What is TestNG used for?

TestNG makes automated tests more structured, readable, maintainable and user-friendly. It provides powerful features and reporting. Its high-end annotations like dataprovider, makes it easier to scale up, as you perform cross browser testing across multiple devices, browsers, and their versions.

What is TestNG in Selenium?

What is TestNG in Selenium? TestNG is an open-source testing framework where NG stands for 'Next Generation. ' It is architected to simplify a broad range of testing needs starting from unit testing to integrated system testing. Initially, both JUnit and TestNG were designed solely for unit testing.

What is TestNG and Maven?

TestNG is a testing framework used to create, run and generate a report of your tests. TestNG can also be invoked using Maven. To run the tests too, sometimes you may depend on the dev code or some other artifacts.

What is TestNG and its advantage?

TestNG is preferred by developers for its ability to write powerful test cases with the help of annotations, grouping, and parametrizing. It covers all classifications of test automation like Unit testing, Functional testing, End-to-End, and integration testing. It's available in the form of jar files.


1 Answers

You are mixing "functionality" and "test". Separating them will solve your problem.

For example, create a helper class/method that executes the steps to log in, then call that class/method in your Login test and all other tests that require the user to be logged in.

Your other tests do not actually need to rely on your Login "Test", just the login class/method.

If later back-end modifications introduce a bug in the login process, all of the tests which rely on the Login helper class/method will still fail as expected.

Update:

Turns out this already has a name, the Page Object pattern. Here is a page with Java examples of using this pattern:

http://code.google.com/p/selenium/wiki/PageObjects

like image 55
alanning Avatar answered Nov 15 '22 08:11

alanning