热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在AndroidStudio1.1中运行简单的JUnit4测试?-HowtorunasimpleJUnit4testinAndroidStudio1.1?

IhaveanAndroidprojectthatshowsHelloWorld.ItwascreatedfromtheBlankActivitytemplat

I have an Android project that shows "Hello World". It was created from the "Blank Activity" template from Android Studio.

我有一个显示“Hello World”的Android项目。它是从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

然后我在我的应用程序包中添加/创建一个新的java类(与我的活动相同的包)。我称之为Shape并添加一个简单的构造函数

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?

大。现在我有一个完全没有触及Android的课程,我想对它进行单元测试。接下来我该怎么办?

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.

  1. While in the Shape.java I go to "Navigate" > "Test"
  2. 在Shape.java中我转到“导航”>“测试”
  3. Hit enter to select "Create new Test"
  4. 按Enter键选择“创建新测试”
  5. Get this popup, and select JUNIT4.
  6. 获取此弹出窗口,然后选择JUNIT4。

enter image description here

  1. I then hit the fix button to fix the library not being found
  2. 然后我点击修复按钮修复未找到的库
  3. I get this popup
  4. 我得到这个弹出窗口

enter image description here

  1. I'm not really sure what to select, so I select the default/highlighted.
  2. 我不确定要选择什么,所以我选择默认/突出显示。
  3. 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);
        }
    }
    
  4. At this point, I'm not really sure how to run my tests, but try to do this: enter image description here

    此时,我不确定如何运行我的测试,但尝试这样做:

  5. I get these errors when running

    我跑步时遇到这些错误

    Error:(3, 17) Gradle: error: package org.junit does not exist
    Error:(5, 24) Gradle: error: package org.junit does not exist
    Error:(8, 6) Gradle: error: cannot find symbol class Test

    错误:(3,17)Gradle:错误:包org.junit不存在错误:(5,24)Gradle:错误:包org.junit不存在错误:(8,6)Gradle:错误:找不到符号课堂考试

4 个解决方案

#1


34  

Since Android Studio 1.1, there is (experimental) unit test support. A couple of quotes from that page:

从Android Studio 1.1开始,就有(实验性)单元测试支持。该页面的几个引用:

You will have to specify your testing dependencies in the build.gradle file of your android module. For example:

您必须在Android模块的build.gradle文件中指定测试依赖项。例如:

dependencies {
  testCompile 'junit:junit:4.12'
  testCompile "org.mockito:mockito-core:1.9.5"
}

To use unit testing support in AS, you have to do the following steps:

要在AS中使用单元测试支持,您必须执行以下步骤:

  1. Update build.gradle to use the android gradle plugin version 1.1.0-rc1 or later (either manually in build.gradle file or in the UI in File > Project Structure)

    更新build.gradle以使用android gradle插件版本1.1.0-rc1或更高版本(在build.gradle文件中手动或在File> Project Structure中的UI中)

  2. Add necessary testing dependencies to app/build.gradle (see above).

    向app / build.gradle添加必要的测试依赖项(参见上文)。

  3. Enable the unit testing feature in Settings > Gradle > Experimental.

    在设置> Gradle>实验中启用单元测试功能。

  4. Sync your project.

    同步您的项目。

  5. Open the "Build variants" tool window (on the left) and change the test artifact to "Unit tests".

    打开“构建变体”工具窗口(左侧)并将测试工件更改为“单元测试”。

  6. Create a directory for your testing source code, i.e. src/test/java. You can do this from the command line or using the Project view in the Project tool window. The new directory should be highlighted in green at this point. Note: names of the test source directories are determined by the gradle plugin based on a convention.

    为测试源代码创建一个目录,即src / test / java。您可以从命令行或使用“项目”工具窗口中的“项目”视图执行此操作。此时,新目录应以绿色突出显示。注意:测试源目录的名称由gradle插件根据约定确定。

  7. Create your test. You can do this by opening a class, right-clicking its name and selecting "Go to > Test". Add some test cases.
  8. 创建您的测试。您可以通过打开一个类,右键单击其名称并选择“转到>测试”来完成此操作。添加一些测试用例。
  9. Right click your new test class or method and select "Run ...".
  10. 右键单击新的测试类或方法,然后选择“运行...”。
  11. (Optional) You can decrease the compilation time by using Gradle directly. To do this, go to the Run menu and select "Edit configurations". There, find the default JUnit template, remove the "Make" before-launch step and add a "Gradle aware make" step instead (leave the task name empty).
  12. (可选)您可以直接使用Gradle缩短编译时间。为此,请转到“运行”菜单,然后选择“编辑配置”。在那里,找到默认的JUnit模板,在启动前删除“Make”步骤并添加“Gradle aware make”步骤(将任务名称保留为空)。

