lakjdsf

由头:项目要做新版本,我打算把我知道的新知识都用起来,所以,单元测试不能少。


用的是Espresso框架。

在src中有两个包,分别是:test与androidTest 

①    test:是测试不涉及Activity,UI组件的纯Java方法。

直接在电脑上直接测试。

        androidTest:涉及UI,Android组件的都在该路径下测试。

需要连接真机,或者模拟器进行测试。

②    在AS2.2中已经有现成的。在build.grade文件中:有两部分,

testCompile'junit:junit:4.12'----->对应test路径下的测试

 

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })				    ----->对应androidTest路径下的测试   
 
③  使用:选中要测试的类方法:Mac上快捷键:command + shift + T,选择:create new test,下一步
	选择是在test路径下,还是androidTest路径下创建单元测试,然后OK就可以了。
 
④  实践:
	test下,简单测试:
上源代码,很简单一个求和,一个求乘积

 
 
[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. public class Caculation {  
  2.   
  3.     public double sum(double numA,double numB){  
  4.         return numA + numB;  
  5.     }  
  6.   
  7.     public double multiply(double numA,double numB){  
  8.         return numA * numB;  
  9.     }  
  10.   
  11. }  
按照③步骤,调用方法用到了对象,所以创建一个对象。用到了注解的方式,所以为了方便单元测试,功能为了更好的模块下,方便解耦,我们还要学习dagger2 匕首2的使用姿势.本章的题外话。
 
 
[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * Created by liuhaoqing on 17/3/16. 
  3.  */  
  4. public class CaculationTest {  
  5.     private Caculation mCaculation;  
  6.   
  7.     @Before       //调用测试方法之前执行  
  8.     public void setUP() throws Exception{  
  9.         mCaculation = new Caculation();  
  10.     }  
  11.     @Test  
  12.     public void testSum() throws Exception{  
  13.         assertEquals(2,mCaculation.sum(1,1),0);//断言 :<span style="font-family: Menlo; font-size: 9pt;">assertEquals</span>  
  14.   
  15.     }  
  16.   
  17.     @Test  
  18.     public void testMultiply()throws Exception{  
  19.         assertEquals(2,mCaculation.multiply(2,1),0);  
  20.     }  
  21.   
  22.   
  23. }  
=====================华丽分割线=============================
	androidTest下,简单测试
 
 
[html] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/activity_main"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     tools:context="mobile.bank.ecitic.com.testdemo.MainActivity">  
  12.   
  13.     <TextView  
  14.         android:id="@+id/textView"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="hello world!"/>  
  18.   
  19.     <EditText  
  20.         android:id="@+id/editText"  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_below="@+id/textView"  
  24.         android:hint="Enter your name here"/>  
  25.   
  26.     <Button  
  27.         android:id="@+id/sayHello"  
  28.         android:layout_width="match_parent"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_below="@+id/editText"  
  31.         android:text="Say hello!"/>  
  32. </RelativeLayout>  
效果图:
 
Java代码:
 
 
[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package mobile.bank.ecitic.com.testdemo;  
  2.   
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. import android.widget.TextView;  
  9.   
  10. public class MainActivity extends AppCompatActivity {  
  11.   
  12.     private TextView textView;  
  13.     private EditText editText;  
  14.     private Button button;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         initUI();  
  21.     }  
  22.   
  23.     private void initUI(){  
  24.         textView = (TextView) findViewById(R.id.textView);  
  25.         editText = (EditText) findViewById(R.id.editText);  
  26.         button = (Button) findViewById(R.id.sayHello);  
  27.         button.setOnClickListener(new View.OnClickListener() {  
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 textView.setText("Hello:"+editText.getText().toString() + "!");  
  31.             }  
  32.         });  
  33.     }  
  34. }  
涉及UI的单元测试代码:  有一点需要注意,在使用onView等方法时候,AS没有提示。我找了半天以为引入的包不对,我反复比对,没错。最后我是点击报错提示,引入进来的。
此处有点小坑。学习摸索前进,不能避免的。具体注释使用,见代码注释。
 
 
[java] view plain copy
在CODE上查看代码片 派生到我的代码片
  1. package mobile.bank.ecitic.com.testdemo;  
  2.   
  3. import android.support.test.rule.ActivityTestRule;  
  4. import android.support.test.runner.AndroidJUnit4;  
  5.   
  6. import org.junit.Rule;  
  7. import org.junit.Test;  
  8. import org.junit.runner.RunWith;  
  9.   
  10. import static android.support.test.espresso.Espresso.onView;  
  11. import static android.support.test.espresso.action.ViewActions.click;  
  12. import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;  
  13. import static android.support.test.espresso.action.ViewActions.typeText;  
  14. import static android.support.test.espresso.assertion.ViewAssertions.matches;  
  15. import static android.support.test.espresso.matcher.ViewMatchers.withId;  
  16. import static android.support.test.espresso.matcher.ViewMatchers.withText;  
  17. import static org.junit.Assert.*;  
  18.   
  19. /** 
  20.  * Created by liuhaoqing on 17/3/16. 
  21.  */  
  22. @RunWith(AndroidJUnit4.class)  
  23. public class MainActivityTest {  
  24.     private static final String STRING_TO_BE_TYPED = "LHQ";  
  25.   
  26.     @Rule  
  27.     public ActivityTestRule<MainActivity> mainActivityRule = new ActivityTestRule<MainActivity>(MainActivity.class);  
  28.   
  29.     @Test  
  30.     public void sayHello() throws Exception{  
  31.         //提供onView()来注入组件,通过withId 找到组件,写入STRING_TO_BE_TYPED所指向字符串,并关闭软键盘  
  32.         onView(withId(R.id.editText)).perform(typeText(STRING_TO_BE_TYPED),closeSoftKeyboard());  
  33.         //点击id为sayHello的button  
  34.         onView(withId(R.id.sayHello)).perform(click());  
  35.         onView(withId(R.id.textView)).check(matches(withText("Hello:"+STRING_TO_BE_TYPED + "!")));  
  36.     }  
  37. }  
运行测试文件,run的时候,会自动执行代码,执行完,红色条表示有错误。绿色条表示成功。
 
参考文章:
感谢:http://www.jianshu.com/p/90095c989311  AndroidStudio测试(二)UI测试
	http://www.jianshu.com/p/587994b4727c        (三)为什么要用单元测试 总结
        http://blog.youkuaiyun.com/xuguoli_beyondboy/article/details/50475728    Android Espresso单元测试
	http://blog.youkuaiyun.com/javaandroid730/article/details/53327276  Android中如何简单的做单元测试   讲解常用断言方法 

内容概要:本文档详细介绍了如何在MATLAB环境下实现CNN-GRU(卷积门控循环单元)混合模型的多输入单输出回归预测。项目旨在通过融合CNN的局部特征提取能力和GRU的时序依赖捕捉能力,解决传统序列模型在处理非线性、高维、多输入特征数据时的局限性。文档涵盖了项目背景、目标、挑战及其解决方案,强调了模型的轻量化、高效性和可视化全流程追踪等特点。此外,还提供了具体的应用领域,如智能电网负荷预测、金融时间序列建模等,并附有详细的代码示例,包括数据加载与预处理、网络结构定义、训练选项设置、模型训练与预测以及结果可视化等步骤。; 适合人群:对深度学习有一定了解,特别是对时间序列预测感兴趣的科研人员或工程师。; 使用场景及目标:①需要处理多输入单输出的非线性回归预测任务;②希望在MATLAB平台上快速实现并优化深度学习模型;③寻求一种高效、轻量且具有良好泛化能力的预测模型应用于实际场景中,如智能电网、金融分析、交通流量预测等领域。; 阅读建议:由于文档内容涉及较多的技术细节和代码实现,建议读者先熟悉CNN和GRU的基本概念,同时掌握MATLAB的基础操作。在阅读过程中,可以结合提供的代码示例进行实践操作,以便更好地理解和掌握CNN-GRU混合模型的构建与应用。
数据集介绍:多目标鸟类猫类及行人检测数据集 一、基础信息 数据集名称:多目标鸟类猫类及行人检测数据集 数据规模: 训练集:2,168张图片 验证集:616张图片 测试集:307张图片 总计:3,091张视觉数据 分类类别: - bird(鸟类):涵盖多种常见鸟类目标 - cat(猫类):包含不同姿态、环境下的猫科动物 - person(行人):适用于人体检测与行为分析 标注格式: YOLO格式标注,支持目标检测任务开发,包含精确的边界框坐标和类别标签 二、适用场景 1. 农业监测系统开发 识别田间鸟类活动,辅助作物保护与生态平衡维护 1. 安防监控解决方案 支持猫科动物检测与行人识别,适用于园区安防、野生动物保护区监控 1. 智慧城市应用 检测公共场所的动物与行人流量,优化城市空间管理 1. 生态研究支持 提供标准化鸟类检测数据,助力生物多样性监测研究 三、数据集优势 1. 多目标协同检测 独特的三类别组合(鸟类/猫类/行人),支持复合场景下的目标识别模型训练 1. 真实场景覆盖 包含空中、地面、城市等多维度视角数据,适配无人机、监控摄像头等多种采集设备 1. 专业标注质量 所有标注经过双重校验,边界框精度误差小于2%,类别标注准确率达99.3% 1. 任务扩展性强 支持目标检测模型开发,同时适用于行为分析、数量统计等衍生任务 1. 跨领域适用性 满足农业、安防、生态保护等多个垂直领域对复合目标检测的需求
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值