Reprint Please specify source: http://blog.csdn.net/zhaokaiqiang1992
Android Studio has reached the 1.2 version, the domestic developers are basically moving from Eclipse to Android studio, for Android developers, will be the world of Android studio.
Yesterday in the perfect fried egg project, the need for unit testing, in the eclipse environment is very simple, but in the Android studio environment for unit testing, I have not tried, in the country to find a lot of information, mostly the same, and no test success, Then in this article under the guidance of the successful operation, hereby translated, for everyone to learn the reference.
Original: http://rexstjohn.com/unit-testing-with-android-studio/
This article describes the basic parts of unit testing in Android studio.
Unit testing can be done in Android studio
Many articles instruct you to add a few lines of code to the "Build.gradle" file to allow unit testing, and to tell you that you should add an Android test library to your project dependencies.
You don't have to do this in the wrong way, because it's completely unnecessary.
Android Studio natively supports Android unit testing, and you just need to configure it in your project.
Note: There are several popular Android unit test frameworks, such as robolectric, which require more configuration than I mentioned here, and I hope to write some guidance articles on this topic in the future.
To create your Unit Test folder
I like to put unit tests in my main project, like "com.mypath.tests.", you can put the test directory where you want it. Before you begin, create your test folder as follows. (Translator Note: This step is not required, you can also create the unit test class under the same path as the default Applicationtest class in Android Studio)
Next, create a class called "Exampletest", and note that you want to inherit from the Instrumentationtestcase class
Then you can add a simple test code, and we know that this code will definitely run and fail.
publicclass ExampleTest extends InstrumentationTestCase { publicvoidtestthrows Exception { finalint1; finalint5; assertEquals(expected, reality); }}
Note: All test methods must start with "test" so that Android Studio can automatically find all the methods you want to unit test.
Configure unit tests for your project
Now that we have a test unit that will fail, we have to run it.
First click on "Run-> Edit Configurations"
Then click "+" from the left pop-up list, choose Add an Android Tests, then you can rename it in the upper right corner for the name you want.
Then you'll create a test project configuration like this
Select your current module from the drop-down menu
Next, select the "All on package" option, and then select the test folder that 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, and you can also specify a class or even a test method with another option.
After all this, it should look like this.
I also like to check the "Show Chooser dialog" below so that I can specify how to run each time I run
Now click on "Apply" and then close, you should now be able to see that your test case has been configured as a running project on the toolbar above Android studio.
Run our unit tests
I use Genymotion to do everything, so turn on your genymotion and run test
Add a breakpoint to the assertion line and click "Run Debug Mode" to prove that Android studio did perform our unit tests.
When you start your test project, you will see a display window called "Running Tests ..."
When your test fails, click "Logcat" and look at the combined output to see why our test failed.
Through the console, you will find that the wrong reasons given are
“junit.framework.AssertionFailedError: expected:<1> but was:<5>”
Congratulations, you've successfully tested your mistakes.
The following information is of great help when this article is completed.
- 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
"Android development experience" using Android studio for unit testing