2

What I want to do is

  1. define dependencies on test methods (TestNG)
  2. define for each test method if the test fixture is shared (not reset) or re-created before the test method runs

See the following example:

  1. test1 would be run first
  2. if test1 succeeds then test2 is run and uses the same data that test1 inserted
  3. if test2 succeeds then test3 is run and uses the date from test1 and test2
  4. if test3 succeeds then test4 is run but test4 starts with a clean state, it will not share the same fixture

    public class SomeTestClass extends Insertable {
    @BeforeSuite
    public void background() {
        insert(0);
    }
    
    @Test()
    public void test1() {
    
        // when
        insert(1);
    
        // then
        assertThatIsContained(0);
        assertThatIsContained(1);
    }
    
    @Test(dependsOnMethods = {"test1"})
    @SharedFixture
    public void test2() {
        // when
        insert(2);
    
        // then
        assertThatIsContained(0);
        assertThatIsContained(1);
        assertThatIsContained(2);
    }
    
    @Test(dependsOnMethods = {"test2"})
    @SharedFixture
    public void test3() {
        // when
        insert(3);
    
        // then
        assertThatIsContained(0);
        assertThatIsContained(1);
        assertThatIsContained(2);
        assertThatIsContained(3);
    }
    
    @Test(dependsOnMethods = {"test2"})
    @FreshFixture
    public void test4() {
        // given
        insert(99);
    
        // when
        insert(4);
    
        // then
        assertThatIsNotContained(1);
        assertThatIsNotContained(2);
        assertThatIsNotContained(3);
        assertThatIsContained(0);
        assertThatIsContained(99);
        assertThatIsContained(4);
    }
    

    }

cheffe
  • 9,345
  • 2
  • 46
  • 57
leozilla
  • 1,296
  • 2
  • 19
  • 29

1 Answers1

1

You can split your tests in two groups (by specifying the groups property within the @Test annotation) and annotate your initialization method with @BeforeGroups to make it run only once per group.

To make your groups execute in a certain order, use dependsOnGroups in your @Test annotation.

A more straight-forward approach to achieve the same is to split your methods in two classes (say Test1 and Test2) and define a @BeforeClass-annotated method in your Insertable superclass which cleans up your repository (by invoking clear() or truncate() or whatever) before the test methods in each class will get executed.

Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • ok, that sounds good, so I make two groups (maybe in different classes) one that does setup the test preconditions only once in the @BeforeGroups and the other before each test method runs. I like that. – leozilla Dec 22 '14 at 14:39
  • I assumed you wanted those methods to be in the same class. I'll edit my answer to cover the two classes approach. – Costi Ciudatu Dec 23 '14 at 08:04
  • IMO both options are good (same class vs different classes) but I think I will go for having one class with different groups. Thank you for your edit. – leozilla Dec 23 '14 at 12:29