JohnRock JohnRock - 1 year ago 71
Android Question

How to run simple junit tests inside Android Studio 2.1.3

There was a brief moment in time recently where the Build Variants view in Android Studio contained an option for 'Unit Tests' and if such option was checked you could easily run your basic unit tests right in Android Studio by simply selecting the test class and hitting 'run'. Then I think when Android Studio 2.0 was released they removed that option from the build variants menu. All the documentation says that you should be able to right click on the test class and say 'RUN', but whenever I do that I am prompted to select a deployment target which is for instrument tests and I am not interested in those. Am I missing something here? How do you run your basic unit tests in Android Studio 2.1.3?

This is an excerpt from my current build.gradle which contains code I had needed previously to run unit tests:

android {
compileSdkVersion project.COMPILE_SDK_VERSION.toInteger()
buildToolsVersion project.BUILD_TOOLS_VERSION

defaultConfig {
minSdkVersion project.SDK_MIN_VERSION_LIBRARY
targetSdkVersion project.COMPILE_SDK_VERSION.toInteger()
versionName project.VERSION_NAME
versionCode project.VERSION_CODE.toInteger()

lintOptions {
abortOnError false
buildTypes {
versionNameSuffix = "DEBUG"
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), ''
sourceSets {
androidTest {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/resources')

testOptions {
unitTests.returnDefaultValues = true
android.testOptions.unitTests.all {
// Configure includes / excludes
include '**/*Test.class'
exclude '**/espresso/**/*.class'

// Configure max heap size of the test JVM
maxHeapSize = '2048m'

// Configure the test JVM arguments - Does not apply to Java 8
jvmArgs '-XX:MaxPermSize=4096m', '-XX:-UseSplitVerifier'

// Specify max number of processes (default is 1)
maxParallelForks = 4

// Specify max number of test classes to execute in a test process
// before restarting the process (default is unlimited)
forkEvery = 250

// configure whether failing tests should fail the build
ignoreFailures false

// use afterTest to listen to the test execution results
afterTest { descriptor, result ->
println "Executing test for ${descriptor.parent}: ${} with result: ${result.resultType}"


Answer Source

You have the androidTest sourceset overridden to look in src/test/. Hence, Gradle thinks that your test/ code represents instrumentation tests.

Should I just remove the whole sourceSets block?

Ideally, you follow the stock configuration: androidTest/ holds the instrumentation tests, test/ holds the unit tests.

If you want something else, that's fine. However, when you right-click on an instrumentation test class and choose Run, it will run an instrumentation test. So, if you want androidTest/ to hold unit tests, and instrumentationTestBecauseLongDirectoryNamesAreFun/ to hold instrumentation tests, that's fine, but you'll need to keep track of what's where yourself.

So, whether you get rid of the sourceSets closure, or update it to reflect where you want your instrumentation tests and unit tests to go, is up to you.