testng - 如何区分测试和支持方法?

问题描述:

我正在试验testng。我的目标是在几个类中提供测试方法,并在一个单独的类中准备和包装一系列测试的“支持”方法。testng - 如何区分测试和支持方法?

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

支持1,种皮,TESTB,支持2,支持1,TESTC,TESTD,支持2

我的第一种方法是(部分)合作是注释与@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”不存在?我忽略了什么?

或者还有另一种方法可行吗?

感谢您的帮助!

编辑: 的测试开始与蚂蚁和我使用的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> 
+0

你能分享更多的代码吗?给出一个很好的答案会很有帮助!顺便说一句,我通常有安装方法和真正的测试作为同一类的一部分! – Patton 2012-07-17 09:55:35

+0

你也可以告诉你如何运行测试?您是否使用testng.xml来运行测试? – Patton 2012-07-17 10:01:07

+0

测试使用testng.xml文件运行。我为这个问题添加了一个例子。 – prockel 2012-07-17 10:20:33

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

我需要使用什么是@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没有改变。即可以在testng.xml文件中配置测试,而不必更改java代码。

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

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

那么得到执行的步骤是:
beforeTest(),种皮,beforeTest(),TESTB,afterTest()。

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

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

那么得到执行的步骤是:
的setUp = beforeTest(),种皮,TESTB,拆解= afterTest( )。

我得到了故障!

问题是与注释

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

基本上你的错误信息是想说存在与组名称为setUp没有@Test方法!来到你的问题,解决的办法是修改注释对于测试方法如下

@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-> superTest -> afterGroups并且封闭

+0

感谢您的努力。你的方法适用于我的小例子测试,因此你会得到赞成。但实际上这不是我想要的,因为我必须将支持方法放在应该运行的每一组测试中。我想在testng.xml文件中配置它,而不是在Java代码中。 – prockel 2012-07-17 12:08:46

+1

检查我更新的答案!希望这给你一个更好的方式来做 – Patton 2012-07-18 06:37:43

+0

对不起,但它没有。它仍然有缺点,你必须将支持方法添加到Java代码中的每个组。从testng.xml的内容来看,支持方法执行的地方也不明显。 BeforeTest和AfterTest的方法更好地满足了我的要求。 – prockel 2012-07-18 07:21:20

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"); 
    } 
}