如何根据条件禁用 TestNG 测试
目前有没有一种方法可以根据条件禁用 TestNG 测试
我知道您当前可以在 TestNG 中禁用测试:
@Test(enabled=false, group={"blah"})
public void testCurrency(){
...
}
我想根据条件禁用相同的测试,但不知道如何操作。像这样的事情:
@Test(enabled={isUk() ? false : true), group={"blah"})
public void testCurrency(){
...
}
任何人都知道这是否可能。
Is there currently a way to disable TestNG test based on a condition
I know you can currently disable test as so in TestNG:
@Test(enabled=false, group={"blah"})
public void testCurrency(){
...
}
I will like to disable the same test based on a condition but dont know how. something Like this:
@Test(enabled={isUk() ? false : true), group={"blah"})
public void testCurrency(){
...
}
Anyone has a clue on whether this is possible or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在用
@BeforeMethod
注释的方法中抛出SkipException
对我来说不起作用,因为它跳过了我的测试套件的所有剩余测试,而不考虑SkipException< /code> 被抛出用于这些测试。
我没有彻底调查它,但我找到了另一种方法:在
@Test
注释上使用dependsOnMethods
属性:Throwing a
SkipException
in a method annotated with@BeforeMethod
did not work for me because it skipped all the remaining tests of my test suite with no regards if aSkipException
were thrown for those tests.I did not investigate it thoroughly but I found another way : using the
dependsOnMethods
attribute on the@Test
annotation:SkipException:当类中只有一个 @Test 方法时,它很有用。与数据驱动框架一样,我只有一个测试方法,需要根据某些条件执行或跳过。因此,我将检查条件的逻辑放入 @Test 方法中并获得所需的结果。
它帮助我获得了测试用例结果为“通过/失败”和特定“跳过”的范围报告。
SkipException: It's useful in case of we have only one @Test method in the class. Like for Data Driven Framework, I have only one Test method which need to either executed or skipped on the basis of some condition. Hence I've put the logic for checking the condition inside the @Test method and get desired result.
It helped me to get the Extent Report with test case result as Pass/Fail and particular Skip as well.
一个更简单的选择是在检查您的条件的方法上使用 @BeforeMethod 注释。如果您想跳过测试,只需抛出 SkipException 即可。像这样:
An easier option is to use the @BeforeMethod annotation on a method which checks your condition. If you want to skip the tests, then just throw a SkipException. Like this:
您有两个选择:
您的注释转换器将测试条件,然后覆盖 @Test 注释以在不满足条件时添加属性“enabled=false”。
You have two options:
Your annotation transformer would test the condition and then override the @Test annotation to add the attribute "enabled=false" if the condition is not satisfied.
据我所知,有两种方法可以让您控制 TestNG 中的“禁用”测试。
需要特别注意的区别是,SkipException 将中断所有后续测试,同时实现 IAnnotationTransformer 使用 Reflection 来根据您指定的条件取消单个测试。我将解释 SkipException 和 IAnnotationTransfomer。
SKIP 异常示例
IAnnotationTransformer 示例
有点复杂,但其背后的想法是一个称为反射的概念。
维基 - http://en.wikipedia.org/wiki/Reflection_(computer_programming)
首先实现 IAnnotation 接口,将其保存在 *.java 文件中。
然后在测试套件 java 文件中的 @BeforeClass 函数中执行以下操作
最后一步是确保在 build.xml 文件中添加侦听器。
我的最终看起来像这样,这只是 build.xml 中的一行:
There are two ways that I know of that allow you the control of "disabling" tests in TestNG.
The differentiation that is very important to note is that SkipException will break out off all subsequent tests while implmenting IAnnotationTransformer uses Reflection to disbale individual tests, based on a condition that you specify. I will explain both SkipException and IAnnotationTransfomer.
SKIP Exception example
IAnnotationTransformer example
A bit more complicated but the idea behind it is a concept known as Reflection.
Wiki - http://en.wikipedia.org/wiki/Reflection_(computer_programming)
First implement the IAnnotation interface, save this in a *.java file.
Then in you test suite java file do the following in the @BeforeClass function
One last step is to ensure that you add a listener in your build.xml file.
Mine ended up looking like this, this is just a single line from the build.xml:
我更喜欢这种基于注释的方式来根据环境设置禁用/跳过一些测试。易于维护,不需要任何特殊的编码技术。
检查详细信息:
https://www.lenar.io/skip-testng -tests-based-condition-using-iinvokedmethodlistener/
I prefer this annotation based way for disable/skip some tests based on environment settings. Easy to maintain and not requires any special coding technique.
Check for the details:
https://www.lenar.io/skip-testng-tests-based-condition-using-iinvokedmethodlistener/
第三选项也可以是假设
TestNG 的假设 - 当假设失败时,TestNG 将被指示忽略测试用例,因此不会执行它。
方法
您可以使用以下示例: 在 TestNG 中有条件地运行测试
A Third option also can be Assumption
Assumptions for TestNG - When a assumption fails, TestNG will be instructed to ignore the test case and will thus not execute it.
method
You can use this example: Conditionally Running Tests In TestNG