您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
selenium 使用junit4
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2014/3/18 13:49:45 ] 推薦標簽:Selenium Junit 單元測試

Selenium的開發(fā)提供的SeleneseTestCase是Junit3風格的,放在JUnit4底下跑,JUnit4的Annotation功能用不起來了。Selenium要啟動瀏覽器,如果用不上@BeforeClass的話,每次啟動都初始化一下Selenium,開個IE或者Firefox,這個測試的效率可吃不消(也有比較麻煩的Workaround,但總覺得不是很好)。而甩開SeleneseTestCase的話,又舍不得那個在測試沒有通過的時候自動截屏的功能。網(wǎng)絡(luò)上有人已經(jīng)有解決方法,我整理如下:、

編寫MyRunListener,繼承RunListener

importorg.junit.runner.notification.Failure;

importorg.junit.runner.notification.RunListener;

 

publicclassMyRunListenerextendsRunListener {

   @Override

   publicvoidtestFailure(Failure failure)throwsException {

     

   }

   }

編寫MyRunner類

importorg.junit.runner.notification.RunNotifier;

importorg.junit.runners.BlockJUnit4ClassRunner;

importorg.junit.runners.model.InitializationError;

 

publicclassMyRunnerextendsBlockJUnit4ClassRunner {

   privateMyRunListenermyRunListener;

 

     publicMyRunner(Class<?> c)throwsInitializationError {

        super(c);

        myRunListener=newMyRunListener();

     }

 

  @Override

   publicvoidrun(RunNotifier rn) {

         rn.addListener(myRunListener);

        super.run(rn);

 

   }

}

在測試代碼中引入

@RunWith(MyRunner.class)

publicclassMyhomeTestextendsSeleneseTestCase {

  具體的參與地址是: http://rockhoppertech.com/blogs/archives/45

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