As far as I can tell, you can't successfully run any tests from Android Studio yet (see How can I create tests in Android Studio?). You need to do it from the command line (note that you may need to clean first ./gradlew clean
):
To run the instrumentTests, use ./gradlew connectedInstrumentTest
To run your test task, use ./gradlew localTest
For future reference when Android Studio works better with test integration, you can set it up to run any given Gradle task. Go to 'Edit Configurations' and click the '+' button to add a new one. Choose 'Gradle' and then configure it to point to the correct gradle.build file and the task you want to run. I'm not sure what you would add in the 'Before launch' section, however, if anything.
All that being said, here's how I got my robotium tests to run:
I used the built-in Instrument Test configuration because I couldn't get the localTest task to work (I got the same errors as you did where it couldn't find any imports). I used the default folder structure with the
instrumentTest
folder, but it looks like you can just use the lineinstrumentTest.setRoot('src/test')
to make it work the same with your setup.I configured my build.gradle file like this:
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.5.+' } } apply plugin: 'android' repositories { mavenCentral() } dependencies { // Included library modules ... // My regular dependencies compile 'com.android.support:support-v4:13.0.0' ... // Test dependencies. Notice that all I have to include is robotium // because the rest just works using the instrumentTest configuration instrumentTestCompile 'com.jayway.android.robotium:robotium-solo:4.3' } android { compileSdkVersion 17 buildToolsVersion "18.0.1" defaultConfig { minSdkVersion 7 targetSdkVersion 16 } }
I added the robotium library in Project Structure (
command-;
on my mac) as a maven libary.I then wrote a test like this:
import android.test.ActivityInstrumentationTestCase2; import com.example.activity.WelcomeScreen; import com.jayway.android.robotium.solo.Solo; public class WelcomeScreenTest extends ActivityInstrumentationTestCase2<WelcomeScreen> { private Solo solo; public WelcomeScreenTest() { super(WelcomeScreen.class); } protected void setUp() throws Exception { super.setUp(); solo = new Solo(getInstrumentation(), getActivity()); } public void testActivity() { // robotium assert solo.assertCurrentActivity("Welcome Screen", WelcomeScreen.class); // junit assert assertEquals(true, true); } }
I then ran the test via the command line
./gradlew connectedInstrumentTest
and it worked for me.