How to get Android Studio to recognize file as source (test)

25,700

Solution 1

In Android Studio 1.0 the scheme has changed a little bit.

Your path should be (app)/src/androidTest/java/com/myapp/HelloWorldTest.java

Here's how I set up Unit Tests in a new Android Studio project:

  • Open app in Android Studio.
  • Set the Project explorer (left hand window) to display 'Project' mode. Tap the little drop-down at the top left and select 'Project'.
  • Right click the 'src' directory, 'New -> Directory'.
  • Call new directory androidTest
  • Right click androidTest and add a 'java' directory. It will appear in green (indicating it's a test src directory).
  • Now right-click again and add a package, e.g. com.mycompany.myapp.tests
  • Add a new class that extends AndroidTestCase.

Now add a new configuration for testing:

  • Edit Configurations
  • Click + at top left
  • Select Android Tests
  • In General Tab select your main module.
  • Hit OK

Now you can run the tests.

Solution 2

In Android Studio 1.3:

Build > Select Build Variant

In the Build Variants window, select Unit Tests as the Test Artifact.

Solution 3

Follow the guidelines available here :

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing

and here

http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Configuring-the-Structure

Summary :

As the first link says you have to rename your test directory as instrumentTest so Studio can automatically detects the sourceSets for your test project

or

Alternate is you can have your tests directory in root(with you main directory) and sources in a manner like tests/java, as the second link says

instrumenttest.setRoot("tests")

in sourceSets configuration under android tag

From the document

Note: setRoot() moves the whole sourceSet (and its sub folders) to a new folder. This moves src/instrumentTest/* to tests/* This is Android specific and will not work on Java sourceSets.

Solution 4

I had similar problem for a long time and finally I solved that.

To make Android Studio to recognize src/test dir as source you have to pick 'Unit Tests' in 'Build Variants'->'Test Artifact:'

However, in my project I hadn't had 'Unit Tests' I only had 'Android Instrumentation tests'. This is two different types of unit-tests in Android.

See: http://developer.android.com/training/testing/unit-testing/index.html
1. Unit Tests - Local Unit Tests
2. Android Instrumentation tests - Instrumented Unit Tests

That is not obvious at all but inside 'Build Variants'->'Test Artifact:' you does not have 'Unit Tests' option when you have multiple modules in your project.

My project dir looked like this:
:project
-:module1
-:module2
-:module3
-:module4
-:module5
-settings.gradle -> include ':module1',':module2', ...
These modules are not really linked to each other, but it is how it has to be for many other developers who came from Eclipse. That was a problem.

Then I created separate project for my module only:
:project
-:module1
-settings.gradle -> include ':module1'

After that 'Unit Tests' appeared in 'Build Variants'->'Test Artifact:' and I can choose between Local Unit Tests and Android Instrumentation tests now.

Share:
25,700
David T.
Author by

David T.

Junior Android developer coming here on Stack Overflow to improve my coding abilities game dev experience (iOS, Android & OUYA + others)

Updated on December 04, 2020

Comments

  • David T.
    David T. over 3 years

    I'm trying to create Robolectric tests for an android project (heck, i'd be happy to even make them unit tests)

    I have the folder directory as:

    MyApp
        - app
            - src
                - main
                    - java
                        - com.myapp
                            HelloWorld
                - test
                    - java
                        - com
                            - myapp
                                HelloWorldTest.java
    

    The problem is that HelloWorldTest.java can't be run because it's not being recognized as source. how do i set it up so that i can run this class as a test?????

    if i try to do CMD + SHIFT + T (shortcut for creating tests), it prompts to create the tests under the same directory as my source file and i do NOT want that

  • David T.
    David T. about 10 years
    Thanks for the note. I tried the first way, but i still keep getting !!! JUnit version 3.8 or later expected: and java.lang.RuntimeException: Stub!. Most answers on stackoverflow tell me to just move JUnit upwards in the project module settings, but that doesn't solve the issue :/
  • Piyush Agarwal
    Piyush Agarwal about 10 years
    Can you please include the screenshot of File > Project Structure > yourmodule > Dependencies in your question and also the build.gradle file ?
  • Ben Clayton
    Ben Clayton about 9 years
    This has changed for Android Studio 1.0 and up - the folder is now called androidTest. Please see my answer
  • Seraphim's
    Seraphim's almost 8 years
    The folder is "activated" as a valid source java folder once you selected the build variant.