How to create a test structure in Android Studio?

I have a project that has recently been ported to Android Studio (and gradle). Now I want to create a test suite for him. Unfortunately, I'm not sure how to do this in Android Studio (it was so easy in Eclipse ADT).

I have the following file structure: project structure

My build.gradle looks like this:

apply plugin: 'android'

repositories {
    maven { url 'libs'}
}

sourceSets {
    testLocal {
        java.srcDir file('src/test/java')
        resources.srcDir file('src/test/resources')
    }
}

dependencies {
    compile 'com.larswerkman.holocolorpicker:holocolorpicker:1.1'
    compile fileTree(dir: 'libs', include: '*.jar')
    testLocalCompile 'junit:junit:4.8.2'
    testLocalCompile 'com.google.android:android:4.0.1.2'
    testLocalCompile 'com.google.android:support-v4:r6'
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"
    defaultConfig {
        testPackageName "com.glasstowerstudios.stainedglass.test"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }
}

task localTest(type: Test, dependsOn: assemble) {
    testClassesDir = sourceSets.testLocal.output.classesDir

    android.sourceSets.main.java.srcDirs.each { dir ->
        def buildDir = dir.getAbsolutePath().split('/')
        buildDir =  (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')

        sourceSets.testLocal.compileClasspath += files(buildDir)
        sourceSets.testLocal.runtimeClasspath += files(buildDir)
    }

    classpath = sourceSets.testLocal.runtimeClasspath
}

check.dependsOn localTest

The testing that I created is called AllTests.java, and it just has one statement in it right now (I'm just trying to get the test environment installed before I implement the actual test logic:

import junit.framework.Assert;
import junit.framework.TestCase;

package com.glasstowerstudios.stainedglass.test;

public class AllTests extends TestCase {
  public void setUp() {

  }

  public void tearDown() {

  }

  public void testStartup() {
    Assert.assertEquals(true, true);
  }
}

When I run the build, it succeeds even if I do it gradle localTestfrom the command line. However, inside Android Studio, when I right-click on AllTests.java and select Run AllTests, I get the following error:

Error running 'AllTests':
Class 'AllTests' not found in module 'stainedglass'

, - , ( /, NewFile. , , src/main/java ( - src/main/java , src/test/java .

"" Android Studio?

+4

Source: https://habr.com/ru/post/1527153/


All Articles