2010-09-10 54 views
6

我想在我的Eclipse項目中運行所有junit 4測試。該項目使用/ source和/ test進行設置。在/測試各種套餐,如:如何遞歸查找並在Eclipse中運行所有Junit 4測試?

com.yaddayadda.test.core.entity 
com.yaddayadda.test.core.framework 

如果我在Package Explorer中的/test級別右鍵單擊並選擇運行方式; JUnit測試我收到的錯誤:

No tests found with test runner 'JUnit 4'. 

如果我在com.yaddayadda.test.core.entity單擊鼠標右鍵,然後找到並運行該程序包中的所有測試。所以@Test註釋是正確的(它們也被構建服務器上的Ant正確拾取)。但是,如果我嘗試並在com.yaddayadda.test.core內運行所有測試,則不會發現任何測試。

基本上,它似乎只是看在包裏,而不是在所有的孩子們那裏。有沒有辦法來解決這個問題?

回答

4

櫃面其他人正在尋找一個解決方案,這一點,我發現這裏的伯特·貝克威思網站答案:

http://burtbeckwith.com/blog/?p=52

To use this, just right-click it in the class tree in Eclipse and click 「Run As JUnit Test」.

import java.io.File; 
import java.io.UnsupportedEncodingException; 
import java.lang.reflect.Modifier; 
import java.net.URLDecoder; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Comparator; 
import java.util.List; 

import org.apache.log4j.Logger; 
import org.junit.internal.runners.InitializationError; 
import org.junit.runner.Description; 
import org.junit.runner.RunWith; 
import org.junit.runner.notification.RunListener; 
import org.junit.runner.notification.RunNotifier; 
import org.junit.runners.Suite; 

/** 
* Discovers all JUnit tests and runs them in a suite. 
*/ 
@RunWith(AllTests.AllTestsRunner.class) 
public final class AllTests { 

    private static final File CLASSES_DIR = findClassesDir(); 

    private AllTests() { 
    // static only 
    } 

    /** 
    * Finds and runs tests. 
    */ 
    public static class AllTestsRunner extends Suite { 

    private final Logger _log = Logger.getLogger(getClass()); 

    /** 
    * Constructor. 
    * 
    * @param clazz the suite class - <code>AllTests</code> 
    * @throws InitializationError if there's a problem 
    */ 
    public AllTestsRunner(final Class<?> clazz) throws InitializationError { 
     super(clazz, findClasses()); 
    } 

    /** 
    * {@inheritDoc} 
    * @see org.junit.runners.Suite#run(org.junit.runner.notification.RunNotifier) 
    */ 
    @Override 
    public void run(final RunNotifier notifier) { 
     initializeBeforeTests(); 

     notifier.addListener(new RunListener() { 
     @Override 
     public void testStarted(final Description description) { 
      if (_log.isTraceEnabled()) { 
      _log.trace("Before test " + description.getDisplayName()); 
      } 
     } 

     @Override 
     public void testFinished(final Description description) { 
      if (_log.isTraceEnabled()) { 
      _log.trace("After test " + description.getDisplayName()); 
      } 
     } 
     }); 

     super.run(notifier); 
    } 

    private static Class<?>[] findClasses() { 
     List<File> classFiles = new ArrayList<File>(); 
     findClasses(classFiles, CLASSES_DIR); 
     List<Class<?>> classes = convertToClasses(classFiles, CLASSES_DIR); 
     return classes.toArray(new Class[classes.size()]); 
    } 

    private static void initializeBeforeTests() { 
     // do one-time initialization here 
    } 

    private static List<Class<?>> convertToClasses(
     final List<File> classFiles, final File classesDir) { 

     List<Class<?>> classes = new ArrayList<Class<?>>(); 
     for (File file : classFiles) { 
     if (!file.getName().endsWith("Test.class")) { 
      continue; 
     } 
     String name = file.getPath().substring(classesDir.getPath().length() + 1) 
      .replace('/', '.') 
      .replace('\\', '.'); 
     name = name.substring(0, name.length() - 6); 
     Class<?> c; 
     try { 
      c = Class.forName(name); 
     } 
     catch (ClassNotFoundException e) { 
      throw new AssertionError(e); 
     } 
     if (!Modifier.isAbstract(c.getModifiers())) { 
      classes.add(c); 
     } 
     } 

     // sort so we have the same order as Ant 
     Collections.sort(classes, new Comparator<Class<?>>() { 
     public int compare(final Class<?> c1, final Class<?> c2) { 
      return c1.getName().compareTo(c2.getName()); 
     } 
     }); 

     return classes; 
    } 

    private static void findClasses(final List<File> classFiles, final File dir) { 
     for (File file : dir.listFiles()) { 
     if (file.isDirectory()) { 
      findClasses(classFiles, file); 
     } 
     else if (file.getName().toLowerCase().endsWith(".class")) { 
      classFiles.add(file); 
     } 
     } 
    } 
    } 

    private static File findClassesDir() { 
    try { 
     String path = AllTests.class.getProtectionDomain() 
     .getCodeSource().getLocation().getFile(); 
     return new File(URLDecoder.decode(path, "UTF-8")); 
    } 
    catch (UnsupportedEncodingException impossible) { 
     // using default encoding, has to exist 
     throw new AssertionError(impossible); 
    } 
    } 
} 
+4

請將博文的要點添加到您的回答中,以防博客在我們的404上發佈。 – ShiDoiSi 2011-03-21 07:52:56

8

第一: 在項目管理器中選擇您的項目和按Alt + Shift + X T.它將運行在該項目下的所有JUint測試。右鍵單擊該項目並選擇「Run as」(運行方式) - > JUnit測試即可完成相同的操作。

如果這不起作用(可能),請轉到「運行/運行配置」,創建一個新的JUnit配置並告訴它運行項目中的所有測試。如果這不起作用,我需要先看看你的項目,然後才能提供幫助。

+0

這兩個選項都會導致:未使用測試運行器「JUnit 4」發現測試。 – 2010-09-15 22:21:33

+0

我會嘗試從頭開始創建一個新項目並將代碼複製到那裏。這似乎是一個破碎的配置。 – 2010-09-16 22:39:33

+0

爲我工作。正是我想知道的。 – ashes999 2011-01-06 20:35:39

0

您是否已將/test添加到構建路徑 - >源文件夾?

+0

感謝您的建議。是的,那已經在那裏了。 – 2011-03-21 04:00:03

0

我發現,伯特貝克威思的代碼是偉大的,但它無論您放置AllTests的位置,都將運行項目中的每項測試。對一個函數的這種修改將允許您將AllTests放置在您的項目的任何子目錄中,並且它只會在該位置下運行測試。

private static Class<?>[] findClasses() {List<File> classFiles = new ArrayList<File>(); 
    String packagepath = AllTests.class.getPackage().getName().replace(".", "/"); 
    File RELATIVE_DIR = new File(CLASSES_DIR.getAbsolutePath() + "\\" + packagepath); 
    findClasses(classFiles, RELATIVE_DIR); 
    List<Class<?>> classes = convertToClasses(classFiles, CLASSES_DIR); 
    return classes.toArray(new Class[classes.size()]); 
} 
相關問題