【问题标题】:testng - how to separate tests and supporting methods?testng - 如何分离测试和支持方法?
【发布时间】:2012-07-17 09:48:36
【问题描述】:

我正在试验 testng。我的目标是在几个类中拥有测试方法,并在一个单独的类中准备和总结一堆测试的“支持”方法。

另一个要求是,在测试套件中,必须为多个测试部分调用支持方法。例如。第一部分包含 testA 和 testB,第二部分包含 testC 和 testD。这将导致以下步骤:

support1, testA, testB, support2, support1, testC, testD, support2

我的第一个(部分)有效的方法是用@Test 注释所有方法,使用组并定义组之间的依赖关系,例如测试方法依赖于一组“setUp”,在上面的例子中是一组支持方法“support1”。

这种方法的问题是支持的方法算作测试,所以生成的报告显示错误的“真实”测试数量。

接下来的想法是使用@BeforeGroups@AfterGroups,将支持的方法放在一个组中,并使用组依赖。支持的方法不应再被视为测试。但我一开始就被困住了。 例如我试过

@BeforeGroups (groups = {"setUp"})

对于Support 类中的设置方法,以及

@Test(groups = { "TestA" }, dependsOnGroups = { "setUp" })

在“真正的”测试类中。这会导致以下(简化的)错误:

[testng] DependencyMap::Method "TestClass.testSomething()[...]" depends on nonexistent group "setUp"

为什么组“setUp”不存在?我是不是忽略了什么?

或者还有其他可行的方法吗?

感谢您的帮助!

编辑: 测试从 Ant 开始,我使用这样的 testng.xml:

<test name="TestA">
    <groups>
        <run>
            <include name="setUp" />
            <include name="TestA"/>
            <include name="tearDown"/>
        </run>
    </groups>
    <classes>
        <class name="seleniumtest.test.technical.Support"/>
        <class name="seleniumtest.test.business.TestClassA"/>
    </classes>
</test>
<test name="TestB">
    <groups>
        <run>
            <include name="setUp" />
            <include name="TestB"/>
            <include name="tearDown"/>
        </run>
    </groups>
    <classes>
        <class name="seleniumtest.test.technical.Support"/>
        <class name="seleniumtest.test.business.TestClassB"/>
    </classes>
</test>

【问题讨论】:

  • 你能分享更多代码吗?给出一个好的答案会很有帮助!顺便说一句,我通常将设置方法和实际测试作为同一个类的一部分!
  • 另外你能告诉你你是如何运行测试的吗?您是否使用 testng.xml 来运行测试?
  • 测试使用 testng.xml 文件运行。我在问题中添加了一个示例。
  • 在同一个类中拥有支持方法和测试方法并不是我想要的,因为支持方法的重用。

标签: java testng


【解决方案1】:

我发现了错误!

问题出在注释上

@Test(groups = { "TestA" }, dependsOnGroups = { "setUp" })

基本上你的错误信息是想说没有@Test 方法的组名为setUp!!针对您的问题,解决方案是修改测试方法的注释,如下所示

 @Test(groups = { "TestA" })

并在支持方法中修改注解

  @BeforeGroups (groups = {"TestA"})

我用这个设置运行了一个示例

public class TestSupport {

    @BeforeGroups(groups = { "sample","sample1" })
    public void beforeTest() {
        System.out.println("Before test");
    }

    @AfterGroups(groups = { "sample","sample1" })
    public void afterTest() {
        System.out.println("after test");
    }

}

我的测试类为

public class TestClassA {

    @Test(groups = { "sample" })
    public void superTestA() {
        System.out.println("This is the actual test");
    }

    @Test(groups = { "sample" })
    public void superTestB() {
        System.out.println("This is the another test under sample group");
    }

    @Test(groups = { "sample1" })
    public void superTest() {
        System.out.println("This is another test");
    }
}

和我的testng.xml如下图

<test name="sampletest" >
     <groups>
        <run>
            <include name="sample" />
            <include name="sample1" />

        </run>
    </groups>
    <classes>
        <class name="test.global.testng.TestClassA"/>
        <class name="test.global.testng.TestSupport"/>
    </classes>

  </test>

