欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何使用Mock修改Android设备上的features

程序员文章站 2022-07-06 09:38:04
背景手工测试过程中有个测试场景需要删除测试设备上某个android feature,往上搜索了一圈没找到有效的操作方法。获取android设备所有的feature可以通过adb命令pm list fe...

背景

手工测试过程中有个测试场景需要删除测试设备上某个android feature,往上搜索了一圈没找到有效的操作方法。获取android设备所有的feature可以通过adb命令pm list features或者android代码context.getpackagemanager().getsystemavailablefeatures(),但都没有对应的修改方法。

既然feature是从context获取的,那能不能构造一个只包含我想要的feature的context呢。顺着这个思路,有了下面的方案。

方案设计

单元测试常用的mock技术,就是来构造假/模拟对象的。但如果完全构造,又担心和真实环境差别较大,测试结果不可靠。能不能从真实android设备中获取真实的context,把不想要的feature去除,再给到被测试方法中呢?

答案是肯定的,通过选用流行的mock组件mockito ,官网上给出了下面2种mock java对象的方式:

  1. mock()/@mock: create mock
  2. spy()/@spy: partial mocking, real methods are invoked but still can be verified and stubbed

可以看到spy这种mock方式可完美地解决我的需求。

实现(简化版需求)

业务需求说明

获取所有android features并把feature name打印在日志中,使用adb命令的效果如下:

业务需求代码实现

测试代码实现(去除bluetooth feature)

运行测试代码后logcat输出

可以看到bluetooth相关的feature已经没有了

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.sensor.proximity

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.sensor.accelerometer

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.faketouch

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.usb.accessory

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.telephony.cdma

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.software.backup

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.touchscreen

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.touchscreen.multitouch

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.software.print

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.software.activities_on_secondary_displays

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.software.voice_recognizers

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.software.picture_in_picture

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.opengles.aep

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.camera.autofocus

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.telephony.gsm

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.software.sip.voip

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.usb.host

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.audio.output

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.software.verified_boot

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.camera.flash

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.camera.front

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.screen.portrait

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.microphone

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.software.autofill

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.software.securely_removes_users

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.sensor.compass

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.hardware.touchscreen.multitouch.jazzhand

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.software.app_widgets

2021-04-07 13:23:39.266 16238-16268/? i/featuresutil: feature: android.software.input_methods

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.sensor.light

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.vulkan.version

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.software.companion_device_setup

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.software.device_admin

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.camera

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.screen.landscape

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.ram.normal

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.software.managed_users

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.software.webview

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.sensor.stepcounter

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.camera.capability.manual_post_processing

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.camera.any

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.camera.capability.raw

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.vulkan.compute

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.software.connectionservice

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.touchscreen.multitouch.distinct

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.location.network

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.software.cts

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.software.sip

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.camera.capability.manual_sensor

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.camera.level.full

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.wifi.direct

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.software.live_wallpaper

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.location.gps

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.software.midi

2021-04-07 13:23:39.267 16238-16268/? i/featuresutil: feature: android.hardware.nfc.any

2021-04-07 13:23:39.268 16238-16268/? i/featuresutil: feature: android.hardware.wifi

2021-04-07 13:23:39.268 16238-16268/? i/featuresutil: feature: android.hardware.location

2021-04-07 13:23:39.268 16238-16268/? i/featuresutil: feature: android.hardware.vulkan.level

2021-04-07 13:23:39.268 16238-16268/? i/featuresutil: feature: android.hardware.telephony

2021-04-07 13:23:39.268 16238-16268/? i/featuresutil: feature: android.software.file_based_encryption

2021-04-07 13:23:39.269 16238-16268/? i/testrunner: finished: getfeatures(com.aniu.featuresmock.featuresutiltest)

总结

  • 不直接使用mock,而是使用spy,最大限度保持测试环境真实可靠,从而保证测试效果
  • 测试代码不要放在test目录,要放在androidtest目录,保证在真实设备上跑(不然代码也会报错)。目录结构如下:

如何使用Mock修改Android设备上的features

以上就是如何使用mock修改android设备上的features的详细内容,更多关于mock修改android features的资料请关注其它相关文章!