2016-09-26 16 views
0

我試圖從一個測試通過參數去另一個使用ITestContext:無法從一個測試通過參數去其他使用ITestContext返回NULL(TestNG的+ JAVA)

public class One { 
int waterfallId; 

@Test() 
public void testOne(ITestContext ctx) { 
    /*here waterfallId was initialized*/ 
    ctx.setAttribute("waterfallId", waterfallId); 
} 

@Test() 
public void testTwo(ItestContext ctx) { 
    ctx.getAttribute("waterfallId"); //returns null 
} 

} 

有什麼不對?任何其他方式來解決這個問題?

回答

0

cxt.getAttribute(「waterfallId」); //返回null 改變這 ctx.getAttribute( 「waterfallId」)

0

它ctx.getAttribute( 「waterfallId」)不cxt.getAttribute( 「waterfallId」)

請參考以下片段:

public class Test1{ 
    int waterfallId; 

    @Test() 
    public void testOne(ITestContext ctx) { 
     waterfallId=100; 
     ctx.setAttribute("waterfallId", waterfallId); 
    } 

    @Test() 
    public void testTwo(ITestContext ctx) { 
     ctx.getAttribute("waterfallId"); 
     System.out.println(ctx.getAttribute("waterfallId")); 
    } 
} 

輸出:100

1

你testOne方法應該執行第一和然後testTwo方法。如果以另一種方式執行它,則當您調用getAttribute方法時,您將獲得null值。

爲了確保testOne在testTwo之前被調用,在下面的testTwo方法中做了一些小改動。

@Test(dependsOnMethods = {"testOne"}) 
public void testTwo(ITestContext ctx) { 
    System.out.println(ctx.getAttribute("waterfallId")); //returns null 
} 
相關問題