testng入门_单元测试

1.定义TestNG 的配置文件

    <test name="exampletest1">
        <classes>

                         <!--1.只执行com.testng.TestGroup的方法testPrintMessage,不执行testPrintMessage1 -->
            <class name="com.testng.TestGroup">
                <methods>
                    <include name="testPrintMessage" />
                  <exclude name="testPrintMessage1"/>
                </methods>
            </class>
            <!--2.按顺序执行TankLearn2.Learn.TestNGLearn1 类的三个方法-->
       <class name="TankLearn2.Learn.TestNGLearn1"> <methods> <include name="TestNgLearn3" /> <include name="TestNgLearn1" /> <include name="TestNgLearn2" /> </methods> </class>

        </classes>        
    </test>

2.分组执行

@Test(groups = { "BeiJing"})
public void getBeiJing_Succ() throws IOException{
     exp_city="北京";
     cityCode="101010100";
     resultCheck(cityCode, exp_city);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite1">
    <test name="test1">
        <groups>
            <run> 定义执行的组名及顺序
                <include name="BeiJing" />
......
            </run>
        </groups>
        <classes>  定义class
            <class name="findyou.testcase.test1" />  
        </classes>  
    </test>
</suite>

3.组中组

 <test name="test1">
      <groups>
         <define name="all">   定义执行的组和不执行的组
	    <include name="functest"/>
	    <include name="checkintest"/>
        <exclude name="functest"/>
	 </define>            
	 <run>                 执行组
	    <include name="all"/>
	 </run>
      </groups>
      <classes>
	  <class name="GroupTestExample" />
       </classes>
   </test>

4.自定义参数1

    @Test
  @Parameters({ "cityCode_str", "exp_city_str" })
  public void resultCheckp(String cityCode_str, String exp_city_str) throws IOException{    
    resultCheck( cityCode_str,  exp_city_str);
  }

  <parameter name="country" value="中国" />  公用参数,适用所有

 <test name="resultCheckp2">
     <parameter name="cityCode_str" value="101010100" />  适用参数
     <parameter name="exp_city_str" value="北京" />
     <classes>
        <class name="findyou.testcase.test1">
           <methods>
              <include name="resultCheckp" />
           </methods>
         </class>
      </classes>
    </test>   

 

5.依赖测试

   public class DependsTest {
    @Test
    public void setupEnv(){
        System.out.println("this is setup Env");
    }
    @Test(dependsOnMethods = {"setupEnv"})
    public void testMessage(){
        System.out.println("this is test message");
    }
  }

6.生命周期

待续

posted on 2016-04-16 20:54  dengzy  阅读(296)  评论(0编辑  收藏  举报