现在这是测试的运行方式:beforeGroups-&gt; superTestA/superTestB -&gt;afterGroupsbeforeGroups-&gt; superTest -&gt; afterGroups 并关闭

【讨论】:

  • 感谢您的努力。您的方法适用于我的小示例测试,因此您会获得支持。但实际上这不是我想要的,因为我必须将支持方法放在应该运行的每一组测试中。我想在 testng.xml 文件中配置它,而不是在 Java 代码中。
  • 检查我的更新答案!希望这能给你一个更好的方法
  • 抱歉,没有。它仍然有一个缺点,您必须将支持方法添加到 java 代码中的每个组。从 testng.xml 的内容来看,支持方法在什么时候执行也不是很明显。使用 BeforeTest 和 AfterTest 的方法更好地满足我的要求。
  • 如果你这么认为,你可以继续做同样的事情。没有冒犯,我觉得这是一个更好的方法!
【解决方案2】:

我想我已经想出了我想要的解决方案。

我需要在支持类中分别使用@BeforeTest@AfterTest 而不是@BeforeGroups@AfterGroups

@BeforeTest(groups = {"setUp"})
public void beforeTest() {[...]}

@AfterTest( groups = {"tearDown"})
public void afterTest() {[...]}

在测试类中:

@Test(groups = { "TestA" })
public void testSomething() {[...]}

dependsOnGroups 不见了,就像巴顿的做法一样。

与我的问题相比,testng.xml 没有变化。 IE。 可以在 testng.xml 文件中配置测试,无需更改 java 代码。

此外,这个解决方案还摆脱了 BeforeGroups 方法的另一个问题,至少像 Patton 所设想的那样(@Patton,我并不是要冒犯你)。 对于后者,使用多个测试组的测试不会按预期运行,因为 beforeTest() 方法将在任何组之前运行。例如。如果您有以下测试(testng.xml 的摘录):

    <groups>
        <run>
            <include name="TestA"/>
            <include name="TestB"/>
        </run>
    </groups>

...所产生的执行步骤是:
beforeTest()、TestA、beforeTest()、TestB、afterTest()。

使用带有 BeforeTest 的解决方案,您将进行以下测试:

    <groups>
        <run>
            <include name="setUp" />
            <include name="TestA"/>
            <include name="TestB"/>
            <include name="tearDown"/>
        </run>
    </groups>

...所产生的执行步骤是:
setUp = beforeTest(), TestA, TestB, tearDown = afterTest().

【讨论】:

    【解决方案3】:
    package com.test.MySample;
    
    import org.testng.annotations.*; 
    
    public class TestNGTest1 {    
    
        @BeforeTest   
        public void BeforeTest() {         
            System.out.println("@BeforeTest");
        }
    
        @BeforeClass    
        public void BeforeClass() {         
            System.out.println("@BeforeClass");
        }      
    
        @BeforeGroups  (groups = {"My group"}) 
        public void BeforeGroups() {       
            System.out.println("@BeforeGroups");
        }
    
        @BeforeGroups  (groups = {"My group1"}) 
        public void BeforeGroups1() {       
            System.out.println("@BeforeGroups1");
        }
    
        @AfterGroups  (groups = {"My group1"}) 
        public void AfterGroups1() {       
            System.out.println("@AfterGroups1");
        }
    
        @BeforeMethod    
        public void BeforeMethod() {         
            System.out.println("@BeforeMethod");     
        } 
    
        @Test(groups = {"My group"})     
        public void test1() {         
            System.out.println("test1");     
        } 
    
        @Test (groups = {"My group", "My group1"})    
        public void test2() {         
            System.out.println("test2");     
        }
    
        @AfterMethod    
        public void AfterMethod() {         
            System.out.println("@AfterMethod");     
        } 
    
        @AfterGroups  (groups = {"My group"}) 
        public void AfterGroups() {       
            System.out.println("@AfterGroups");
        }
    
        @AfterClass    
        public void AfterClass() {         
            System.out.println("@AfterClass");
        }  
    
        @AfterTest   
        public void AfterTest() {         
            System.out.println("@AfterTest");
        }
    } 
    

    【讨论】:

      猜你喜欢
      • 2013-10-23
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多