sysuser
sysuser

Reputation: 1100

Running a specific test in a testNG group

I see that I have a testNG group like

class TestXYZ {

    @Test(groups = "group1")
    public void test1() {
        Assert.assertTrue(true);
    }

    @Test(groups = "group1")
    public void test2() {
        Assert.assertTrue(false);
    }   
} 

Now if I want to run the whole group tests I can use

java -cp testjar.jar org.testng.TestNG -testjar testjar.jar -groups group1

(Assume that testjar.jar is a fat jar that has all testNG and test classes bundled together).

But I am not sure how to run test1 only from the group1 group. I tried out the -methods option, but realized that it's not for a method within a group, couldn't find any other solutions online.

Upvotes: 1

Views: 490

Answers (2)

Yu Zhang
Yu Zhang

Reputation: 2149

There is other way to run tests using a combination of regular expression and xml configuration file.

Say, you have four tests:

@test(groups={"test1"})
public void testMethodOne() {}

@test(groups={"test11"})
public void testMethodOneOne() {}

@test(groups={"test20"})
public void testMethodTwoZero() {}

@test(groups={"test21"})
public void testMethodTwoOne() {}

In the xml configuration file, you can write something like below:

<run>
    <include name="test1.*"/>
    <exclude name="test2.*"/>
</run>

this .* is basically a wild card, all tests that have names starting with test1 will be executed while all tests that have names starting with test2 will not be.

Another example will be, if you use this string .*test.*, you are basically searching for a test name that has this test string in it, a few examples are:

  • 123test
  • test234
  • ssstestsss

This should provide more scalability than my first suggestion.

Upvotes: 1

Yu Zhang
Yu Zhang

Reputation: 2149

Can you please try this approach by assigning your tests into different groups? Say, smokeTest and functionTest groups.

class TestXYZ {

    @Test(groups = "group1", "smokeTest")
    public void test1() {
        Assert.assertTrue(true);
    }

    @Test(groups = "group1", "functionTest")
    public void test2() {
        Assert.assertTrue(false);
    }   
} 

Then you can

  • run test1 only by running tests that belong to smokeTest.
  • run test2 only by running tests that belong to funtionTest.
  • run both test1 and test2 by running tests that belong to group1.

Upvotes: 1

Related Questions