Barodapride
Barodapride

Reputation: 3723

How do I get started making unit tests for my Android app?

This seems like a silly question but I have an app in Android Studio and I'd like to try some TDD/Unit testing. I tried adding a tests folder and adding a class manually in the explorer but found I can only add Java classes under my main src folder. How can I simply add a tests folder at the same level as my src folder and create Java classes for it? Do I need something in my build.gradle?

Upvotes: 0

Views: 96

Answers (1)

Rodrigo Henriques
Rodrigo Henriques

Reputation: 1812

The new latest version of gradle build tools (1.3.1) came with new test functionality and I will show you how to create your test easily.

First of all there is now two default directories for tests:

androidTest for Instrumentation Tests and test for Java Unit Tests

You should put your instrumentation tests into androidTest and your simple JUnit tests (with almost no android dependencies) into test

If you wanna run your Unit Tests your have to select this option:

Pure Java tests

When you select this options your directory will be highlighted like this: enter image description here

When you select instrumentation tests then you will see something like this: enter image description here

You can go deep into Support Test Library checking this link:

http://developer.android.com/intl/pt-br/tools/testing-support-library/index.html

Hope that it helps!

Upvotes: 4

Related Questions