Search code examples
mavenunit-testing

Can Maven `test` issue warning if no tests exist?


Does Maven have an option to issue a warning if the test sub-command is used, but the project it's being used on contains no tests? I've tried tests where I ran mvn clean compile test ... on a project that did not have a src/test directory. The test sub-command just reported that no tests were found and continues without error. Is there a way so that if test is used, but no tests are found, that it "complain" about it? I could then look for the complaint in the log and take actions from there.


Solution

  • There is an option for surefire plugin: failIfNoTests.

    You can add as user property:

    mvn test -DfailIfNoTests
    

    or add in project configuration:

    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>ues-the-latest</version>
          <configuration>
            <failIfNoTests>true</failIfNoTests>
          </configuration>
        </plugin>
      <plugins>
    <pluginManagement>
    
    

    https://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#failIfNoTests

    The same options is also for failsafe:

    https://maven.apache.org/surefire/maven-failsafe-plugin/verify-mojo.html#failIfNoTests