title: TestNG & Selenium: Separate tests into "groups", run ordered inside each group
body: <p>We use TestNG and Selenium WebDriver to test our web application.</p>
  
  <p>Now our problem is that we often have several tests that need to run in a certain order, e.g.:</p>
  
  <ul>
  <li>login to application</li>
  <li>enter some data</li>
  <li>edit the data</li>
  <li>check that it's displayed correctly</li>
  </ul>
  
  <p>Now obviously these tests need to run in that precise order.</p>
  
  <p>At the same time, we have many other tests which are totally independent from the list of tests above.</p>
  
  <p>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:</p>
  
  <ul>
  <li>tests inside one "group" always run together and in the same order</li>
  <li>but different test "groups" as a whole can run in any order</li>
  </ul>
  
  <p>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).</p>
  
  <p>Is there a way to achieve this using TestNG?</p>
  
  <p><strong>Solutions we tried</strong></p>
  
  <ul>
  <li>At first we just put tests that belong together into one class, and used <code>dependsOnMethods</code> 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 <code>dependsOnMethods</code>). There does not seem to be a way to tell TestNG "Always run tests from one class together".</li>
  <li>We considered writing a <a href="http://testng.org/doc/documentation-main.html#methodinterceptors" rel="noreferrer">method interceptor</a>. 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 <code>dependsOnMethods</code> 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.</li>
  <li>The <a href="http://testng.org/doc/selenium.html" rel="noreferrer">TestNG docs</a> propose using <code>preserve-order</code> to order tests. That looks promising, but only works if you list every test method separately, which seems redundant and hard to maintain.</li>
  </ul>
  
  <p>Is there a better way to achieve this?</p>
  
  <p>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 <em>all</em> tests.</p>
  
  <p><strong>PS</strong></p>
  
  <p>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:</p>
  
  <ul>
  <li>Test 1: login to application</li>
  <li>Test 2: enter some data</li>
  <li>Test 3: edit the data</li>
  </ul>
  
  <p>we would get</p>
  
  <ul>
  <li>Test 1: login to application</li>
  <li>Test 2: login to application, enter some data</li>
  <li>Test 3: login to application, enter some data, edit the data
  etc.</li>
  </ul>
  
  <p>In addition to needlessly increasing testing time, this also feels unnatural - it should be possible to model a workflow as a series of tests.</p>
  
  <p>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.</p>