首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何跳过基于测试环境的Selenium TestNG测试

如何跳过基于测试环境的Selenium TestNG测试
EN

Stack Overflow用户
提问于 2018-06-15 07:51:41
回答 4查看 1.4K关注 0票数 1

我有一个登录到应用程序并检查一些东西的测试,但登录屏幕不会出现在所有测试环境中。登录验证测试在没有登录屏幕的情况下会失败,如何根据环境禁用/跳过登录测试来克服这个问题?

此外,我不太清楚的是,这些测试将与自动化CI/CD流水线集成。如果没有人工干预,测试如何知道测试是在什么环境下运行的。很抱歉,这个问题可能有一个显而易见的答案,但我是一个自动化和CI/CI的新手。我的测试是用Java编写的,非常感谢您的帮助

EN

回答 4

Stack Overflow用户

发布于 2018-08-17 00:20:06

TestNG中有“开箱即用”的批注转换器,您可以使用它来实现transform()方法,并定义测试上的@Test批注应该用ignore=false TestNG doc.标记的语句

但是,这种方法有一个缺点--必须在你的'test.xml‘中设置注释转换器才能使用它。当您不使用xml构建测试DOM时,您可以像这样定义自己的注释

代码语言:javascript
复制
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface EnvDependentIgnore {
String reason();
Environment env();
boolean skip();
}

此外,您还需要实现IInvokedMethodListener,以便在测试调用之前读取注释值。

代码语言:javascript
复制
public class IgnoreTestListener implements IInvokedMethodListener {

@Override
public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult testResult) {
    Method method = iInvokedMethod.getTestMethod().getConstructorOrMethod().getMethod();
    if (method.isAnnotationPresent(EnvDependentIgnore.class)) {
        if (//"your environment value"
                .equalsIgnoreCase(method.getAnnotation(EnvDependentIgnore.class).env().getValue()) &&
                method.getAnnotation(EnvDependentIgnore.class).skip()) {
            throw new SkipException(method.getAnnotation(EnvDependentIgnore.class).reason());
        }
    }
}

@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
    // no need to implement
}
}

这是一个有点混乱的方法,但它对我来说很好。

票数 1
EN

Stack Overflow用户

发布于 2018-06-15 11:03:15

我有一个非常精确的用例。事实上,有两个相似的用例。

基于缺陷status.

  • skipping测试的
  1. 跳过测试基于测试环境。

尽管它们听起来都一样,但实现方式略有不同。

为了跳过基于环境的测试,我不得不使用TestNG IMethodInterceptor

代码语言:javascript
复制
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> testMethods, ITestContext context) {    

    //removeIf method returns a boolean if any of the item is removed
    boolean isTestRemoved = testMethods.removeIf(somePredicate);

    //if any item is removed, then there is a chance that we might get execption due to missing dependencies.
    testMethods.stream()
               .map(IMethodInstance::getMethod)
               .forEach(method -> method.setIgnoreMissingDependencies(isTestRemoved));

    return testMethods;
}

为了跳过基于缺陷状态的测试,我必须使用IInvokedMethodListener

更多细节在这里。

http://www.testautomationguru.com/selenium-webdriver-how-to-skip-tests-based-on-test-environment/

http://www.testautomationguru.com/selenium-webdriver-how-to-automatically-skip-tests-based-on-open-defects/

票数 0
EN

Stack Overflow用户

发布于 2018-06-15 15:28:00

您可以使用TestNG组来标记测试用例。使用您的环境数据标记测试用例。因此,当您运行测试用例时,您将指定哪种环境(例如,登录环境),您希望使用TestNG -groups选项运行。它将只运行那些用指定标签/gropu标记的测试用例。

有关组的更多信息,请参阅https://testng.org/doc/documentation-main.html#test-groups

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50867421

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档