导入testng管理测试用例

发表于:2018-11-09 10:15  作者:singleSpace   来源:51testing采编

字体: | 上一篇 | 下一篇 |我要投稿 | 推荐标签: TestNG 白盒测试工具

  1.在pom.xml中增加testng的依赖,以导入testNG
  2.在src-main-resources目录下新建xml文件,比如untitled.xml.
   
  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  <suite name="testSuite1">
  <test name="UITest">
  <classes>
  <class name="testcase.TestNG" />
  <class name="testcase.testNG2" />
  <class name="testcase.testNG1" />
  </classes>
  </test>
  </suite>
  3.BeforeTest AfterTest BeforeClass AfterClass的区别
  BeforeTest AfterTest
  case1:
  package testcase;
  import org.testng.annotations.AfterTest;
  import org.testng.annotations.BeforeTest;
  import org.testng.annotations.Test;
  public class testNG3 {
  @BeforeTest
  public void setup(){System.out.println("case3:set up");}
  @Test
  public void test1(){System.out.println("case3");}
  @AfterTest
  public void tearup(){System.out.println("case3:tear down");}
  }
  case2:
  package testcase;
  import org.testng.annotations.AfterTest;
  import org.testng.annotations.BeforeTest;
  import org.testng.annotations.Test;
  public class testNG4 {
  @BeforeTest
  public void setup(){System.out.println("case4:set up");}
  @Test
  public void test1(){System.out.println("case4");}
  @AfterTest
  public void tearup(){System.out.println("case4:tear down");}
  }
  xml:
  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  <suite name="testSuite1">
  <test name="UITest">
  <classes>
  <!--<class name="testcase.TestNG" />
  <class name="testcase.testNG2" />
  <class name="testcase.testNG1" />-->
  <class name="testcase.testNG3"/>
  <class name="testcase.testNG4"/>
  </classes>
  </test>
  </suite>
  执行结果:
   
  将BeforeTest替换为BeforeClass,AfterTest替换为AfterClass
  case1:
  package testcase;
  import org.testng.annotations.*;
  public class testNG3 {
  @BeforeClass
  public void setup(){System.out.println("case3:set up");}
  @Test
  public void test1(){System.out.println("case3");}
  @AfterClass
  public void tearup(){System.out.println("case3:tear down");}
  }
  case2:
  package testcase;
  import org.testng.annotations.*;
  public class testNG4 {
  @BeforeClass
  public void setup(){System.out.println("case4:set up");}
  @Test
  public void test1(){System.out.println("case4");}
  @AfterClass
  public void tearup(){System.out.println("case4:tear down");}
  }
  xml:
  <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
  <suite name="testSuite1">
  <test name="UITest">
  <classes>
  <!--<class name="testcase.TestNG" />
  <class name="testcase.testNG2" />
  <class name="testcase.testNG1" />-->
  <class name="testcase.testNG3"/>
  <class name="testcase.testNG4"/>
  </classes>
  </test>
  </suite>
  执行结果:
   
  4.使用TestNg增加断言
  package testcase;
  import org.testng.Assert;
  import org.testng.annotations.*;
  public class testNG3 {
  @BeforeClass
  public void setup(){System.out.println("case3:set up");}
  @Test
  public void test1(){
  System.out.println("case3");
  String actureMessage="abc";
  String expectMessage="efg";
  Assert.assertTrue(actureMessage.equalsIgnoreCase(expectMessage),"Expect message is "+expectMessage+".But acture message is "+actureMessage);
  }
  @AfterClass
  public void tearup(){System.out.println("case3:tear down");}
  }
   
  将expectMessage修改为abc,结果为
   
   上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。

【有奖活动】填问卷 送70G测试大牛精品资料(干货+视频)!

评 论

论坛新帖

顶部 底部


建议使用IE 6.0以上浏览器,800×600以上分辨率,法律顾问:上海瀛东律师事务所 张楠律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2018, 沪ICP备05003035号
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪公网安备 31010102002173号

51Testing官方微信

51Testing官方微博

扫一扫 测试知识全知道