@BeforeTest失败但仍然运行@Test方法

时间:2013-08-13 04:52:08

标签: testng

我是新手,如果我问愚蠢的问题,请原谅。

当“@BeforeTest”失败时遇到问题,“@ Test”通过“@Factory”运行。理想情况下,当“@BeforeTest”失败时,应该跳过测试但是这里没有这样做,@ Test正在执行。

请详细说明这个问题或让我知道我错过了什么。

TestNG XML:

<test name="FactoryTest">
 <parameter name="callSSO" value="com.acc.Test1.callSSO"/>
 <groups>
  <run>
   <include name="@mode@"/>
  </run>
 </groups>
 <classes>
  <class name="com.acc.FactoryTest"/> 
 </classes>
</test>

TestNG课程:

public class ADFSTest {
   public ADFSTest(Object testData) {
    this.testData = (ArrayList) testData;
}

@Factory
public Object[] createTestInstances() {
    Object[] testInstances = null;
    try {
        finalList = getTestData();  **<<<<---- GETS TEST INPUT FROM FILE (no complex stuff)**
        testInstances = new Object[finalList.size()];
        for (int i = 0; i < finalList.size(); i++)
            testInstances[i] = new ADFSTest(finalList.get(i));
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return testInstances;
}

@BeforeTest(groups = { "group1" })
public void setUp() throws Exception {
    boolean isADFSConfigComplete = false;
    try {
        System.out.println("*** IN SETUP ***");
        isADFSConfigComplete = adfsConfig.setUp(); **<<<<------- THROWS SOME EXPECTION IF SOME THINGS GOES WRONG**
    }
} catch (Exception e1) {
    e1.printStackTrace();
}

@AfterTest(groups = { "group1" })
public void cleanUp() throws Exception {
    try {
        System.out.println("*** IN CLEANUP ***");
    }
} catch (Exception e) {
    e.printStackTrace();*
}


@Parameters({ "com.acc.Test1.callSSO" })
@Test(groups = { "group1" })
public void callSSO(String testName1) throws Exception {
    String testName = testData.get(0).toString();
    String initOp = testData.get(1).toString();
    String userState = testData.get(2).toString();
    String authScheme = testData.get(3).toString();
    String user = testData.get(4).toString();

    System.out.println("------------>> TestName is " + testName);
    System.out.println("------------>> UserState is " + userState);
    System.out.println("------------>> User is " + user);
    if(user.equals("Fred")
            assert true;
    else
        assert false;
}
}

正在使用TestNG jar: testng-5.10-jdk15.jar

2 个答案:

答案 0 :(得分:0)

因为异常被捕获,所以testNG的一切看起来都很好。只是重新抛出或不抓住,它应该按预期工作。

示例代码:

@BeforeTest(groups = { "group1" })
public void setUp() throws Exception {
    boolean isADFSConfigComplete = false;
    System.out.println("*** IN SETUP ***"); // you should use logs..
    isADFSConfigComplete = adfsConfig.setUp();
}

答案 1 :(得分:-1)

使用此代码:

@BeforeTest
    public void setUpClass() throws RuntimeException
    {
        try
        {
            loadRequiredFile();
        }
        catch (Exception e)
        {
            throw new RuntimeException("some error");
        }
    }

在这里,loadRequiredFile将抛出异常。但是不要在loadRequiredFile函数中捕获异常,而是在setUpClass方法中捕获它。