3

I run my JUnit and Mockito tests in a big project. I use them for testing my server-side components that connect to web-service. All these connections require some time and it is not neccessary for them to be executed during the build.

I would like that my tests would be ignored during the build.

I have about 10 classes with tests. So the obvious way is to annotate all the classes with @Ignore. However I should do this every time I commit my code to the project and then re-annotate all tests. Not the very best solution I think.

So is this possible somehow simply ignore all package (let say com.example.tests) with the tests? Or what might be the solution to ignore tests in the build in a simple way?

Kirill Ch
  • 5,496
  • 4
  • 44
  • 65
  • 1
    Take a look at [Junit test categories](https://github.com/junit-team/junit4/wiki/categories) –  May 12 '17 at 14:59
  • 1
    see http://stackoverflow.com/questions/14132174/how-to-exclude-all-junit4-tests-with-a-given-category-using-maven-surefire – Markus Mitterauer May 12 '17 at 20:05

3 Answers3

9

You can mention on your build.gradle what packages to exclude from tests

test {
    exclude '**/*IntegrationTest*'
}

same for maven:

must consider this notation:

By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:

"**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test".
"**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test".
"**/*Tests.java" - includes all of its subdirectories and all Java filenames that end with "Tests".
"**/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".


<project>
  [...]
 <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.20</version>
            <configuration>
                <excludes>
                <exclude>*com.example.tests*/*Test.java</exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>
  [...]
</project>

Another option is the old

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>

or even when call it

mvn install -DskipTests
Guilherme
  • 593
  • 1
  • 6
  • 23
  • Yes. That might be a good solution. However it means that I should change the main pom.xml if I add some new test classes, or I should fix my test classes and add all new methods in them. Of course I would prefer not to change main pom.xml only because of my own tests. However the answer is very good. Thanks. – Kirill Ch May 12 '17 at 15:31
  • you can still use the exclusion for the entire package, so if new tests classes and methods are created under the same package they are also excluded from run on build. – Guilherme May 12 '17 at 18:34
  • Great! How can I exclude only my own package? Could you please complete your answer with this information. – Kirill Ch May 12 '17 at 18:37
  • done! let me know if it works, I am using gradle here, try the second entry without the ' – Guilherme May 12 '17 at 18:39
  • I'm sorry I don't understand. Could you use the exact example. Let say the package with the tests is **com.example.tests**. How to exclude it during the build? – Kirill Ch May 12 '17 at 18:43
  • ok, I am not sure, but you could try the new version of my answer – Guilherme May 12 '17 at 18:50
  • :) Ok, I'll try and let you know. Thanks. – Kirill Ch May 12 '17 at 18:51
  • Nope. It doesn't work. Please return a previous version of the answer so that others can see some working example. Thank you for your help. – Kirill Ch May 12 '17 at 19:24
  • ok, finally I had time to build my own test, it works as last edit notes ;-) – Guilherme May 12 '17 at 20:19
  • 1
    Great! That is really works! Also I have found the solution based on Spring @IfProfileValue annotation, you can see my answer below. – Kirill Ch May 12 '17 at 20:34
1

Using Categories seems to be an option that can come in handy

This is how you may add these to your Gradle script.

test {
    useJUnit {
        includeCategories 'org.gradle.junit.CategoryA'
        excludeCategories 'org.gradle.junit.CategoryB'
    }
}

A sample can be found here, adding it for a quick reference.

public interface FastTests 
{ 
    /* category marker */ 
}
public interface SlowTests 
{ 
    /* category marker */ 
}

public class A 
{
    @Category(SlowTests.class)
    @Test public void a() 
    {
    }
}

@Category(FastTests.class})
public class B 
{
    @Test public void b() 
    {
    }
}

@RunWith(Categories.class)
@IncludeCategory(SlowTests.class)
@ExcludeCategory(FastTests.class)
@SuiteClasses({ A.class, B.class })
public class SlowTestSuite 
{
}
N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
  • Works even without test suites: http://stackoverflow.com/questions/14132174/how-to-exclude-all-junit4-tests-with-a-given-category-using-maven-surefire – Markus Mitterauer May 12 '17 at 20:06
1

I have found the solution for my case.

To disable all the tests during the build or even in any other context that you want the Spring annotation @IfProfileValue can be used. All tests with this annotation will be executed only in wanted context.

The example is this:

@IfProfileValue(name="enableTests", value="true")
public class DemoApplicationTests {

    @Test
    public void contextLoads() {

    ...

    }

}

In my IDE I can edit the configuration and set the variable by:

-DenableTests=true

This annotation can be used on the level of a class or on the level of a test also. All classes or tests annotated with such @IfProfileValue will be executed only in my environment and will be ignored during the build.

This approach is the best for me because it is not convenient in my project to change main pom.xml for my own test needs.

Addition. Also in Spring or Spring Boot you should add Runner. For example in Spring:

@RunWith(SpringJUnit4ClassRunner.class)
@IfProfileValue(name="enableTests", value="true")
@ContextConfiguration(classes = { YourClassConfig.class })

YourClassConfig might be empty:

@Configuration
public class YourClassConfig {
}
Kirill Ch
  • 5,496
  • 4
  • 44
  • 65