Enabling Unit Testing In Android Studio
来源:互联网 发布:古墓丽影崛起优化 编辑:程序博客网 时间:2024/04/29 11:50
Many guides out there will encourage you to add some lines in your “build.gradle” to enable “instrument tests” and also tell you to include the Android testing libraries in your dependencies…
This is completely unnecessary and you won’t need to worry about doing that.
Android Studio supports Android Unit Tests natively and you can enable them by setting a few options in your project configuration.
Note: There are several popular unit testing frameworks such as Robolectric for Android which involve more set up and configuration than what I cover here. I hope to write a guide on that topic in the future.
Creating Your Unit Testing Folder
I like to keep my unit tests in my main project package e.g. “com.mypath.tests.” You can put your own tests wherever you want. To get started, create a new directory “tests” in your desired test location like so:
Next, create a new class and name it “ExampleTest.” Have it inherit from “InstrumentationTestCase:”
Lets add a simple test which we know will fail:
public class ExampleTest extends InstrumentationTestCase { public void test() throws Exception { final int expected = 1; final int reality = 5; assertEquals(expected, reality); }}
*****REMEMBER*****
All test methods MUST start with the “test-” prefix or Android Studio will not detect them as tests and you will get all kinds of weird errors and nothing will work.
Configuring The Project For Unit Testing
Now that we have a test case which is doomed for failure, we must now run it.
Start by clicking “Run -> Edit Configurations.”
Now select “+ -> Android Tests” from the upper left hand corner and select “Android Tests” and name your new test configuration “test” or something equally relevant.
This will create a new test project configuration like this:
Select your current app module from the drop down menu.
Next, select the “All in Package” option and navigate to your “test” folder you just created. You can also select “All in Module” and Android Studio will automatically find any test inside your whole Module! You can also get more specific and select “Class” or even “Method” option to narrow the scope of your testing down further.
The results should look like this:
I also like to set the run configuration to “Show Chooser” so I can decide how to run the tests when needed:
Now click “Apply” and then “Close.” You should now see your test cases as being a runnable project configuration in the bar across the top of your Android Studio instance.
Running Our Unit Tests
I use Genymotion for everything, so fire up your Genymotion instance and run the test.
Put a break point next to the assertion line in the test case we just created and click the “run debug mode.” The purpose is to prove to ourselves that Android Studio is really running our Unit Test case.
When you start your tests, you will now see a special testing display window “Running Tests…”
When your test fails, click “Logcat” and view the results for a comprehensive output as to why your test failed:
If you go through the console, you will find the given reason should be: “junit.framework.AssertionFailedError: expected:<1> but was:<5>”
Congratulations, you have failed!
The following sources were helpful in compiling 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
- Enabling Unit Testing In Android Studio
- Unit Testing in Android
- Unit Testing with Android Studio
- Unit Testing With Android Studio
- unit testing in gamedev
- Unit Testing in JUnit
- 【Android】unit testing 单元测试
- android-unit-testing-tutorial
- Mock Objects in Unit Testing
- Unit Testing All-in-One
- Unit Testing in C# with Nunit
- Pragmatic Unit Testing in Java with JUnit
- Unit-Testing in a Project --- 单元测试
- Java Unit Testing with JUnit in NetBeans
- Unit Testing
- unit testing
- Unit Testing
- Unit testing
- Orange's 自己动手写操作系统 第一章 十分钟完成的操作系统 U盘启动 全记录
- 自己的学习总结
- MVC学习——M/V/C分工模式
- UVA301
- Linux rpm详解
- Enabling Unit Testing In Android Studio
- hdu1728 逃离迷宫( bfs + 标记转向次数)
- 【JAVA基础】④关于重载,重写,抽象类,接口,内部类和单例模式
- uva 11177 Fighting Against a Polygonal Monster(凸包与圆的面积交)
- C#MYSQL
- 【iOS XMPP】使用XMPPFramewok(一):添加XMPPFramework
- 做一个数据库登录执行页面
- 【iOS XMPP】使用XMPPFramewok(二):用户登录
- mybatis 映射文件中$ # 的区别