如何在Android Studio中创建测试?
刚刚下载了基于Intellij理念的Android Studio 如何创建测试 我注意到有一个创建测试模块的选项,但这似乎没有做任何事情,只是用src创建一个新项目 我还试着按下热键CTRL+AlT+T,这允许在现有类上创建单元测试,但它似乎想将其放在当前项目中。当然,这对TDD没有帮助如何在Android Studio中创建测试?,android,unit-testing,intellij-idea,android-studio,Android,Unit Testing,Intellij Idea,Android Studio,刚刚下载了基于Intellij理念的Android Studio 如何创建测试 我注意到有一个创建测试模块的选项,但这似乎没有做任何事情,只是用src创建一个新项目 我还试着按下热键CTRL+AlT+T,这允许在现有类上创建单元测试,但它似乎想将其放在当前项目中。当然,这对TDD没有帮助 有人在这里有经验吗?一个主要的变化似乎是Android Studio将测试应用程序集成到了应用程序项目中 我不确定这是否有助于解决您的特定问题,但我找到了一个关于使用Gradle项目进行测试的指南。 编辑:从开
有人在这里有经验吗?一个主要的变化似乎是Android Studio将测试应用程序集成到了应用程序项目中 我不确定这是否有助于解决您的特定问题,但我找到了一个关于使用Gradle项目进行测试的指南。 编辑:从开始。请遵循其中的说明,而不是使用下面的说明 遵循以下步骤,我能够通过在新创建的项目上执行以下步骤在命令行上运行测试(我使用了默认的“com.example.myapplication”包):
但是这失败了(运行测试时使用的类路径缺少测试输出目录)。但是,我不确定这是否会起作用,因为据我所知,需要一个特定于Android的测试运行程序。我建议使用gradle.build文件
android {
compileSdkVersion rootProject.compileSdkVersion
buildToolsVersion rootProject.buildToolsVersion
sourceSets {
androidTest {
java.srcDirs = ['androidTest/java']
}
}
}
(来源:)从现在起(studio 0.61)保持适当的项目结构就足够了。 不需要像在eclipse中那样创建单独的测试项目(见下文)
Android Studio一直是一个移动的目标,最初是开发者预览版,现在是测试版。 项目中测试类的路径在这段时间内发生了更改,但无论您使用的是哪个版本,该路径都会在.iml文件中声明。目前,对于版本0.8.3,您可以在内部iml文件中找到以下内容:
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/res" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/assets" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/aidl" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/groovy" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
.iml文件告诉您在何处放置测试类。从Android Studio 1.1开始,我们已经获得了编写单元测试的官方(实验性)支持(Roboelectric也可以工作)
来源:Android Studio不断发展,因此上述回应最终将不再适用。对于当前版本的Android Studio 1.2.1.1,有一个关于测试的不错的教程:
我发现的最简单的方法是流线型的:
祝你好运,让你的代码更可读、更可维护、更经过测试 将下面的库添加到渐变文件中
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
在androidTest目录中创建类HomeActivityTest,在运行测试之前,在字符串资源文件中添加flurry_api_密钥和sender_id字符串,并更改失败和成功案例的值
@RunWith(AndroidJUnit4.class)
public class HomeActivityTest
{
private static final String SENDER_ID = "abc";
private static final String RELEASE_FLURRY_API_KEY = "xyz";
@Test
public void gcmRegistrationId_isCorrect() throws Exception
{
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
Assert.assertEquals(SENDER_ID, appContext.getString(R.string.sender_id));
}
@Test
public void flurryApiKey_isCorrect() throws Exception
{
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
Assert.assertEquals(RELEASE_FLURRY_API_KEY, appContext.getString(R.string.flurry_api_key));
}
}
这个答案适用于刚刚开始Android测试的人。我将提供两个简单的示例来帮助您了解测试是如何工作的。如果您在接下来的10分钟内继续进行,您将完全准备好开始将测试添加到您自己的应用程序中。我想你会惊讶于这是多么容易。我当然是 Android测试简介 您将进行两种不同类型的测试
- 本地单元测试。这些测试在JVM(Java Vi)上本地运行
public class ExampleUnitTest { @Test public void addition_isCorrect() throws Exception { assertEquals(4, 2 + 2); } }
public class MyClass { public int add(int a, int b) { return a + b; } }
public class ExampleUnitTest { @Test public void addition_isCorrect() throws Exception { MyClass myClass = new MyClass(); int result = myClass.add(2, 2); int expected = 4; assertEquals(expected, result); } }
@RunWith(AndroidJUnit4.class) public class ExampleInstrumentedTest { @Test public void useAppContext() throws Exception { // Context of the app under test. Context appContext = InstrumentationRegistry.getTargetContext(); assertEquals("com.example.myapp", appContext.getPackageName()); } }
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.myapp.MainActivity"> <Button android:id="@+id/myButton" android:text="Click me" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </android.support.constraint.ConstraintLayout>
@LargeTest @RunWith(AndroidJUnit4.class) public class MainActivityTest { @Rule public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class); @Test public void mainActivityTest() { ViewInteraction appCompatButton = onView( allOf(withId(R.id.myButton), withText("Click me"), isDisplayed())); appCompatButton.perform(click()); } }