简体   繁体   English

如何只执行测试 class 和 TestNG 中的第一个测试

[英]How to only execute the first test in a Test class with TestNG

I'm putting together a navigation test suite to exercise a little bit of functionality on each page of my company's internal application with Java / Selenium / TestNG.我正在组装一个导航测试套件,以在我公司内部应用程序的每个页面上使用 Java / Selenium / TestNG 来练习一些功能。

Say I have a test class called ApplicationsIT in my NavigationTests directory.假设我在我的 NavigationTests 目录中有一个名为 ApplicationsIT 的测试 class。 Within this class, I have 10 @Tests annotations so every time I run the full NavigationTests suite all 10 Tests run.在这个 class 中,我有 10 个 @Tests 注释,所以每次我运行完整的 NavigationTests 套件时,所有 10 个测试都会运行。

But, for the sake of time, is there a way for me to only run the first test in each test class?但是,为了时间,有没有办法让我在每个测试 class 中只运行第一个测试?

I tried messing with the "(alwaysRun = false)" tags but it didn't seem to work how I'd expected.我试着弄乱了“(alwaysRun = false)”标签,但它似乎并没有像我预期的那样工作。

I know this is a poorly constructed question/post so I'd be happy to clarify anything I can.我知道这是一个结构不佳的问题/帖子,所以我很乐意尽我所能澄清一切。

There are at-least two ways of doing this dynamically.至少有两种动态执行此操作的方法。

  1. Using an implementation of org.testng.IAnnotationTransformer .使用org.testng.IAnnotationTransformer的实现。 Here you basically implement the interface org.testng.IAnnotationTransformer and within its transform() method, you inspect the incoming testMethod java.lang.reflect.Method object to check if its name matches with what you want to run or not run.在这里,您基本上实现了接口org.testng.IAnnotationTransformer并在其transform()方法中检查传入的testMethod java.lang.reflect.Method object 以检查其名称是否与您要运行或不运行的名称匹配。 You can specify the intent to run/not run a bunch of methods via a JVM argument.您可以通过 JVM 参数指定运行/不运行一系列方法的意图。 If there's a match, then you basically disable the test method via annotation.setEnabled(false) here annotation is of type org.testng.annotations.ITestAnnotation .如果匹配,那么您基本上通过annotation.setEnabled(false)禁用测试方法,此处annotation的类型为org.testng.annotations.ITestAnnotation You would need to wire in this listener via a service loader (or) using the <listeners> tag in your suite xml file.您需要通过服务加载器(或)使用套件 xml 文件中的<listeners>标记连接此侦听器。 For more information on listeners in general, you can take a look my blog post here有关一般听众的更多信息,您可以在此处查看我的博客文章
  2. The other option is to make use of a beanshell.另一种选择是使用 beanshell。 The below sample shows you how to use the beanshell.下面的示例向您展示了如何使用 beanshell。 You can read more about how to work with beanshells, from my blog post here .您可以从我的博客文章中阅读更多关于如何使用 beanshell 的信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="47230879_Suite" parallel="false" verbose="2">
    <test name="47230879_test" verbose="2">
        <method-selectors>
            <method-selector>
                <script language="beanshell">
                    <![CDATA[
                        try {
                            input = System.getProperty("methods","");
                            if (input.trim().isEmpty()) {
                                return true;
                            }
                            methods= input.split(",");
                            for (int i =0; i< methods.length; i++) {
                                if (method.getName().equals(methods[i])) {
                                    return true;
                                }
                            }
                            return false;
                        } catch( e ) {
                            print(e);
                        }
                    ]]>
                </script>
            </method-selector>
        </method-selectors>
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn47230879.TestclassSample"/>
        </classes>
    </test>
</suite>

The test class looks like below测试 class 如下所示

package com.rationaleemotions.stackoverflow.qn47230879;

import org.testng.annotations.Test;

public class TestclassSample {
    @Test
    public void a() {
        System.err.println("a()");
    }

    @Test
    public void b() {
        System.err.println("b()");
    }

    @Test
    public void c() {
        System.err.println("c()");
    }

    @Test
    public void d() {
        System.err.println("d()");
    }

    @Test
    public void e() {
        System.err.println("e()");
    }
}

If you run the suite without specifying any methods via JVM arguments, all methods get run, but if you specify a bunch of method names via JVM argument -Dmethods=a,b,c (for eg,) then only those methods get executed.如果您在未通过 JVM arguments 指定任何方法的情况下运行套件,所有方法都会运行,但如果您通过 JVM 参数-Dmethods=a,b,c (例如,)指定一堆方法名称,则只会执行这些方法。

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

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