2009-08-19 27 views
5

Özellikle testRunFinished için JUnitCore dinleyicileri olarak eklemek istediğim bazı test altyapısı sınıflarım var. Karınca'nın görevinden Junit 4'ü çağırıyorum.Dinleyici eklemek için mevcut JUnitCore'a nasıl erişilir?

Görev tarafından oluşturulan JUnitCore'a erişmek için bir yol var mı, böylece bir dinleyici ekleyebilir miyim?

cevap

2

Karınca'nın görevinden değil.

Test paketinizi "el ile" çalıştıran bir ana yöntem yazmalısınız. Kendi Runner'ı verin ve run(RunNotifier notifier) geçersiz:

package test; 

import org.junit.runner.Request; 
import org.junit.runner.Result; 
import org.junit.runner.Runner; 
import org.junit.runner.notification.RunListener; 
import org.junit.runner.notification.RunNotifier; 

public class RollYourOwnTestRun { 

    public static void main(String[] args) { 
     Runner runner = Request.classes(StackTest.class).getRunner(); 
     RunNotifier notifier = new RunNotifier(); 
     Result result= new Result(); 
     RunListener listener= result.createListener(); 
     notifier.addListener(listener); 
     notifier.addListener(...); // add your listener 
     notifier.fireTestRunStarted(runner.getDescription()); 
     runner.run(fNotifier); 
     notifier.fireTestRunFinished(result); 
    } 

} 
1

A @RunWith ek açıklama (bazı küçük API en iyi uygulama ihlalleri ile) yardımcı olabilir. RunNotifier aracılığıyla, yalnızca şu anda dahili olarak işaretlenmiş olan add * Listener-API'yi kullanabilirsiniz. İyi şanslar!

1

Bu biraz geç olmakla birlikte (org.apache.ant:ant-junit itibaren) ant'ın JUnitResultFormatter içine RunListener sarma deneyebilirsiniz:

import static org.apache.tools.ant.taskdefs.optional.junit.JUnitVersionHelper.getTestCaseClassName; 
import static org.apache.tools.ant.taskdefs.optional.junit.JUnitVersionHelper.getTestCaseName; 
import static org.junit.runner.Description.createTestDescription; 

public class MyJunitFormatter implements JUnitResultFormatter { 

private final MyListener delegate = new MyListener(); 

@Override 
@SneakyThrows(Exception.class) 
public void endTest(Test test) { 
    delegate.testFinished(
      createTestDescription(
        getTestCaseClassName(test), 
        getTestCaseName(test))); 
} 

// .... 

https://mail-archives.apache.org/mod_mbox/ant-user/201009.mbox/%[email protected]cehub.com%3E

Bkz
İlgili konular