0

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

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

请阐明这个问题或让我知道我缺少什么。

测试NG 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>

测试NG类:

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

4

2 回答 2

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();
}
于 2013-08-13T05:37:51.203 回答
-1

使用此代码:

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

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

于 2016-09-02T08:40:43.810 回答