testng如何从Factory动态设置组?

时间:2015-12-21 22:30:30

标签: selenium-webdriver testng factory

在我设置测试类之前,如下面的代码: 1. Factory和测试Dataprovider都使用excel作为数据提供者。 2.在Factory dataprovider表中,它有一个url列表 3.每次,它都会找到工厂dataprovider表中的一个url,并在每个测试方法中运行测试。

public class Test {
    WebDriver driver;
    private String hostName;
    private String url;


    @Factory(dataProvider = "xxxx global variables", dataProviderClass = xxxx.class)
    public GetVariables(String hostName, String url) {
        this.hostName = hostName;
        this.url = url;

    }

    @BeforeMethod
    @Parameters("browser")
    public void start(String browser) throws Exception {

        driver = new FirefoxDriver();
        driver.get(url);
        Thread.sleep(1000);

    }

    @Test(priority = 10, dataProvider = "dataprovider Test A", dataProviderClass = xxx.class)
    public void TestA(Variable1,
            Variable2,Variable3) throws Exception {
        some test here...

    }

    @Test(priority = 20, dataProvider = "dataprovider Test B", dataProviderClass = xxx.class)
    public void TestB(Variable1,
            Variable2,Variable3)
            throws Exception {
        some test here...
    }

    @AfterMethod
    public void tearDown() {
        driver.quit();
    }

现在我想为不同的网址为每个测试动态分配不同的组。我想在@Factory数据提供者中添加一个变量'flag':

@Factory(dataProvider = "xxxx global variables", dataProviderClass = xxxx.class)
public GetVariables(String hostName, String url, String flag) {
    this.hostName = hostName;
    this.url = url;
    this.flag = flag;
}

That when flag.equals("A"), it will only run test cases in test groups={"A"}.
When flag.equals("B"), it will only run test cases in test groups ={"B"},
When flag.equals("A,B"), it will only run test cases in test groups ={"A","B"}

我有什么方法可以做到吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

TestNG小组为您的测试划分方式提供了灵活性"但它不适用于条件测试集。为此,您只需使用普通的旧Java。

您可以使用继承或组合(我推荐后者,请参阅项目16:赞成继承来自Effective Java)。

无论哪种方式,一般的想法是相同的:使用Factory创建测试类实例,动态创建适当的类类型,并使用您想要运行的相应测试注释和/或方法。

示例:

  1. 继承

    import org.testng.annotations.Factory;
    import org.testng.annotations.Test;
    
    public class DemoTest {
        @Factory
        public static Object[] createTests() {
            return new Object[]{
                    new FlavorATest(),
                    new FlavorBTest(),
                    new FlavorABTest()
            };
        }
    
        /**
         * Base test class with code for both A-tests and B-tests.
         *
         * Note that none of these test methods are annotated as tests so that
         * subclasses may pick which ones to annotate.
         */
        public static abstract class BaseTest {
            protected void testA() {
                // test something specific to flavor A
            }
    
            protected void testB() {
                // test something specific to flavor B
            }
        }
    
        // extend base but only annotate A-tests
        public static class FlavorATest extends BaseTest {
            @Test
            @Override
            public void testA() {
                super.testA();
            }
        }
    
        // extend base but only annotate B-tests
        public static class FlavorBTest extends BaseTest {
            @Test
            @Override
            public void testB() {
                super.testB();
            }
        }
    
        // extend base and annotate both A-tests and B-tests
        public static class FlavorABTest extends BaseTest {
            @Test
            @Override
            public void testA() {
                super.testA();
            }
    
            @Test
            @Override
            public void testB() {
                super.testB();
            }
        }
    }
    
  2. 组合物

    import org.testng.annotations.Factory;
    import org.testng.annotations.Test;
    
    public class DemoTest {
        @Factory
        public static Object[] createTests() {
            return new Object[]{
                    new FlavorATest(),
                    new FlavorBTest(),
                    new FlavorABTest()
            };
        }
    
        private static void testA() {
            // test something specific to flavor A
        }
    
        private static void testB() {
            // test something specific to flavor B
        }
    
        // only create A-test methods and delegate to shared code above
        public static class FlavorATest {
            @Test
            public void testA() {
                DemoTest.testA();
            }
        }
    
        // only create B-test methods and delegate to shared code above
        public static class FlavorBTest {
            @Test
            public void testB() {
                DemoTest.testB();
            }
        }
    
        // create A-test and B-test methods and delegate to shared code above
        public static class FlavorABTest {
            @Test
            public void testA() {
                DemoTest.testA();
            }
    
            @Test
            public void testB() {
                DemoTest.testB();
            }
        }
    }
    
  3. 您的工厂方法不会像您一样简单,需要使用您的"标志"从您的测试数据中关闭并创建相应测试类的实例。