maven - fail build when unit test takes too long

后端 未结 6 2115
广开言路
广开言路 2021-02-12 20:22

I have in my project a lot of unit tests, written in JUnit and TestNG. The building process is based on maven with surefire plugin.

Is there any way/plugin for maven to

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-12 20:51

    If all your tests extend some base class, I think you can stick @Rule in there which will apply to all the @Test in the child class.

    e.g.

    import org.junit.rules.Timeout;
    public abstract class BaseTestConfiguration {
        @Rule public Timeout testTimeout = new Timeout(60000); // 60k ms = 1 minute
    }
    
    public class ThingTests extends BaseTestConfiguration {
        @Test
        public void testThis() {
            /*will fail if not done in 1 minute*/
        }
        @Test
        public void testThat() {
            /*will fail if not done in 1 minute*/
        }
    }
    

    We found this to be easiest rather than messing with AspectJ or secret configuration directives. Devs are more likely to figure out the Java parts than all the secret XML this and that. You can put @Before @After and any number of other junit directives in the base class too.

提交回复
热议问题