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
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 asinstrumentTest
so Studio can automatically detects the sourceSets for your test projector
Alternate is you can have your
tests
directory in root(with you main directory) and sources in a manner liketests/java
, as the second link saysin sourceSets configuration under android tag
From the document
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:
Now add a new configuration for testing:
Now you can run the tests.
In Android Studio 1.3:
Build > Select Build Variant
In the Build Variants window, select Unit Tests as the Test Artifact.
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.