Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running dependent and independent test methods in expected sequence #437

Open
riddhirch opened this issue Aug 29, 2013 · 4 comments
Open

Comments

@riddhirch
Copy link

I want to execute test methods in sequence. I have used classes in testng.xml with preserve-order set to true.

<test name="group-test" preserve-order="true" >
<classes>
    <class name="com.dependency.ClassA">
        <methods>
            <include name="create"/>
            <include name="enter"/>
            <include name="delete"/>
        </methods>
    </class>
</classes>
and my Test class is public class ClassA {
@Test()
public void Create() throws Exception
  {
 System.out.println("in method create");
  }   

@Test(dependsOnMethods= "Create")
public void Enter() throws Exception
{
System.out.println("in method Enter");
 }

@Test()
public void delete() throws Exception
{
System.out.println("in method delete");
}

After executing the test my output is
in method create,
in method delete,
in method enter

But what I want is to first execute "create" then "enter" then "delete" method. Here delete is an independent test method.

I cannot use dependsOnMethods for delete method because if enter method fails then delete method will skip but i want that delete method has to be run after enter method no matter whether it is fail or pass

@JnRouvignac
Copy link
Contributor

It is not clear why you want them to be run in sequence.
If delete() is independent, why does it matter if delete() is not run last?

@fpavageau
Copy link

You can use dependsOnMethods = "enter" on delete() if you also add alwaysRun = true.

@riddhirch
Copy link
Author

There are some scenarios where i need them to run in sequence.
For e.g.

public class ClassA {

@Test()
public void createOrderSet() throws Exception
 {
        System.out.println("in method createOrderSet");
 }   

@Test(dependsOnMethods= "createOrderSet")
public void enterProcedureInOrderSet() throws Exception
 {
  System.out.println("in method enterProcedureInOrderSet");
  }

@Test(dependsOnMethods= "enterProcedureInOrderSet")
public void deleteProcedureInOrderSet() throws Exception
 {
  System.out.println("in method deleteProcedureInOrderSet");
 }

@Test(dependsOnMethods= "createOrderSet")
public void deleteOrderSet() throws Exception
 {
   System.out.println("in method deleteOrderSet");
 }

After executing the test my output is :
in method createOrderSet
in method enterProcedureInOrderSet
in method deleteOrderSet
in method deleteProcedureInOrderSet

first i want to execute createOrderSet(), then enterProcedureInOrderSet(), then deleteProcedureInOrderSet() and finally deleteOrderSet()

@fpavageau
Copy link

If your methods have an order, they are not really independent.

public class MyTest {
    @Test
    public void one() {
        System.out.println("one");
    }

    @Test(dependsOnMethods = "one")
    public void two() {
        System.out.println("two");
    }

    @Test(dependsOnMethods = "two")
    public void three() {
        System.out.println("three");
    }

    @Test(dependsOnMethods = "three", alwaysRun = true)
    public void four() {
        System.out.println("four");
    }
}

produces:

one
two
three
four

Change three() to

    @Test(dependsOnMethods = "two")
    public void three() {
        System.out.println("three");
        throw new IllegalStateException();
    }

and you still get

one
two
three
four

with a failed test.

Or change two() to

    @Test(dependsOnMethods = "one")
    public void two() {
        System.out.println("two");
        throw new IllegalStateException();
    }

and you get

one
two
four

with one test failed and one test skipped.

You can also have a diamond configuration:

public class MyTest {
    @Test
    public void one() {
        System.out.println("one");
    }

    @Test(dependsOnMethods = "one")
    public void two() {
        System.out.println("two");
    }

    @Test(dependsOnMethods = "one")
    public void three() {
        System.out.println("three");
    }

    @Test(dependsOnMethods = { "two", "three" }, alwaysRun = true)
    public void four() {
        System.out.println("four");
    }
}

and you get either

one
two
three
four

or

one
three
two
four

but four() always comes last.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants