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
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With