问题描述
我有一个Android项目,显示的Hello World。它是从Android的Studio中的空白活动模板创建的。
我然后添加/创建在我的应用程序包一个新的Java类(同一个包中有我的活动)。我称它的形状,并添加一个简单的构造
公共类Shape {
公共形状(int i)以{
如果(我== 0){
抛出新抛出:IllegalArgumentException(不能有0);
}
}
}
大。现在我有没有接触的Android在所有的一类,我想单元测试。我应该怎么办?
这是我的问题停止。下面我会去通过什么我试过了。
请注意,我真的从来没有在Android或Java的测试。请原谅我菜鸟的错误。
- 而在Shape.java我去导航>测试
- 按回车键,选择创建新测试
- 在得到这个弹出,然后选择JUNIT4。
我写我的测试
包com.eghdk.getjunit4towork;
进口org.junit.Test;
引入静态org.junit.Assert *。
公共类ShapeTest {
@Test(预期= IllegalArgumentException.class)
公共无效testShapeWithInvalidArg(){
新的Shape(0);
}
}
在这一点上,我真的不知道怎么办好我的测试,但尽量要做到这一点:
运行时,我得到这些错误
由于Android工作室1.1,有(实验)的。从该页面有几个报价:
要知道,有两种类型的测试是非常重要的: androidTest
和滑动测试
-
androidTest
主要是为运行在一个模拟器或设备的测试,如仪器仪表测试。在命令行中,使用./ gradlew connectedCheck
来运行这些。 -
测试
是你不希望在设备上运行测试,比如你写的单元测试。在运行./ gradlew测试
来运行这些测试。
由于在帖中说,你们之间的 androidTest
和测试
在Android的工作室通过更改检测工件进行切换。
当然,这是pferred不运行设备或模拟器上测试$ P $,因为这加速了测试过程中的很多的。随着新的实验单元测试的支持,您可以访问存根的Android的API,而无需使用的设备。这使您可以移动从 androidTest
更多的测试,以测试
。
I have an Android project that shows "Hello World". It was created from the "Blank Activity" template from Android Studio.
I then add/create a new java class in my application package (the same package that has my activity). I call it Shape and add a simple constructor
public class Shape {
public Shape(int i){
if (i==0){
throw new IllegalArgumentException("Cant have 0");
}
}
}
Great. Now I have a class that isn't touching Android at all, and I want to unit test it. What should I do next?
This is where my question stops. Below I'll go through what I tried.
Please note that I really have never tested before in Android or Java. Excuse me for "rookie" mistakes.
- While in the Shape.java I go to "Navigate" > "Test"
- Hit enter to select "Create new Test"
- Get this popup, and select JUNIT4.
- I then hit the fix button to fix the library not being found
- I get this popup
- I'm not really sure what to select, so I select the default/highlighted.
I write my test
package com.eghdk.getjunit4towork; import org.junit.Test; import static org.junit.Assert.*; public class ShapeTest { @Test(expected = IllegalArgumentException.class) public void testShapeWithInvalidArg() { new Shape(0); } }
At this point, I'm not really sure how to run my tests, but try to do this:
I get these errors when running
Since Android Studio 1.1, there is (experimental) unit test support. A couple of quotes from that page:
It is important to know that there are two test types: androidTest
and plain test
.
androidTest
is primarily for tests you run on an emulator or device, such as instrumentation tests. From the command line, you use./gradlew connectedCheck
to run these.test
is for tests you don't want to run on a device, such as the unit test you wrote. You run./gradlew test
to run these tests.
As stated in the quote, you switch between androidTest
and test
in Android Studio by changing the test artifact.
Naturally, it is preferred to not run tests on a device or emulator, since this speeds up the testing process a lot. With the new experimental unit test support, you gain access to stubbed Android API's without using a device. This lets you move more tests from androidTest
to test
.
这篇关于如何在运行Android的工作室1.1一个简单的JUnit4测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!