2017-12-27 553 views
0

我真的不知道如何在標題中更好地描述它,但在此解釋如下:Junit測試 - 每個測試的操作。如何最小化代碼?

我想爲Rest Api編寫測試。含義:我爲每次測試登錄服務器,運行我的電話並註銷。如果我可以在測試開始時以某種方式登錄到服務器,完成所有的調用(儘管如此,仍然在單獨測試中),然後註銷,那麼代碼會更少,效率更高。

有沒有一個聰明的方法來做到這一點?

感謝您的每一個回覆!

回答

0

你看過註釋標籤嗎? 即@Before和@After標籤

因此,例如:

@Before 
private void loginToServer() throws Exception { 
    /* Some code to do your login 
    and some code to do your repetitive tests 
} 

@Test 
private void testEvents() { 
//// Your test code 
} 

@After 
private void logoutServer() throws Exception { 
/// Code to logout of your server 
} 

這樣,它運行任何你在@Test類設置之前,你的代碼將永遠做標記之前。你的@ After課完成後會一直註銷。

0

您應該使用@BeforeClass和@AfterClass。