0

I am not getting where i am going wrong. Using android studio and trying to write test cases. I created a new project and the structure is :

-app
 -src
  -androidTests
   -java (com.example.testdemo.tests)
       - MainActivityTest 
  -main
   -java (com.example.testdemo)
       -MainActivity
   -res
   -AndroidManifest

I will go step by step :

1. build.gradle file content

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.example.testdemo"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        testApplicationId "com.example.testdemo.tests"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.0.1'
}
  1. MainActivityTest.java

    import android.test.ActivityInstrumentationTestCase2;
    import android.test.suitebuilder.annotation.SmallTest;
    import android.widget.TextView;
    import com.example.webonise.testdemo.MainActivity;
    import com.example.webonise.testdemo.R;
    
    public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
    
    
    private TextView tv;
    private MainActivity mActivity;
    
    public MainActivityTest(Class<MainActivity> activityClass) {
        super(activityClass);
    }
    
    @Override
    public void setUp() throws Exception {
        super.setUp();
        mActivity = getActivity();
        tv = (TextView)mActivity.findViewById(R.id.tv);
        assertNotNull("Text view is null",tv);
    }
    
    public void testPreconditions(){
        assertNotNull("Text view is null",tv);
    }
    
    @SmallTest
    public void checkTextViewText(){
        String text = tv.getText().toString();
        assertEquals(mActivity.getResources().getString(R.string.hello_world), text);
    }
    
    @SmallTest
    public void checkTextViewText2(){
        String text = tv.getText().toString();
        assertEquals("fail", text);
    }
    }
    
  2. In studio Terminal i am giving this command: ./gradlew connectedCheck
    I am getting BUILD SUCCESSFUL but the report generated is showing this :


Test Summary 0 tests 0

failures

duration

successful Classes Class Tests Failures Duration Success rate


I appreciate you help.

Rana Ranvijay Singh
  • 6,055
  • 3
  • 38
  • 54
  • Have you tried to name your test folder from `androidTest` to `test`? – stuXnet Sep 24 '15 at 11:07
  • And its works .... you are awesome ... changed "androidTests" to "tests" and DONE. You can post this answer i will accept it. – Rana Ranvijay Singh Sep 24 '15 at 11:21
  • Changed "androidTests" to "tests" and it works. Now its showing one test case executed "ExampleUnitTest". Still non of my test cases are executed. Can you help? – Rana Ranvijay Singh Sep 24 '15 at 11:30
  • I can look at it in about two hours - maybe recheck which annotations are necessary in your test class. – stuXnet Sep 24 '15 at 11:33
  • Do you have any device connected (and recognized)? Either a virtual or a real one? – stuXnet Sep 24 '15 at 11:36
  • I have nexus connected. – Rana Ranvijay Singh Sep 24 '15 at 11:39
  • Sorry, my experience with Android is quite limited, I just read that `src/androidTest/java` is the default test directory for Android (but it seems that it's just `androidTest`, no plural) I can chat with you privately in about two hours if you want to, now I'm afk for a while :) – stuXnet Sep 24 '15 at 11:41
  • yes i know ... but for some reason .. my build fails if i give "androidTest" as parent. It works with "andriodTests" ,"test" and even with "tests". – Rana Ranvijay Singh Sep 24 '15 at 11:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90518/discussion-between-ranvijay-and-stuxnet). – Rana Ranvijay Singh Sep 24 '15 at 11:51

0 Answers0