How to get Android Studio to recognize file as sou

2019-01-13 20:29发布

问题:

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

回答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.



回答2:

In Android Studio 1.3:

Build > Select Build Variant

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



回答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.



回答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.