Is there a JUnit TestRunner for running groups of tests?

前端 未结 9 756
既然无缘
既然无缘 2021-02-05 13:42

I am currently using JUnit 4 and have a need to divide my tests into groups that can be run selectively in any combination. I know TestNG has a feature to annotate tests to assi

9条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-05 14:22

    First, you are addressing two problems - unit tests (often in the same package as the unit under test) and integration tests. I usually keep my integration tests in a separate package, something like com.example.project.tests. In eclipse, my projects look like:

    project/
      src/
        com.example.project/
      tsrc/
        com.example.project/
        com.example.project.tests/
    

    Right-clicking on a package and selecting 'run' runs the tests in the package; doing the same on the source folder runs all the tests.

    You can acheive a similar effect, although you expressed a disinterest in it, by using the Suite runner. However, this violates DRY - you have to keep copies of the test names up to date in the suite classes. However, you can easily put the same test in multiple suites.

    @RunWith(Suite.class)
    @Suite.SuiteClasses( { 
        TestAlpha.class, 
        TestBeta.class })
    public class GreekLetterUnitTests {
    }
    

    Of course, I really should be keeping these things automated. A good method for doing that is to use the Ant task.

    
      
        
          
            
            
          
        
      
    
    
      
        
          
            
          
        
      
    
    

提交回复
热议问题