导入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,结果为


 

 5.编写基于TestNg的测试用例

Baidu.java

 1 package testcase;
 2 
 3 import org.openqa.selenium.By;
 4 import org.openqa.selenium.WebDriver;
 5 import org.openqa.selenium.chrome.ChromeDriver;
 6 import org.testng.annotations.AfterTest;
 7 import org.testng.annotations.BeforeTest;
 8 import org.testng.annotations.Test;
 9 
10 import java.util.concurrent.TimeUnit;
11 public class Baidu {
12     private WebDriver driver;
13     @BeforeTest
14     public void setUp(){
15         String chromepath = System.getProperty("user.dir")+"/src/main/java/drivers/chromedriver";
16         System.setProperty("webdriver.chrome.driver",chromepath);
17         driver = new ChromeDriver();
18         driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
19     }
20     @Test
21     public void test(){
22         driver.get("https://www.baidu.com");
23         driver.findElement(By.xpath("//input[@class='s_ipt' and @id='kw']")).sendKeys("selenium");
24         driver.findElement(By.xpath("//input[@id='su']")).click();
25         System.out.println(driver.findElement(By.xpath("//span[@class='nums_text']")).getText());
26 
27     }
28     @AfterTest
29     public void tearDown(){
30         driver.quit();
31     }
32 }

test.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.Baidu" />
        </classes>

    </test>
</suite>

测试结果:

 


  

posted on 2018-06-18 15:15  singleSpace  阅读(1204)  评论(0编辑  收藏  举报