2014-12-21 30 views
0

我是TESTNG框架的新手。在這個框架中沒有主要的方法。因此我的疑問是,我們可以使用new關鍵字在測試套件的任何位置創建對象嗎?或者是否有一個基本規則,即對象只能在某些地方被實例化(比如像@beforesuite,@test等testng註釋)?在下面的代碼中,當我使用new關鍵字(Ideal objIdeal = new Ideal();)時,它會失敗,但是當我將它放入@test註釋方法(login或logout)時,它會通過。因此,在使用Testng框架時,在類中實例化對象的基本拇指規則是什麼。TestNG框架和新關鍵字

package testing.ideal; 

public class Application { 

    public String strURL; 

    public Application() { 
     this.strURL = Ideal.strURL; 
     System.out.println("the url is--" + this.strURL); 
    } 

} 

package testing.ideal; 
     import org.testng.annotations.*; 

public class Ideal 
     extends Application { 
    public static String strURL = "XXX"; 
    Ideal objIdeal = new Ideal(); 

    @Test 
    public void login() { 
     System.out.println("This is an testng Method"); 
    } 

    @Test 
    public void logout() { 
     System.out.println("This is an testng Method"); 
    } 

} 

/* This is Testng XML*/ 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="Suite" parallel = "none"> 
    <test name="login" > 
     <classes> 
      <class name="testing.ideal.Ideal"> 
      </class> 
     </classes> 
    </test> 
</suite> 
+1

你需要用專用的亞軍類運行測試類的實例;你的環境很不清楚 – fge

回答

0

您可以使用註釋:

@BeforeSuite/@AfterSuite之前運行一些邏輯/後在特定套件中的所有測試運行。

@BeforeMethod/@AfterMethod在每個測試方法之前/之後運行某些邏輯。

@BeforeGroups\@AfterGroups運行一些一些前/屬於該組的第一/最後一個試驗方法後

@BeforeTest\@AfterTest到被封閉在<test>

任何測試方法前後運行一些邏輯/所以在你的測試類中,爲例如創建一個方法setUp創造objIdeal

@BeforeTest 
    public void setUp() 
    { 
    objIdeal = new Ideal(); 
    }