具有依赖关系的 TestNG 中的奇怪行为 - 没有引发错误
Weird behaviour in TestNG with dependencies - no error raised
更新: 我还在 GitHub project of TestNG here.
中打开了一个 bug
我在定义我的一项测试时犯了一个 愚蠢的 印刷错误。我不小心写了这样的注释:
@Test(dependsOnMethods = {"method1, method2"}, alwaysRun = true)
你看到错误了吗?我肯定没有(因为真正的方法名称也更长),这让我浪费了将近 2 个小时,因为 TestNG 对此提供了无帮助。
为什么它没有提供帮助:
由于这个错误,引发了没有错误,所有编译都很好,但是当我的套件是关于运行时它只是跳过所有测试,没有任何说明原因:
__PLAN___
Total tests run: 0, Failures: 0, Skips: 0
logs/xmls 中也没有地方表明可能有问题。
过了很久我才发现我打算写:
@Test(dependsOnMethods = {"method1", "method2"}, alwaysRun = true)
意味着测试实际上依赖于 2 种方法,除了一种显然不存在的长方法。
我现在正尝试在我们的框架中添加一个选项来提醒用户此类错误,这样它就不会发生在更多人身上(我们是一家大公司)。
问题: 我在哪里可以找到,也许在 TestNG 的代码中,code/documentation 可以让我 grab TestNG 失败 的事件并采取行动?那里 必须有 某个地方,TestNG 检查套件并决定不 运行 因为这个问题的测试,它在哪里?
错误被忽略的原因是因为您在 @Test
声明中指定了 alwaysRun=true
。来自 Test.java
:
/**
* If set to true, this test method will always be run even if it depends
* on a method that failed. This attribute will be ignored if this test
* doesn't depend on any method or group.
*/
public boolean alwaysRun() default false;
设置 alwaysRun=false
或将其从 @Test
声明中完全删除应该会给您带来预期的错误。
要回答您的问题,即 TestNG 在何处检查缺少的依赖项,在 MethodHelper.java#findDependedUponMethods(ITestNGMethod , ITestNGMethod[])
中。如您所见,将 alwaysRun
或 ignoreMissingDependencies
设置为 true
(它们都默认为 false
)将导致缺失的方法依赖项被静默忽略。我会避免设置 alwaysRun=true
除非你有特定的理由。
更新: 我还在 GitHub project of TestNG here.
中打开了一个 bug我在定义我的一项测试时犯了一个 愚蠢的 印刷错误。我不小心写了这样的注释:
@Test(dependsOnMethods = {"method1, method2"}, alwaysRun = true)
你看到错误了吗?我肯定没有(因为真正的方法名称也更长),这让我浪费了将近 2 个小时,因为 TestNG 对此提供了无帮助。
为什么它没有提供帮助: 由于这个错误,引发了没有错误,所有编译都很好,但是当我的套件是关于运行时它只是跳过所有测试,没有任何说明原因:
__PLAN___
Total tests run: 0, Failures: 0, Skips: 0
logs/xmls 中也没有地方表明可能有问题。
过了很久我才发现我打算写:
@Test(dependsOnMethods = {"method1", "method2"}, alwaysRun = true)
意味着测试实际上依赖于 2 种方法,除了一种显然不存在的长方法。
我现在正尝试在我们的框架中添加一个选项来提醒用户此类错误,这样它就不会发生在更多人身上(我们是一家大公司)。
问题: 我在哪里可以找到,也许在 TestNG 的代码中,code/documentation 可以让我 grab TestNG 失败 的事件并采取行动?那里 必须有 某个地方,TestNG 检查套件并决定不 运行 因为这个问题的测试,它在哪里?
错误被忽略的原因是因为您在 @Test
声明中指定了 alwaysRun=true
。来自 Test.java
:
/**
* If set to true, this test method will always be run even if it depends
* on a method that failed. This attribute will be ignored if this test
* doesn't depend on any method or group.
*/
public boolean alwaysRun() default false;
设置 alwaysRun=false
或将其从 @Test
声明中完全删除应该会给您带来预期的错误。
要回答您的问题,即 TestNG 在何处检查缺少的依赖项,在 MethodHelper.java#findDependedUponMethods(ITestNGMethod , ITestNGMethod[])
中。如您所见,将 alwaysRun
或 ignoreMissingDependencies
设置为 true
(它们都默认为 false
)将导致缺失的方法依赖项被静默忽略。我会避免设置 alwaysRun=true
除非你有特定的理由。