[Android development experience] Use Android Studio for unit testing and android unit testing

Source: Internet
Author: User

[Android development experience] Use Android Studio for unit testing and android unit testing

Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992

Android Studio has reached version 1.2, and developers in China are basically switching from Eclipse to Android Studio. for Android Developers, it will surely be the world of Android Studio in the future.
When I completed the egg project yesterday, I needed to perform a unit test. It was very simple in the Eclipse environment, but I have not tried it yet in the Android Studio environment, I have found a lot of materials in China, mostly in the cloud, and have not been tested successfully. Then, I have successfully run it under the guidance of this article. I would like to translate it for your reference.

Original article: http://rexstjohn.com/unit-testing-with-android-studio/

This article describes the basics of unit testing in Android Studio.

Unit tests can be performed in Android Studio.

Many articles guide you to add several lines of code in the "build. gradle" file to allow unit tests, and tell you to add the Android Test Library to the project dependency.

In fact, you do not need to follow this wrong method, because this is completely unnecessary.

Android Studio supports Android unit testing. You only need to configure it in your project.

Note: There are several popular Android unit testing frameworks, such as Robolectric, which require more configurations than I mentioned here, I hope that I can write more guidance articles on this topic in the future.

Create your unit test folder

I like to put unit testing in my main project, such as "com. mypath. tests.". You can put the test directory where you want it. Before you start, create your test folder as follows. (Note: This step is not necessary. You can also create the unit test class in the same path as the default ApplicationTest class of Android Studio)

Next, create a class named "ExampleTest". Note that it must inherit from the InstrumentationTestCase class.

Then you can add a simple test code. We know that this code will definitely fail to run.

public class ExampleTest extends InstrumentationTestCase {    public void test() throws Exception {        final int expected = 1;        final int reality = 5;        assertEquals(expected, reality);    }}

Note: All testing methods must start with "test" so that Android Studio can automatically find all the methods you want to perform unit testing.

Configure unit tests for your project

Now we have a test unit that will fail to run. We must run it.

First click "Run-> Edit deployments"

Click "+" and select "add Android Tests" from the pop-up list on the left. Then, you can rename it in the upper right corner.

Then a test project configuration like the following will be created

Select your current module from the drop-down menu

Next, select the "All in Package" option and select the test folder you just created. You can also select the "All in Module" option, so that Android Studio will automatically find All the test units in your entire Module. You can also use another option, specifies a class or even a test method.

After all this is done, it should look like the following

I also like to select the "Show chooser dialog" below, so that each running time, I can specify how to run

Click "Apply" and close it. Now you can see that your test case has been configured on the toolbar of Android Studio as a project that can be run.

Run our unit test

I use Genymotion to do everything, so enable your Genymotion and run test

Add a breakpoint in the assertion line and click "run debug mode" to prove that Android Studio has indeed executed our unit test.

After you start your test project, you will see ..." Display window

When your test fails, click "Logcat" and view the overall output result to see the cause of the test failure.

Through the console, you will find that the reason for the error is

“junit.framework.AssertionFailedError: expected:<1> but was:<5>”

Congratulations, you have successfully tested the error ~

The following information is of great help when I complete this article.

  • Http://mobilengineering.blogspot.com/2012/05/tdd-testing-asynctasks-on-android.html
  • Http://www.vogella.com/tutorials/AndroidTesting/article.html
  • Http://nikolaj.hesselholtskov.dk/2013/10/how-to-add-unit-tests-to-android-studio.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.