TestNG 练习

java文件

package selniumhomework;

import org.testng.annotations.Test;

public class Test1 {
  @Test(groups = {"regression"})
  public void method1() {
       System.out.println("Method 1 printed");
  }
  @Test(groups = {"regression"})
  public void method2() {
       System.out.println("Method 2 printed");
  }
  @Test(groups = {"smoke"})
  public void method3() {
       System.out.println("Method 3 printed");
  }
  @Test(groups = {"regression"})
  public void method4() {
       System.out.println("Method 4 printed");
  }
  @Test(groups = {"smoke"})
  public void method5() {
       System.out.println("Method 5 printed");
  }
}

 

case1:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
    <test name="Test">
        <groups>
            <run>
                <include name = "regression"/>
            </run>
        </groups>
        <classes>
            <class name="selniumhomework.Test1">
                <methods>
                 <include name = "method1"/>
                 <include name = "method2"/>
                 <include name = "method3"/>
                 <include name = "method4"/>
                 <include name = "method5"/>
                </methods>
            </class>
        </classes>
    </test> 
</suite>

结果: 当class 包含有methods时,无论method是否被标注为regression,都会被执行。所以5个方法都被打印。当method少了 include method1 时 则不会打印method1(即使它被标注为regression的groups)。所以结论是:

当 class 包含有 methods时,则运行 method include 的方法,不会运行 分组(groups,包括 inlcude和 exclude 都不会运行)里的方法。

----------------------------------------------------------------------------------------------------------------------

case2:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
    <test name="Test">
        <groups>
            <run>
                <include name = "regression"/>
            </run>
        </groups>
        <classes>
            <class name="selniumhomework.Test1" />
        </classes>
    </test> 
</suite>

结果: 该配置打印那些被标注为 groups=“regression” 的方法。

----------------------------------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------------------------------

多增加一个java文件:

package selniumhomework;

import org.testng.annotations.Test;

public class Test2 {
  @Test(groups = {"smoke"})
  public void method1() {
       System.out.println("F2: method1");
  }

  @Test(groups = {"regression"})
  public void method2() {
       System.out.println("F2: method2 ");
  }

  @Test(groups = {"regression", "smoke"})  //注意别写成:groups={"regression,smoke"}
  public void method3() {
       System.out.println("F2: method3");
  }

  @Test(groups = {"smoke"})
  public void method4() {
       System.out.println("F2: method4");
  }
}

-----------------------------------------------------------------------------------

case1

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
    <test name="Test">
        <groups>
            <run>
                <include name = "regression"/>
            </run>
        </groups>
        <classes>
            <class name="selniumhomework.Test1" />
            <class name="selniumhomework.Test2" />
        </classes>
    </test> 
</suite>

结果: 打印两个java文件中所有被标注为 groups={"regression"}的方法:

Method 1 printed

Method 2 printed

Method 4 printed

F2: method2

F2: method3

-----------------------------------------------------------------------------------

case2

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
    <test name="Test">
        <groups>
            <run>
                <exclude name = "regression"/>
            </run>
        </groups>
        <classes>
            <class name="selniumhomework.Test1" />
            <class name="selniumhomework.Test2" />
        </classes>
    </test> 
</suite>

结果: 打印两个java文件中所有被标注为 groups={"regression"}除外的其他方法 (exclude :不运行的方法):

Method 3 printed

Method 5 printed

F2: method1

F2: method4

-----------------------------------------------------------------------------------

case3

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
    <test name="Test">
        <groups>
            <run>
                <exclude name = "regression"/>
                <include name = "smoke"/>
            </run>
        </groups>
        <classes>
            <class name="selniumhomework.Test1" />
            <class name="selniumhomework.Test2" />
        </classes>
    </test> 
</suite>

 

结果:当某个方法既属于include的组,又属于exclude的组,那么exclude的组优先。所以该配置打印两个java文件中:

1). 排除所有被regression的分组;

2). 属于smoke的分组; 如果一个方法既属于regression的分组又属于smoke的分组,则不打印,如Test2的 method3就没有打印出来。

Method 3 printed

Method 5 printed

F2: method1

F2: method4

-------------------------------------------------------------------------------------

case4

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" parallel="false">
    <test name="Test">
        <groups>
          <define name = "chrome">
                <include name = "regression"/>
                <include name = "smoke"/>
            </define>
            <define name = "firefox">
                <include name = "regression"/>
            </define>
            <run>
                <include name = "chrome"/>
            </run>
        </groups>
        <classes>
            <class name="selniumhomework.Test1"/>
            <class name="selniumhomework.Test2"/>
        </classes>
    </test> 
</suite> 

这里自定义了两个组,分别针对chrome 和 firefox 的测试策略。chrome分组包含了regression和smoke两组分组;firefox分组则只包含了regression的分组。这样形成了分组包含分组,在设计分组的层次关系时,定义新组能够带来灵活性:可以在代码中使用粒度非常小的分组,然后在运行时刻将这些小分组合并成大分组。这里既可以运行chrome的分组或firefox的分组,还可以运行regression或smoke 的分组,灵活性很高。

Method 1 printed

Method 2 printed

Method 3 printed

Method 4 printed

Method 5 printed

F2: method1

F2: method2

F2: method3

F2: method4

 

-------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------

Note: 

1). 可以配置两个.xml文件来驱动case的运行,譬如test1.xml 运行 regression 的cases; test2.xml 运行 smoke 的cases.

    而Automatin team 也正是这样做的:在src/test/resources 下的一个文件CISute 中包含两个xml文件,coreRegressionTest.xml 

    和smokeTest.xml.


2). You can define groups at the class level and then add groups at the method level:

@Test(groups = { "smoke" })

public class All {

   @Test(groups = { "regression" )

    public void method1() { ... }

    public void method2() { ... }

}

In this class, method2() is part of the group "smoke", which is defined at the class level, while method1() belongs to both "smoke" and "regression".

 

 

posted @ 2014-06-11 17:51  tomweng  阅读(193)  评论(0编辑  收藏  举报