TestNG功能测试@Test订单序列

时间:2014-07-29 11:02:17

标签: java testng functional-testing end-to-end

我目前正在尝试使用TestNG编写一些自动化测试,这将验证网站上的某些用户操作,因此测试是端到端功能测试,而不是单元测试。

我创建了一个XML文件来保存测试数据(此XML文件的结构不会改变)。测试脚本将从测试文件中读取信息并将其传递给DataProvider,然后DataBuvider将提供@Tests。 问题是 - 数据提供者似乎在每个测试数据实例中抽取每个@Test方法,并且没有按顺序运行测试。

我附上了代码 - 如果我的方法是正确的,或者需要在代码中进行调整以使每个测试数据集可以运行,请有人建议。

测试数据文件

<TestData>
<test>
    <username>letmein</username>
    <password>ok!</password>
    <product>tesco</product>
    <ordertype>Market</ordertype>
    <quantity>1000</quantity>
    <side>Buy</side>
</test>
<test>
    <username>letmein</username>
    <password>ok!</password>
    <product>barc</product>
    <ordertype>RFQ</ordertype>
    <quantity>100</quantity>
    <side>Buy</side>
</test>

数据提供者读取此测试数据并将其传递给Object [] []

自动化测试脚本如下;

public class PlaceOrderTestScript {
InitiateTesting x;
static TestDataProvider y;
static WebDriver driver;
boolean loggedin = false;

@Parameters("browser")
@BeforeTest
public void beforeMethod(String browser) throws Exception {
    x = new InitiateTesting();  
    y = new TestDataProvider(); 
    driver = x.launch(browser);  //select driver, browser and launch website
}

@DataProvider
public static Object[][] dp(ITestContext context) throws Exception {        
    String testdatalocation = context.getCurrentXmlTest().getParameter("placeordertestdata"); //suite.xml holds details of the test data file location
    Object[][] data = y.Provider(testdatalocation);
    return data;
}

@Test (dataProvider = "dp")
public void SignIn(String username, String password, String product, String ordertype, String quantity, String side) throws Exception {
    System.out.println("Log into site");
    // only run this test once
    if (loggedin == false) {
        LogIn_Page.txtbx_UserName(driver).sendKeys(username);
        LogIn_Page.txtbx_Password(driver).sendKeys(password);
        LogIn_Page.btn_LogIn(driver).click();
        Thread.sleep(5000); //pause for x seconds and let site load for the first time
        loggedin = true;
    }
}

@Test (dependsOnMethods={"SignIn"}, dataProvider = "dp")
public void SearchForProduct(String username, String password, String product, String ordertype, String quantity, String side) {
    // search for <product>
}

@Test (dependsOnMethods={"Search"}, dataProvider = "dp")
public void PlaceOrder(String username, String password, String product, String ordertype, String quantity, String side) {
    //Place order
}

@Test (dependsOnMethods={"PlaceOrder"}, dataProvider = "dp")
public void ValidateQuantityAmount(String username, String password, String product, String ordertype, String quantity, String side) {
    //check results <quantity>
}   

@Test (dependsOnMethods={"PlaceOrder"}, dataProvider = "dp")
public void ValidateSide(String username, String password, String product, String ordertype, String quantity, String side) {
    //check results <side>
}

@Test (dependsOnMethods={"PlaceOrder"}, dataProvider = "dp")
public void ValidateTotal(String username, String password, String product, String ordertype, String quantity, String side) {
    //check results - some calcs here
}

@AfterTest
public void CancelOrder() throws Exception {
    //Cancel the Order 
}   

}

问题是当我运行它时 - TestNG将每个数据传递到每个@Test方法。我需要它通过方法序列传递每个实例..所以; ,SignIn,SearchForProduct,PlaceOrder,ValidateQuantityAmount,ValidateSide,ValidateTotal,CancelOrder 然后对下一个

实例重复此操作

我的方法有误吗?我知道每个测试都需要独立 - 但是在运行gend-to-end测试时怎么可能呢?

提前致谢

约翰

1 个答案:

答案 0 :(得分:0)

创建一套测试testng.xml

<suite name="Johnny's Tests">
  <test name="pass1">
    <parameter name="username" value="letmein"/>
    ...
    <class name="PlaceOrderTestScript">
      <methods>
        <include name="*"/>
      </methods>
    </class>
  </test>
  <test name="pass2">
    ...
  </test>
  ,,, repeat for each test
</suite>

然后将参数添加到测试中。