简体   繁体   中英

basic android unit testing

I am using Android Studio and I wrote an ultra basic test for the MAinActivity class. I inherited MainActivityTest from extends ActivityInstrumentationTestCase2<MainActivity> and I want to test

private MainActivity mMainActivityTest;
public void testPreconditions() {
        assertNotNull("mMainActivityTest is null", mMainActivityTest);
    }

using gradle unit testing, without having to launch any emulator.

When I run the test, I get no red or green color bar, and whatever I choose assertNotNull or assertNull , I always get

External tasks execution finished 'cleanTest test --tests "com.my.app.MainActivityTest.testPreconditions"

how can I do correct unit test ?

In your build.gradle app level add to dependencies:

testCompile 'junit:junit:4.12'

Put your test in directory: src/test/java

Then your simple test, for example:

public class MainActivityTest(){
@Test
    public void testInitialAlwaysPasses(){
        assert true;
    }
}

Run it and you will see green lines and all the output. This is the basic Unit Test. For Instrumentation test you need to put your test in directory: src/androidTest/java. (Much easier is to right click in class you want to test and select Goto->test. It will create the test class for you). From here it will be easier to continue! Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM