0

Everytime I run "gradlew test" on terminal in Android Studio, ALL my tests (stored in app\src\androidTest\java\path\to\package") are running.

dependencies to compile tests in app/build.gradle are:

androidTestCompile 'junit:junit:4.10'
androidTestCompile 'org.robolectric:robolectric:2.1.+'
androidTestCompile 'com.squareup:fest-android:1.0.+'
androidTestCompile "org.mockito:mockito-all:1.9.5"

I'm new in JUnit and Robolectric, also I've got a "default" TestRunner:

public class MyTestRunner extends RobolectricTestRunner {
    public MyTestRunner (Class<?> testClass) throws InitializationError {
        super(testClass);
    }

    @Override
    protected AndroidManifest getAppManifest(Config config) {
        String manifestProperty = System.getProperty("android.manifest");
        if (config.manifest().equals(Config.DEFAULT) && manifestProperty != null) {
            String resProperty = System.getProperty("android.resources");
            String assetsProperty = System.getProperty("android.assets");
            return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty),
                    Fs.fileFromPath(assetsProperty));
        }
        return super.getAppManifest(config);
    }

}

Every ClassTest.java is annotated by @RunWith(MyTestRunner.class).

How can I run only a few tests? E.g. only one specific test or all tests in one specific class / one specific package?

And maybe there's a way to do all this by Android Studio GUI instead of the Terminal? (would be nice to know ;) )

lis
  • 670
  • 1
  • 13
  • 32
  • 1
    possible duplicate of [Is there a way to execute a single Robolectric 2.3 test (using Gradle)?](http://stackoverflow.com/questions/24329875/is-there-a-way-to-execute-a-single-robolectric-2-3-test-using-gradle) – Eugen Martynov Aug 11 '14 at 07:22
  • You actually don't need this test runner. `RobolectricTestRunner` from robolectric 2.3 should work out of the box – Eugen Martynov Aug 11 '14 at 07:23

1 Answers1

0

This is how I filter Robolectric test in build.gradle:

robolectric {
    include project.hasProperty("testFilter") ? "**/*${project.ext.testFilter}*Test.class" : '**/*Test.class'
    exclude '**/espresso/**/*.class'

    // other robolectric config
}

When you run gradle command, pass in a command line param -PtestFilter=your_filter where your filter will be something in the full class name of the test class (excluding Test postfix).

A drawback of this is that when I make a typo in my testFilter, Robolectric executes them all (instead of executing none).

hidro
  • 12,333
  • 6
  • 53
  • 53