您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
Junit4學(xué)習(xí)筆記
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2014/3/6 15:39:19 ] 推薦標(biāo)簽:Junit4 測試用例

1:JUnit4,包引用 import org.junit.*
2:JUnit4,測試類不必再擴展junit.framework.TestCase

3:Junit4,Setup和tearDown方法加注解:@Before和@After
          setUpBeforeClass和tearDownAfterClass方法加注解:@BeforeClass和@AfterClass
          測試用例前加注解:@Test;(測試方法也必須返回void并且是無參數(shù)的。)
            例如:
            @Test
            public  void photoupload_home(){}
          忽略測試用例注解:@Ignore
          (試運行機將報告被忽略的測試的個數(shù),以及運行的測試的數(shù)目和運行失敗的測試數(shù)目。注意

,@Ignore使用一個可選參數(shù)(一個String),如果你想記錄為什么一個測試被忽略的話。)

5:斷言(Assert):需加Assert前綴使用,例如:Assert.assertEquals()

6: @BeforeClass和@AfterClass,@Before和@After的區(qū)別
  僅有一次需要分配和釋放昂貴的資源,那么使用@BeforeClass和@AfterClass;
  使用@Before和@After初始化和清除系統(tǒng)。在測試代碼中,好少使用System.out.println();
  運行例子:
  @BeforeClass
    @Before
    @Test  //測試用例1
    @After
    @Before
    @Test  //測試用例2
    @After
    .
    .
  @AfterClass


例子:
package com.example.tests;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.*;


public class singletest {

    /** *//**
     * 包含了登錄的代碼,保證在一個測試內(nèi)部只執(zhí)行一次開啟瀏覽器并登錄操作
     * @throws Exception
     */
    @BeforeClass
    public  static void start() throws Exception {     
    } 
  
    /** *//**
     * 在該類包含的所有測試結(jié)束之后關(guān)閉瀏覽器
     * @throws Exception
     */
    @AfterClass
    public static  void stop() throws Exception {    
      
    } 
  
    @Test         //例子
    public  void Blogpush() throws Exception {  
    }
}

軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd