TestNG, test groups, IDEA, and @BeforeMethod

Say you have

public class SomeTestNG {
@BeforeClass
public void setup() {}
@Test
public void test1() {}
@Test
public void test2() {}
}

You’ll get the following methods invoked:

  • setup()
  • test1()
  • test2()

Not surprising.

What happens, though, with this?

public class SomeTestNG {
@BeforeClass
public void setup() {}
@Test(groups = {"database"})
public void test1() {}
@Test
public void test2() {}
}

And you run the “database” group?

Does setup() get called?

It’d have to, right?

Well…

Not so much.

Not so much.

@BeforeClass has a groups() attribute as well, and it’s respected when you run group test suites. If you want it to run before all methods, you need to use the alwaysRun = true:

public class SomeTestNG {
@BeforeClass(alwaysRun = true)
public void setup() {}
@Test(groups = {"database"})
public void test1() {}
@Test
public void test2() {}
}

What’s a serious humdinger–when you run the test manually in IDEA, you’re running the test outside the scope of it’s group test suite, so the @BeforeClass runs as expected. Only in ant will the @Before… fail to get called.

Related posts:

  1. Simple spring integration testing
    Spring has had a really nice unit test framework available for a while now, but the documentation can be a bit daunting. Here’s a super-simple example of adding dependency injection......
  2. Hibernate Naming Strategies
    Using Hibernate annotations with the default naming strategy leaves you with camelCasedColumnNames in your database schema. Gavin King provided a good camelCase to underscore_separated naming strategy with org.hibernate.cfg.ImprovedNamingStrategy. The only......
  3. IDEA versus Eclipse
    IDEA has been giving me grief lately. Fed up with mysterious pauses, bugs in svn moves, and crashes even after performing cache-deletion voodoo in IDE, I’m back to trying Eclipse.......
  4. HOWTO: Configure an Amazon RDS instance to use UTF-8
    RDS has been working out pretty well for AdGrok — it’s a one-click MySQL 5.1 instance that seems pretty promising: Automatic replication and failover to another deployment zone (colo) Easy......

  • Pavan

    Hi,

    I have a senario where depending on the name of the test method invoked, i pick up a file within the @beforeMethod. That file is named after the test method and hence i need to find a way to get the name of the test being executed in the @beforeMethod method. (In junit setup() method, using getName() will fetch you the testcase name). Any hints?!

  • http://matthew.mceachen.us matthew

    I’d extract that file reading into a method, and call the method explicitly.

    You should also look at TestNG — it makes parametrized calls of a test method really easy.