当前位置: 首页 > 知识库问答 >
问题:

如何忽略/跳过包含少量测试方法的完整testng类&BeforeClass和AfterClass方法

谷梁俊楚
2023-03-14

假设我有testng.xml,

<suite name="TestSuite" parallel="false">
  <test name="smoke" preserve-order="true" verbose="2">
    <groups>
        <run>
            <include name="smoke"/>
        </run>
    </groups>
    <classes>
        <class name="com.testClass1"/>
        <class name="com.testClass2"/>
    </classes>
  </test>
</suite>

这可能包含更多接近10-15的类,这是我的通用testng.xml,来自不同的testdata,我想要的是跳过com.testclass1类(对于特定情况),其余的test应该执行。

我尝试使用TestNG的IAnnotationTransformer监听器来实现我的类。

代码段是,

public class SkipTestClass implements IAnnotationTransformer{
        private SessionProfile profile=null;
        public void transform(ITestAnnotation annotation, Class testClass,
                  Constructor testConstructor, Method testMethod) {
            if (testMethod != null) {
                      Test test = testMethod.getDeclaringClass().getAnnotation(Test.class);
                      if (test != null && !test.enabled() && testMethod.getDeclaringClass().getClass().getName().equalsIgnoreCase("com.testClass1")) {
                        annotation.setEnabled(false);
                      }

                   }

        }
    }

并在测试类级别调用此侦听器,如

@Listeners(com.skiptestclass.class),

预期结果:我假设只有这个类com.testclass1&它的testmethods&beforeclass和afterclass方法应该跳过,而suite的其余部分应该执行。

实际结果:整个套房都被跳过了。

有什么需要帮忙的吗?

共有2个答案

魏宸
2023-03-14

您可以使用suite排除/包含测试用例。

@RunWith(Suite.class)
@Suite.SuiteClasses({ 
                      AuthenticationTest.class
                     /* USERRestServiceTest.class*/
 })

  public class JunitTestSuite
 {

 }

然后使用runner

@Category(IntegrationTest.class)
public class TestRunner {

@Test
public void testAll() {
    Result result = JUnitCore.runClasses(JunitTestSuite.class);
      for (Failure failure : result.getFailures()) {
         System.out.println(failure.toString());
      }
      if (result.wasSuccessful()) {
          System.out.println("All tests finished successfully...");
      }
}
}

更多详细信息-TestRunner文档

长孙阳嘉
2023-03-14

整个套房都被跳过了。

我想这是因为运行在某个地方失败了,因为您的监听器看起来很好。您可以设置一个更高的详细级别来检查正在发生的事情。

顺便说一句,IMethodInterceptor是一个更好的侦听器选择,因为它不依赖于类和/或测试中可能存在或不存在的注释。

public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) {
  List<IMethodInstance> result = new ArrayList<IMethodInstance>();
  for (IMethodInstance m : methods) {
    if (m.getDeclaringClass() != testClass1.class) {
      result.add(m);
    }
  }
  return result;
}

并且更喜欢在套件描述中添加此侦听器:

<suite name="TestSuite" parallel="false">
  <listeners>
    <listener class-name="...MyListener"/>
  </listeners>
  <test name="smoke" preserve-order="true" verbose="2">
    <groups>
        <run>
            <include name="smoke"/>
        </run>
    </groups>
    <classes>
        <class name="com.testClass1"/>
        <class name="com.testClass2"/>
    </classes>
  </test>
</suite>
 类似资料:
  • 这是另一堂课 和TESTNG XML-------- 当我在testNG中运行上述XML时,它没有执行@BeforeCLass和@AfterClass方法。它将输出显示为- [RemoteTestNG]在Open Test Test ONE中检测到TestNG版本7.2.0

  • 我有一个带有以下方法的TestNG类。 我想实现的是:Test2、Test3和Test 4依赖于Test1。所以只有当Test1通过时,我才需要继续。 Test5依赖于Test2、Test3和Test4。但是我可以跳过任何测试(即Test2、Test3或Test4),并且如果其他测试没有失败,我仍然希望继续执行Test5。 我怎样才能做到这一点。

  • 有时,我们编写的代码并没有准备就绪,并且测试用例要测试该方法/代码是否失败(或成功)。 在本示例中,注释有助于禁用此测试用例。 如果使用注释在测试方法上,则会绕过这个未准备好测试的测试用例。 在本教程中,我们将演示如何使用来忽略测试方法。 创建一个Maven项目,其结构如下所示 - pom.xml 依懒包配置 - 创建一个测试类:TestIgnore.java,其代码如下所示 - 运行上面代码,得

  • 本文向大家介绍如何在TestNG中执行时忽略特定的测试方法?,包括了如何在TestNG中执行时忽略特定的测试方法?的使用技巧和注意事项,需要的朋友参考一下 为了从TestNG中执行中忽略特定的测试方法,请使用启用的helper属性。必须将此属性设置为false才能从执行中忽略测试方法。 示例 Java类文件。 在执行过程中,该方法将被忽略。

  • 我正在试验测试。我的目标是在几个类中拥有测试方法,并在一个单独的类中为准备和总结一堆测试提供“支持”方法。 另一个要求是,在测试套件中,必须为多个测试部件调用支持方法。例如,第一部分包含testA和testB,第二部分包含testC和testD。这将导致以下步骤: 支持,测试,测试,支持,支持 我的第一个(部分)有效的方法是使用注释所有方法,使用组并定义组之间的依赖关系,例如,测试方法依赖于组“s