我有一个登录到应用程序并检查一些东西的测试,但登录屏幕不会出现在所有测试环境中。登录验证测试在没有登录屏幕的情况下会失败,如何根据环境禁用/跳过登录测试来克服这个问题?
此外,我不太清楚的是,这些测试将与自动化CI/CD流水线集成。如果没有人工干预,测试如何知道测试是在什么环境下运行的。很抱歉,这个问题可能有一个显而易见的答案,但我是一个自动化和CI/CI的新手。我的测试是用Java编写的,非常感谢您的帮助
发布于 2018-08-17 00:20:06
TestNG中有“开箱即用”的批注转换器,您可以使用它来实现transform()方法,并定义测试上的@Test批注应该用ignore=false TestNG doc.标记的语句
但是,这种方法有一个缺点--必须在你的'test.xml‘中设置注释转换器才能使用它。当您不使用xml构建测试DOM时,您可以像这样定义自己的注释
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface EnvDependentIgnore {
String reason();
Environment env();
boolean skip();
}此外,您还需要实现IInvokedMethodListener,以便在测试调用之前读取注释值。
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
}
}这是一个有点混乱的方法,但它对我来说很好。
发布于 2018-06-15 11:03:15
我有一个非常精确的用例。事实上,有两个相似的用例。
基于缺陷status.
尽管它们听起来都一样,但实现方式略有不同。
为了跳过基于环境的测试,我不得不使用TestNG IMethodInterceptor
@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/
发布于 2018-06-15 15:28:00
您可以使用TestNG组来标记测试用例。使用您的环境数据标记测试用例。因此,当您运行测试用例时,您将指定哪种环境(例如,登录环境),您希望使用TestNG -groups选项运行。它将只运行那些用指定标签/gropu标记的测试用例。
有关组的更多信息,请参阅https://testng.org/doc/documentation-main.html#test-groups
https://stackoverflow.com/questions/50867421
复制相似问题