有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将所有参数传递给每个方法并同时运行每个方法

好的,我使用一个数据提供者将数据传递给一个@Test方法。这些数据来自外部数据库。在我的@Test方法看起来像这样之前:

@Test(enabled = true, dataProvider = DataProviderUtils.REGRESSION, dataProviderClass = DataProviderUtils.class) 
public void use_cases(String env, String browser, String title, String id, String orderType, String productType, String isFreeShipping, String isTaxState,
                      String billingCountry, String shippingCountry, String promoType, String isPromoFree,
                      String isPromoCode, String noOfPromoCodes, String email, Object[] customSteps){



    driverUtils.setUp(browser);
    String[] testType = title.split(" ");
    switch(testType[2].trim()){
        case "Standard_Order":
            useCases.standardOrder(id, env, productType, isFreeShipping, isTaxState, billingCountry, customSteps);
            break;

        case "Warranty_Order":
            useCases.warrantyOrder(id, env, productType, isFreeShipping, isTaxState, billingCountry, customSteps);
            break;

如果有一些测试,这是可行的,但当它达到100时,我会有一个很长的switch语句,它根本不可维护

所以我决定使用反射来处理它,这样我的代码就变成了:

@Test(enabled = true, dataProvider = DataProviderUtils.REGRESSION, dataProviderClass = DataProviderUtils.class)
public void test(String env, String browser, String title, String id, String orderType, String productType, String isFreeShipping, String isTaxState,
                      String billingCountry, String shippingCountry, String promoType, String isPromoFree,
                      String isPromoCode, String noOfPromoCodes, String email, Object[] customSteps){

    driverUtils.setUp(browser);
    String[] testType = title.split(" ");

    Class[] classes = {UseCases.class};
    for(Class clazz : classes){
        Method[] methods = clazz.getDeclaredMethods();
        for(Method method : methods){
            try {
                method = clazz.getMethod(method.getName(), new Class[]{String.class, String.class, String.class, String.class, String.class, String.class, Object[].class});
                Object invoke = method.invoke(new UseCases(), id, env, productType, isFreeShipping, isTaxState, billingCountry, customSteps);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

这是我的用例课:

public class UseCases {

OrderData orderData = new OrderData();

static String testRailURL;
static String localURL;


//New Standard Order Creation
public void standardOrder(String id, String env, String productType, String isFreeShipping, String taxState, String billingCountry, Object[] customSteps){

    testRailURL = TestRailUtil.getEnvironment(env);
    localURL = DriverUtils._targetURL;

    DriverFactory df = new DriverFactory(DriverUtils.getDriver());

    if(TestRailUtil.TEST_GET_DATA.equalsIgnoreCase("local")){
        df.navigateToURL(localURL);
    }else {
        df.navigateToURL(testRailURL);
    }
    df.loginPage.login();
    df.homePage.verifyAdminHomePageDisplay(id, customSteps);
    df.serviceHomePage.navigateToServiceHomePage();
    df.serviceHomePage.beginOrderCreation(id, productType, TestRailEnum.STANDARD_ORDER.getId(), billingCountry, customSteps);
    df.editCreatePage.verifyOrderEditPageDisplay(id, customSteps);
    df.editCreatePage.createOrder(id, productType, TestRailEnum.STANDARD_ORDER.getId(), false, isFreeShipping, taxState, customSteps);
    df.editCreatePage.review();
    df.orderConfirmChangesPage.verifyOrderConfirmChangesPageDisplay(id, customSteps);
    df.orderConfirmChangesPage.verifyConfirmChangesPageElementsDisplay(id, customSteps);
    //df.orderConfirmChangesPage.verifyTaxAndTotal(orderType, isFreeShipping);
    if (testRailURL.contains("qa") || DriverUtils._targetURL.contains("qa")){
        df.orderConfirmChangesPage.confirm();
    }
}

//New Warranty Order Creation
public void warrantyOrder(String id, String env, String productType, String isFreeShipping, String taxState, String billingCountry, Object[] customSteps){

    testRailURL = TestRailUtil.getEnvironment(env);
    localURL = DriverUtils._targetURL;

    DriverFactory df = new DriverFactory(DriverUtils.getDriver());

    if(TestRailUtil.TEST_GET_DATA.equalsIgnoreCase("local")){
        df.navigateToURL(localURL);
    }else {
        df.navigateToURL(testRailURL);
    }
    df.loginPage.login();
    df.homePage.verifyAdminHomePageDisplay(id, customSteps);
    df.serviceHomePage.navigateToServiceHomePage();
    df.serviceHomePage.beginOrderCreation(id, productType, TestRailEnum.Warranty_Order.getId(), billingCountry, customSteps);
    df.editCreatePage.verifyOrderEditPageDisplay(id, customSteps);
    df.editCreatePage.createOrder(id, productType, TestRailEnum.Warranty_Order.getId(), false, isFreeShipping, taxState, customSteps);
    df.editCreatePage.review();
    df.orderConfirmChangesPage.verifyOrderConfirmChangesPageDisplay(id, customSteps);
    df.orderConfirmChangesPage.verifyConfirmChangesPageElementsDisplay(id, customSteps);
    //df.orderConfirmChangesPage.verifyTaxAndTotal(orderType, isFreeShipping);
    if (testRailURL.contains("qa") || DriverUtils._targetURL.contains("qa")){
        df.orderConfirmChangesPage.confirm();
    }
}

}

问题是我在并行运行测试,当我使用反射时,它会调用第一个方法“n”次,然后再调用第二个方法。因此,如果我有两个单独的测试(标准订单测试和保修订单测试),那么数据将正确地从DataProvider传入,但由于循环的结构,这两个测试的数据将被传递到UseCases类中的第一个方法

我的问题是,有没有一种方法可以将信息传递给正确的方法并同时运行(并行),而不是循环通过每个方法,每个方法都会多次运行数据

谢谢


共 (1) 个答案

  1. # 1 楼答案

    我向您推荐典型的测试模式:

    1. “标准订单测试”和“保修订单测试”等场景是带有@Test注释的测试方法
    2. 数据提供程序提供测试参数,例如env。无需重新编译,因为3种环境的场景都很常见,只有运行时参数不同
    3. 您可以将场景分组到测试套件中,以获得几个固定的场景集

    我认为典型的模式简单而有力。如果这还不够,那就提问吧

    您的第一个解决方案是使用方法use_cases,它作为调度器工作,我觉得很奇怪,因为TestNG通常是调度器。在我看来,你的第二个解决方案,比如说,非常奇怪