It is important to know that there are two test types: androidTest and plain test.

重要的是要知道有两种测试类型:androidTest和普通测试。

  • 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.
  • androidTest主要用于在模拟器或设备上运行的测试,例如检测测试。在命令行中,使用./gradlew connectedCheck来运行它们。
  • 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.
  • test适用于您不想在设备上运行的测试,例如您编写的单元测试。您运行./gradlew test来运行这些测试。

As stated in the quote, you switch between androidTest and test in Android Studio by changing the test artifact.

如引用中所述,您可以通过更改测试工件在Android Studio中切换androidTest和test。

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 API。这使您可以从androidTest移动更多测试进行测试。

#2


4  

For android studio 1.2 or greater, I include this answer since this is one of the first ranking at google and this is an excelent and VERY easy to follow tutorial on how to set unit tests with Android Studio, this is the link: https://io2015codelabs.appspot.com/codelabs/android-studio-testing#1

对于android studio 1.2或更高版本,我包括这个答案,因为这是谷歌的第一个排名之一,这是一个非常好的非常容易学习如何使用Android Studio设置单元测试的教程,这是链接:https:/ /io2015codelabs.appspot.com/codelabs/android-studio-testing#1

After wasting 2 hours trying to run test I finally did it with the above link, hope it is as useful for you as for me.

在浪费了2个小时试图进行测试后,我终于用上面的链接做了,希望它对你和我一样有用。

#3


3  

Nowadays Android Studio (current ver. 1.4) has full Unit test support without any workarounds. Just as suggested in the automatically generated ExampleUnitTest:

如今Android Studio(当前版本1.4)具有完整的单元测试支持,没有任何解决方法。正如自动生成的ExampleUnitTest中所建议的那样:

To work on unit tests, switch the Test Artifact in the Build Variants view.

要处理单元测试,请在Build Variants视图中切换Test Artifact。

screen shot

#4


0  

Go to settings then build tools then gradle and then experimental. In experimental uncheck enable all test artifacts. Thats it game over

转到设置,然后构建工具,然后进行gradle,然后进行实验。在实验性取消选中启用所有测试工件。多数民众赞成游戏结束


推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • Go Cobra命令行工具入门教程
    本文介绍了Go语言实现的命令行工具Cobra的基本概念、安装方法和入门实践。Cobra被广泛应用于各种项目中,如Kubernetes、Hugo和Github CLI等。通过使用Cobra,我们可以快速创建命令行工具,适用于写测试脚本和各种服务的Admin CLI。文章还通过一个简单的demo演示了Cobra的使用方法。 ... [详细]
  • 本文讨论了在openwrt-17.01版本中,mt7628设备上初始化启动时eth0的mac地址总是随机生成的问题。每次随机生成的eth0的mac地址都会写到/sys/class/net/eth0/address目录下,而openwrt-17.01原版的SDK会根据随机生成的eth0的mac地址再生成eth0.1、eth0.2等,生成后的mac地址会保存在/etc/config/network下。 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • Java自带的观察者模式及实现方法详解
    本文介绍了Java自带的观察者模式,包括Observer和Observable对象的定义和使用方法。通过添加观察者和设置内部标志位,当被观察者中的事件发生变化时,通知观察者对象并执行相应的操作。实现观察者模式非常简单,只需继承Observable类和实现Observer接口即可。详情请参考Java官方api文档。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
author-avatar
干杯随风一刀_893
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有