Mehrdad Shokri
Mehrdad Shokri

Reputation: 2134

Create unit tests in android studio

I'm following this tutorial.
What I want to do is to create unit tests with AndroidJunitRunner.

My directory in app/src looks like this:

androidTest
debug
main
release

android docs suggests that I create a test/java for my unit tests.
The problem is that in android studio I can't create a new directory if I'm on android project mode. And I should find the directory manually and create it manually.
Is there a way to automatically create these directories and tests for one flavor?
I've read this question, which doesn't relate to my problem.

Upvotes: 1

Views: 891

Answers (2)

José Carlos
José Carlos

Reputation: 674

Just change to Project Mode and create the test folder. Take a look to the screenshot.

enter image description here

By the way, I think when you create a project the folder is already created.

Upvotes: 0

Jahnold
Jahnold

Reputation: 7683

It's easiest to just create the folder manually in the directory. Android Studio will notice it pretty quickly and it will show up in your Android view.

You will need to create the packages in the test/java folder too. So if your package name is com.foo.bar you need to create the following dir structure in your app/src folder:

test/java/com/foo/bar

As an aside in the latest versions of Android Studio you don't need to use AndroidJunitRunner anymore. Studio supports JUnit4 tests out of the box.

To make tests for a particular flavor you need to append the flavor name to the end of the test folder. So if your flavor is dev then the folder structure would be:

testDev/java/com/foo/bar

Upvotes: 2

Related Questions