Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

junit - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...