3

My reading of the TestNG docs suggests that if I have a test method marked like this:

  @BeforeSuite(alwaysRun = true)
  @Test
  public void MyTestMethod { ... }

then MyTestMethod would run before any other test defined anywhere, regardless of class, suite or group. But that does not seem to be the case.

Is there a way to make a test method run unconditionally before everything else? (And that if it fails no other tests will be run.)

Edit:

The test class:

class Suite_Setup
extends BaseTestSuite
{
  @BeforeSuite(alwaysRun = true)
  def setup() {
    System.out.println("Conducting test suite setup...")

    // Verify that the internal API is configured properly and that the API host is available...
    new Action(ApiHostname, new BasicCookieStore)
  }
}

Edit:

The answer:

We generate our own TestNG.xml files (automatically) and the @BeforeSuite method was not being included in it. Once it was included, @BeforeSuite had the expected effect.

However, it does appear that both @BeforeSuite (and presumably other @Before... and @After... annotations) can be mixed with @Test and rather than inhibiting the execution of the annotated method, they cause it to run more than once!

Also, I remiss in not indicating which version of TestNG I'm using. It's 6.2.

papigee
  • 6,401
  • 3
  • 29
  • 31
Randall Schulz
  • 26,420
  • 4
  • 61
  • 81
  • can you please clarify how did you include BeforeSuite annotation method in TestNG.xml ? I've requirement to run multiple classes in parallel and need to run setup method with BeforeSteuie annotation – vikramvi Aug 02 '17 at 13:19

5 Answers5

4

Try using groups either on class level or on method level.

In my case, I have a set of smoke tests that need to run before everything and if any of those fail, no other tests should run.

@Test(groups="smoke")
public class SmokeTests {
    public void test1 {
        //
    }
    ...
}

And all other classes with tests are:

@Test(dependsOnGroups = "smoke")
public class OtherTests {
    public void test1 {
        //
    }
    ...
}
Bato
  • 41
  • 2
  • This does not work. I have a configuration suite file with multiple Test nodes in it and TestNG cannot see a dependant group in another Test group. – djangofan Jun 30 '14 at 23:00
2

I think that the answer is

  1. to separate the configuration-methods from the test-methods,
  2. to use @BeforeSuite for the method to be executed befor all tests in the suite (for example to get an EntityManager)
  3. to use dependsOnMethods to decide in which order the tests shall be run

In the following example the testRemove will run after testInsert, testReadAll and testUpdate have run. testReadAll will run after testInsert has run. testUpdate will run after testInsert has run. Note that there is no dependency between testReadAll and testUpdate.

@Test
public void testInsert() 
{..}

@Test(dependsOnMethods={"testInsert"})
public void testUpdate() 
{..}

@Test(dependsOnMethods={"testInsert"})`
public void testReadAll()`
{..}

@Test(dependsOnMethods={"testInsert", "testUpdate", "testReadAll"})`
public void testRemove()`
{..}
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
0

@Test (alwaysRun=True)

makes the test always run irrespective of methods or groups it depends on and even it is failed

Shyamala
  • 69
  • 4
  • 6
  • 13
0

Is there a way to mark a test method so it will unconditionally be run before everything else?

Just try to assign to target test the lowest priority value.

@Test(priority = -2147483648)
public void myTest() {
    ...
}

You can read more about TestNG test priority over there.

And that if it fails no other tests will be run.

You need to make other tests depending on this test method by using one of the following options:

If you will use one of the dependency options you do not need to provide priority for your first method.

GreenTeaCake
  • 848
  • 10
  • 17
0

Remove @Test, a method cannot be both a configuration and a test method.

Cedric Beust
  • 15,480
  • 2
  • 55
  • 55