TestCase class not found by Android Studio

后端 未结 7 1689
刺人心
刺人心 2020-12-03 17:24

I have written a simple test case class and placed it in the default test directory for Android Studio: \"src/androidTest\". I\'ve created an Android Tests build configurati

相关标签:
7条回答
  • 2020-12-03 17:52

    Please post your entire build.gradle file.
    What version of 'com.android.tools.build:gradle' are you using?
    In 0.8, the default test path is "instrumentTest/java/...'.
    In 0.9, the default test path changed to "androidTest/java/...".

    To use androidTest, you should have:

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.9.+'
        }
    }
    
    0 讨论(0)
  • 2020-12-03 17:54

    You need to check "run" configuration.

    If you are going to run tests from androidTest folder, you should choose configuration under Android Instrumented Test, and not the junit.

    Run(Top tool window) / Edit configurations...

    0 讨论(0)
  • 2020-12-03 18:04

    This issue is crazy, it just is. Don't overthink it.

    How I've fixed it (Win10) :

    1. Reboot (maybe not necessary)
    2. Try to Run test
    3. Try to Run with coverage

    It fixed it for me on Android Studio 3.6.3

    These two ways of running tests are different and this approach could fix the issue for you too.

    0 讨论(0)
  • 2020-12-03 18:06

    Maybe I can save time for somebody: I had the same kind of error "Class not found:" when I had "release" buildVariant selected in Android Studio, so before you run AndroidTests(Espresso) double check your build variant and try to set it to "Debug"

    0 讨论(0)
  • 2020-12-03 18:09

    You needn't really to rebuild structure of src directories to create appropriate tests.

    The tested and the testing class should be in the same package.

    But as for folders, they should be separated. So, for tests and normal sources we use different roots.

    The problem is, how to set these roots. In the AS 1.5 (maybe earlier versions can do it, too), it can be done easily:

    1. Make practically any structure of directories. Somewhere in it there is the root dir of tested sources. Right click it and Mark Directory As ... Sources Root.
    2. Make another arbitrary structure of directories for tests. Somewhere in it there is the root dir of test sources. Right click it and Mark Directory As ... Test Sources Root.

    Under these roots the path to a test class must be the same as the path to the tested class from its root, for dir names along these paths define the packages and they should be the same. But you needn't worry about the structures above the roots.

    Using this method you can add tests to any existing structure of sources. If you are creating the structure yourself, make it rather traditional.

    Edit. Notice, that folders structure for modules of Java 9 is something ABSOLUTELY different.

    0 讨论(0)
  • 2020-12-03 18:12

    The package structure under the androidTest/java directory needs to exactly parallel the structure under the main/java directory.

    My problem above was that the package structure under main was com.mydomain.myapp.subpackage and the directory structure under androidTest was com.mydomain.myapp.subpackage.somethingelse.

    Once the package structures matched, the tests were discovered and executed flawlessly.

    0 讨论(0)
提交回复
热议问题