0

I want to define unit tests for my classes and am running into the following issue. If I define the unit test inside the androidTest folder (testing with instrumentation) then the test runs normally with valid results. If, however, I define the test inside the test folder (local testing) then running the test generates a message "class not found: "[classname]" Empty test suite". This wouldn't be so bad and I would just run all tests from androidTest, except that I want to use code coverage and Android Studio doesn't allow me to run coverage tests from androidTest, but only from test.

Why isn't Android Studio able to find the unit tests when they are defined in the test folder, but is able to when they are defined in the androidTest folder?

Code:

public class SomeTest{

    private Context mInstrumentationCtx;

    @Before
    public void setup() {
        mInstrumentationCtx = InstrumentationRegistry.getTargetContext();
        // do some setup actions
    }

    @Test
    public void testFirst() throws Exception {
        Assert.assertEquals(true, true);
    }

}

Gradle config:

android {

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

debug {
            testCoverageEnabled = true
        }
}

testCompile 'junit:junit:4.12'
    testCompile ('com.android.support.test:runner:0.5', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

compile 'com.android.support:appcompat-v7:25.1.0'
5
  • how do you run the tests (that fail) ? Have you tried to run on one class/dir level? Commented Jan 7, 2017 at 20:37
  • Right click on test class - "run [testname] with coverage" Commented Jan 7, 2017 at 20:38
  • works as expected in freshly created project in latest AS, we can try finding the diff Commented Jan 7, 2017 at 20:41
  • whats your gradle version? Commented Jan 7, 2017 at 20:42
  • version is: 2.3.0-beta1 Commented Jan 7, 2017 at 20:44

1 Answer 1

1

May be not an answer per se

You could try changing

 testCompile ('com.android.support.test:runner:0.5', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

to

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

which seems to be default runner in recent gradle versions.

It works for me with the latter but fails with the former


Edit:

After some struggle it looks like downgrade to version 2.2.3 is a solution for now, smth must have changed in 2.3 beta that author has used

Sign up to request clarification or add additional context in comments.

8 Comments

I've tried replacing it but I still get the failure "Process finished with exit code 1 .. Empty test suite"
what's InstrumentationRegistry in your case? is it supposed to run in deviceless setup?
I want to run tests for an Sqlite wrapper and it needs a context, hence the instrumentationRegistry. I've tried commenting out that line but the test fails the same way
sorry im clueless, whats your build tool version? would you mind trying gradle:2.2.3 ?
buildToolsVersion "25.0.2" - sure, I'll try. Thanks for your help :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.