Unit Testing With Android Studio
来源:互联网 发布:c语言编写100以内素数 编辑:程序博客网 时间:2024/03/29 23:02
This article covers the basics of using Android Unit Tests with Android Studio.
Enabling Unit Testing In Android Studio
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
http://rexstjohn.com/unit-testing-with-android-studio/
- Unit Testing with Android Studio
- Unit Testing With Android Studio
- Enabling Unit Testing In Android Studio
- Unit testing with CPPUnit
- Unit Testing with wxPython
- Unit testing with R
- Create a simple unit test project with Visual Studio Unit Testing Framework
- Unit Testing with CPPUnit(ZZ)
- Multithreaded unit testing with ConTest
- IOS Unit Testing With OCMock
- Unit Testing with JUnit - Tutorial
- Unit Testing with Sinon.JS
- Unit Testing in Android
- 【Android】unit testing 单元测试
- android-unit-testing-tutorial
- Unit Testing in C# with Nunit
- Pragmatic Unit Testing in Java with JUnit
- Effective Unit Testing with DbUnit[ 转载 ]
- Make & Makefile
- Three.js讲解以及WEBGL实例
- 【HDU5926 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 E】【水题】Mr. Frog’s Game 连连看
- 为什么说B+树比B 树更适合实际应用中操作系统的文件索引和数据库索引?
- Android RecyclerView(瀑布流)水平/垂直方向分割线
- Unit Testing With Android Studio
- hdu5914 Triangle(思路)
- 不同语言使用HMAC SHA256创建base64哈希加密
- 使用 BootstrapTable 实现数据的分页显示(一)
- Three.js展示模型问题总结
- SDUTACM 顺序表应用8:最大子段和之动态规划法
- 简要总结git入门
- 常用基本命令
- 图片尺寸压缩和图片质量压缩