繁体   English   中英

有没有办法在不重复代码的情况下使用不同的配置从 2 个不同的类中运行相同的一组测试两次

[英]Is there any way to run same set of tests from 2 different classes twice with different configurations without duplicating code

public class JavaFile1 extends frameworkClass {

    @BeforeClass(alwaysRun=true)
    public void setup(){
    setMailServer1();
    //setMailServer2();
}

    @Test(priority = 0, groups = { "webdriver", "UI","helloTest"})
    public void test1(){}
    @Test(priority = 1, groups = { "webdriver", "UI","helloTest"})
    public void test2(){}
    @Test(priority = 2, groups = { "webdriver", "UI","helloTest"})
    public void test3(){}
    @Test(priority = 3, groups = { "webdriver", "UI","helloTest"})
    public void test4(){}
    @Test(priority = 4, groups = { "webdriver", "UI","helloTest"})
    public void test5(){}

}

public class JavaFile2 extends frameworkClass{
    @Test(priority = 0, groups = { "webdriver", "UI","helloTest1"})
    public void test1(){}
    @Test(priority = 1, groups = { "webdriver", "UI","helloTest1"})
    public void test2(){}
    @Test(priority = 2, groups = { "webdriver", "UI","helloTest1"})
    public void test3(){}
    @Test(priority = 3, groups = { "webdriver", "UI","helloTest1"})
    public void test4(){}
    @Test(priority = 4, groups = { "webdriver", "UI","helloTest1"})
    public void test5(){}

}

`我正在尝试按以下顺序从 2 个 java 文件运行 testng 测试。

configuration1 从 java file1 运行所有测试 从 java file2 运行所有测试 configuration2 从 java file1 运行相同的测试集 从 java file2 运行相同的测试集

在不重复代码的情况下,如何调整测试以按上述顺序运行? 注意:类 2 不能扩展类 1,因为它已经扩展了一些其他框架类

如果将测试类与@Factory结合,则可以执行此操作。

这是您的操作方法 [我使用的是 TestNG 7.0.0-beta7 ,这是发布此答案时最新发布的版本]

  1. 定义一个抽象出setupteardown的接口
  2. 构建 (1) 中定义的接口的一个或多个实现
  3. 使用@Factory实现来生成一个或多个测试类实例,其中每个实例都注入了 (2) 中定义的每个实现。

这是一个完整的示例。

定义设置和拆卸功能的接口

public interface ITestCaseConfiguration {
  void setup();

  void tearDown();
}

接口的实现

public class VariantOne implements ITestCaseConfiguration {

  @Override
  public void setup() {
    System.err.println("setup() from VariantOne");
  }

  @Override
  public void tearDown() {
    System.err.println("tearDown() from VariantOne");
  }
}
public class VariantOne implements ITestCaseConfiguration {

  @Override
  public void setup() {
    System.err.println("setup() from VariantOne");
  }

  @Override
  public void tearDown() {
    System.err.println("tearDown() from VariantOne");
  }
}

这是工厂的样子

import org.testng.annotations.Factory;

public class MyTestClassFactory {

  @Factory
  public static Object[] createTestClassInstances() {
    return new Object[] {
      new SampleTestClass(new VariantOne()), new SampleTestClass(new VariantTwo())
    };
  }
}

您可以在 Framework 类中使用 @BeforClass 方法,并对其进行参数化以针对配置 1 和 2 运行它。我猜想您的 setMailServer1() 和 setMailServer2() 方法执行相同的操作,除了使用不同的数据集。

代码将如下所示:

public class Frameworkclass
{
@Parameters({"data1","data2"})
@BeforeClass
public void setup(String data1, String data2)
{
    setMailServer();
    System.out.println(data1+"   "+data2);
}
}

测试类1:

public class JavaFile1 extends FrameworkClass 
{

    @Test(priority = 0, groups = { "webdriver", "UI","helloTest"})
    public void test1(){}
    @Test(priority = 1, groups = { "webdriver", "UI","helloTest"})
    public void test2(){}
    @Test(priority = 2, groups = { "webdriver", "UI","helloTest"})
    public void test3(){}
    @Test(priority = 3, groups = { "webdriver", "UI","helloTest"})
    public void test4(){}
    @Test(priority = 4, groups = { "webdriver", "UI","helloTest"})
    public void test5(){}
}

测试类2:

public class JavaFile2 extends FrameworkClass
{
    @Test(priority = 0, groups = { "webdriver", "UI","helloTest1"})
    public void test1(){}
    @Test(priority = 1, groups = { "webdriver", "UI","helloTest1"})
    public void test2(){}
    @Test(priority = 2, groups = { "webdriver", "UI","helloTest1"})
    public void test3(){}
    @Test(priority = 3, groups = { "webdriver", "UI","helloTest1"})
    public void test4(){}
    @Test(priority = 4, groups = { "webdriver", "UI","helloTest1"})
    public void test5(){}

}

套件文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="testsuit" parallel="false" verbose="2">
    <test name="test1" parallel="false" preserve-order="true">
        <classes>
            <class name="TestProject.test.JavaFile1"/>
            <parameter name="data1" value="value1"/>
            <parameter name="data2" value="value2"/>
        </classes>
     </test>
     <test name="test2" parallel="false" preserve-order="true">
        <classes>
            <class name="TestProject.test.JavaFile2"/>
            <parameter name="data1" value="value3"/>
            <parameter name="data2" value="value4"/>
        </classes> 
    </test>
</suite>

输出:

[RemoteTestNG] detected TestNG version 6.14.3
...
... TestNG 6.14.3 by Cédric Beust (cedric@beust.com)
...

value1   value2
PASSED: test1
PASSED: test2
PASSED: test3
PASSED: test4
PASSED: test5

===============================================
    test1
    Tests run: 5, Failures: 0, Skips: 0
===============================================

value3   value4
PASSED: test6
PASSED: test7
PASSED: test8
PASSED: test9
PASSED: test10

===============================================
    test2
    Tests run: 5, Failures: 0, Skips: 0
===============================================


===============================================
testsuit
Total tests run: 10, Failures: 0, Skips: 0
===============================================

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM