tsorn tsorn - 3 months ago 96
Android Question

Android JUnitRunner Empty Test Suite: No Test Results

I'm having trouble running tests in Android Studio. I've read through a load of posts with the same problem here on Stack Overflow, but to no avail. Just to be clear, I want to test in the emulator and not in the JVM.

Run configuration:

run-config

Output:

Running tests
Test running startedTest running failed: No test results
Empty test suite.


Build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.0"

defaultConfig {
applicationId "ntnu.stud.markul.crowdshelf"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}


dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
// Unit testing dependencies
androidTestCompile ('com.android.support.test:rules:0.3') {
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile('com.android.support.test:runner:0.3') {
// Necessary if your app targets Marshmallow (since the test runner
// hasn't moved to Marshmallow yet)
exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
}


The test class (located in /src/androidTest/java/com/appname/app/):

@RunWith(AndroidJUnit4.class)
public class NetworkAndDBInstrumentationTest extends ActivityInstrumentationTestCase2<MainActivity> {
private MainActivity mActivity;
public NetworkAndDBInstrumentationTest() {
super(MainActivity.class);
}

@Before
public void setUp() throws Exception {
super.setUp();

// Injecting the Instrumentation instance is required
// for your test to run with AndroidJUnitRunner.
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
mActivity = getActivity();
}

@Test
public void typeOperandsAndPerformAddOperation() {
NetworkController.getCrowd("55f01f29f0a5fad2120bb1db");
System.out.print("ok");
Log.d("1TEST", "1TEST");
}

@After
public void tearDown() throws Exception {
super.tearDown();
}
}

Answer

I got it to work using this:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class NetworkAndDBInstrumentationTest {
    //use the following annotation and declare an ActivityTestRule for your activity under test
    @Rule
    public ActivityTestRule<RealmActivity> mActivityRule = new ActivityTestRule(RealmActivity.class);

    //use @Before to setup your test fixture
    @Before
    public void setUp() {  }

    //annotate all test methods with
    @Test
    public void testGetCrowd() {
        Assert.assertEquals(1, 1);
    }
    @After
    public void tearDown() {  }
}

Where RealmActivity is the Activity I want to test. Build.gradle and everything else is as in the question post. Now, a follow-up question: What does it matter that I make the @Rule with RealmActivity? Can't I use other Activities in that test? Can I make a Activity-independent test?

Comments