Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 406
David T. Avatar asked Feb 21 '14 06:02

David T.


People also ask

What is test folder in Android Studio?

By default, Unit tests are written in src/test/java/ folder and Instrumentation tests are written in src/androidTest/java/ folder. Android studio provides Run context menu for the test classes to run the test written in the selected test classes.

How do I create a test folder in Android?

To add a testing source set for your build variant in Android Studio, follow these steps: In the Project window on the left, click the drop-down menu and select the Project view. Within the appropriate module folder, right-click the src folder and click New > Directory.

Which directory is used to store unit tests in your Android project *?

By default, the source files for local unit tests are placed in module-name/src/test/ . This directory already exists when you create a new project using Android Studio.


3 Answers

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.

like image 197
Ben Clayton Avatar answered Oct 16 '22 09:10

Ben Clayton


In Android Studio 1.3:

Build > Select Build Variant

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

like image 27
TTransmit Avatar answered Oct 16 '22 09:10

TTransmit


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.

like image 10
Piyush Agarwal Avatar answered Oct 16 '22 09:10

Piyush Agarwal