Create unit tests in android studio

2019-07-23 15:59发布

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.

2条回答
够拽才男人
2楼-- · 2019-07-23 16:27

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.

查看更多
成全新的幸福
3楼-- · 2019-07-23 16:33

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

查看更多
登录 后发表回答