【问题标题】:Why is throw new SkipException() skipping all my methods?为什么 throw new SkipException() 会跳过我的所有方法?
【发布时间】:2017-10-23 11:30:20
【问题描述】:

为什么 throw new SkipException() 会跳过所有类中的所有方法?

  1. 第一类有一个方法失败,因此该类中的其余方法将被跳过,skipcounter 设置为 0。
  2. 第二类应该执行所有方法并且所有方法都应该在第二类中通过,因为跳过计数器设置为 0 并且在第一类执行之后。

以下代码似乎跳过了所有方法,即使只有一个 assert.fail 包含在类 1 中?

测试基地:

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.ITestResult;
import org.testng.SkipException;

  public class TestBase {
        static int numb = 0;
      @BeforeMethod
      public void beforeMethod() {
          if(numb == 1) {
                throw new SkipException("Skipping this test");
          }  
      }

      @AfterMethod
      public void afterMethod(ITestResult testResult) {
            if (testResult.getStatus() == ITestResult.FAILURE) {
                numb = 1;
            }
      }

      @BeforeClass
      public void beforeClass() {
          TestBase.numb = 0;
      }

测试1:

import org.testng.annotations.Test;

public class Test1 extends TestBase {
    @Test(priority = 1)
    public void test1() throws Exception {
        Assert.fail();
    }

    @Test(priority = 2)
    public void test2() throws Exception {
        System.out.println("method2");
    }

    @Test(priority = 3)
    public void test3() throws Exception {
        System.out.println("method3");
    }
}

测试2:

import org.testng.annotations.Test;

public class Test2 extends TestBase {
    @Test(priority = 1)
    public void test1() throws Exception {
        System.out.println("method4");
    }

    @Test(priority = 2)
    public void test2() throws Exception {
        System.out.println("method5");
    }

    @Test(priority = 3)
    public void test3() throws Exception {
        System.out.println("method6");
    }
}

控制台输出:

..
... TestNG 6.9.10 by Cédric Beust (cedric@beust.com)
...

[TestNG] Running:
  C:\Users\Gianni.Bruno\Desktop\BuyAGiftFramework\BuyAGiftFramework\Test.xml

[TestRunner] Starting executor for test BuyAGift Automation Tests with time out:2147483647 milliseconds.
SKIPPED CONFIGURATION: @BeforeMethod beforeMethod
SKIPPED CONFIGURATION: @AfterMethod afterMethod
SKIPPED CONFIGURATION: @BeforeMethod beforeMethod
SKIPPED CONFIGURATION: @AfterMethod afterMethod
SKIPPED CONFIGURATION: @BeforeClass beforeClass
SKIPPED CONFIGURATION: @BeforeMethod beforeMethod
SKIPPED CONFIGURATION: @AfterMethod afterMethod
SKIPPED CONFIGURATION: @BeforeMethod beforeMethod
SKIPPED CONFIGURATION: @AfterMethod afterMethod
SKIPPED CONFIGURATION: @BeforeMethod beforeMethod
SKIPPED CONFIGURATION: @AfterMethod afterMethod
FAILED: test1
junit.framework.AssertionFailedError
    at junit.framework.Assert.fail(Assert.java:47)
    at junit.framework.Assert.fail(Assert.java:53)
    at BuyAGiftFramework.test.Test1.test1(Test1.java:9)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

SKIPPED: test2
SKIPPED: test3
SKIPPED: test1
SKIPPED: test2
SKIPPED: test3

===============================================
    BuyAGift Automation Tests
    Tests run: 6, Failures: 1, Skips: 5
    Configuration Failures: 0, Skips: 11
===============================================


===============================================
BuyAGift_Automation_Scripts_by_GBruno
Total tests run: 6, Failures: 1, Skips: 5
Configuration Failures: 0, Skips: 11
===============================================

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@366e2eef: 14 ms
[TestNG] Time taken by org.testng.reporters.XMLReporter@1936f0f5: 16 ms
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@3f2a3a5: 32 ms
[TestNG] Time taken by org.testng.reporters.jq.Main@614c5515: 94 ms
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 16 ms
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@6537cf78: 19 ms

测试执行 XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Automation_Scripts_by_GB" verbose="2" parallel="classes" thread-count="1">

    <test name="BuyAGift Automation Tests">
        <packages>
            <package name="BuyAGiftFramework.test.*"></package>
        </packages>
    </test>
</suite>

【问题讨论】:

  • 因为变量 'numb' 是静态的,并且它的值将在所有实例中保持不变...如果 Test2 中的测试在 Test1 中的测试之前运行,您将得到您正在寻找的东西...
  • @Grasshopper 感谢您的评论,即使我将变量设置为非静态,它仍然会跳过其他类中的测试,有什么想法吗?
  • 你想达到什么目的?如果至少有一个测试失败,您试图跳过所有剩余的测试,我说得对吗?
  • @Alexey R.correct,Test1 有一个失败,因此应该跳过该类中的其余方法,但是 Test2 没有失败,因此不应跳过任何方法。
  • 检查@BeforeClass 的导入。它应该使用来自 testNG 而不是来自 junit 的注释。 (如有异常,请分享完整代码以供进一步分析)

标签: java selenium selenium-webdriver testng


【解决方案1】:

configFailurePolicy 的默认行为是“跳过”。因为如果您在配置方法中遇到异常,则不需要运行您的测试。但是,如果您仍然想运行测试,请将此配置更改为“继续” 可以通过更新配置来实现

  &lt;suite name="SuiteName" parallel="tests" thread-count="1" verbose="10" configfailurepolicy="continue"&gt;

【讨论】:

  • 我在一些 xml 文件中描述了套件和仅列出套件文件的父套件。我的测试以同样的方式被跳过,因为如果configfailurepolicy="continue" 写在父级testng.xml 中,它显然会被忽略,所以它需要在最低级别的套件描述中。
猜你喜欢
  • 2017-06-23
  • 2012-02-27
  • 2017-02-01
  • 2018-02-27
  • 1970-01-01
  • 2015-04-10
  • 2014-03-01
相关资源
最近更新 更多