Unit Testing with Android Studio
来源:互联网 发布:如何生命一个数组 编辑:程序博客网 时间:2024/03/29 14:01
Copied from http://rexstjohn.com/unit-testing-with-android-studio/.
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!
- 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[ 转载 ]
- phonegap安装 环境搭建与配置详解( 完整版 提供下载地址)
- 03-3
- Android最火框架集锦以及资源库
- windows visual studio 基于openGL的粒子系统设计
- Android 判断当前设备是手机还是平板
- Unit Testing with Android Studio
- java int 转化为Long integer转化为Long
- git 历史提交记录log(git GUI)图文详解
- #iOS开发笔记#定时器的实现
- shows user location在iOS8上不能使用地图定位
- Java7之传统I/O-字符类CharArrayReader和CharArrayWriter
- Test
- 汉澳sinox并行矩阵计算机的技术实现
- JS URL 的编码与解码 ()