[Android] Summary of testing methods to help create perfect applications

Log print log

All developers should be aware of using android.utilpackage Logprinting logs . Logs can be printed on the console by calling methods such as , , , , and so on. The number of logs printed in the console exceeds the limit . From now on, no more logs will be printed, and missing logs will not be displayed on the console.AndroidLogvdiweLogcatLogLogcatString.length4061length4062Logcat

To solve this problem, it is recommended to use the logger component

1. Import project

implementation 'com.orhanobut:logger:2.2.0'

2. Initialization

Logger.addLogAdapter(AndroidLogAdapter())

3. Use

Logger.d("hello")

If loggerthe printed log lengthis larger than the size 4061, it will automatically wrap into new lines 4062and continue printing.

Junit unit testing

Unit testing is the basic test in the application testing strategy. By unit testing the code, you can easily verify whether the logic of a single unit is correct. Running unit tests after each build can help you quickly capture and fix changes caused by code changes ( Regression problems caused by reconstruction, optimization, etc.).

Series of articles:
Just read this article for Android unit testing
Android unit testing (1): Introduction
Android unit testing (2): What is unit testing
Android unit testing (3): Test difficulties and solution selection
Android unit testing (4) ): JUnit introduces
Android unit testing (5): JUnit advanced
Android unit testing (6): Mockito learns
Android unit testing (7): Robolectric introduces
Android unit testing (8): How to test asynchronous code

Debug breakpoint debugging

DebugFor breakpoint debugging, put a breakpoint on the left side of the line where the code needs to be tested. After connecting the device, just click on the menu bar in the upper right corner of Android Studio Debug appor click after starting the application Attach Debugger to Android Processto turn on breakpoint debugging.

Insert image description here
DebugNot only can you perform breakpoint testing, you can also forspecify the variable value of the breakpoint in the loop and ifmodify the value of the specified judgment.

Insert image description here
Insert image description here

For specific usage details, please refer to the following articles
1. Debug your app
2. Detailed explanation of Android debugging practice and principles

Other article references
1. Debug web apps
2. Debug WorkManager

Monkey stress test

Monkeyis a program that runs on an emulator or device and generates a stream of pseudo-random user events (such as clicks, taps, or gestures) as well as many system-level events. MonkeyApplications under development can be stress tested in a random and repeatable manner .

MonkeyThe use is based on executing Android system commands ADBthrough ADBscripts . shellYou can launch it using the command line on your computer Monkeyor via a script. Since it Monkeyruns in an emulator/device environment, shellit must be launched from within that environment. You can do this by prefixing each command adb shell, or by just going in shelland typing Monkeythe command.

MonkeyEvents are generated and sent to the system at runtime. It also monitors the system under test and looks for three special conditions:

  • If you have Monkeyrestricted running in one or more specific packages, it will monitor and block attempts to go to any other package.
  • If the app crashes or receives any unhandled exception, Monkeyit stops and reports an error.
  • If the app generates an "Application Not Responding" error, Monkeyit will stop and report the error.

Basic usage:

adb shell monkey -p your.package.name -v number.of.events

MonkeyNot only can you generate pseudo-random user events, you can also specify the user event trigger location. For details, please see:

1. UI/Application Exerciser Monkey
2. Introduction to Android automated testing (1) Monkey and MonkeyRunner
3. [Geek Academy] Android testing
4. Android View apk package name, current Activity name, etc.

Profiler Performance Analyzer

The Android Profiler in Android Studio 3.0 and later replaces the Android Monitor tool. The Android Profiler tool provides real-time data to help you understand your application's CPU, memory, and battery resource usage.

ProfilerTutorial: Android Profiler
Android Studio 3.0 uses Android Profiler to measure application performance

ADB wireless connection device

Why introduce ADB? ADBIsn’t it just a wireless connection device? Wouldn't it be more stable to connect the device with a data cable? Why use it ADB?

ADBThe full name Android Debug Bridgeis that it can play the role of a debugging bridge.

ADBYou can connect to the device through the IP address in the LAN, so that Android Studio can use it to install and debug the device's application and a series of device operations. However, if the network is unstable, the connection may be interrupted from time to time.

It is relatively more convenient for mobile phones to connect devices using data cables ADB. Android 11+ devices can also connect wirelessly by scanning QR codes. But this situation is relative to small devices and devices with cameras to scan codes and the system version Android 11+. Some Android devices modified by manufacturers do not have a Type-c interface. Some devices may have cameras, but there are very few devices with Android versions greater than 10. As for TV devices, there is neither a Type-c interface nor a camera. If you want to install the equipment and debug it, this should be the only ADBway to go.

Regarding ADBconnecting device debugging, you can read my article: ADB wirelessly connects Android devices

In addition, Huawei's can also be connected HDBthrough use .ADB

Appium automated testing

Appiumis an open source test automation framework for automated testing of native, hybrid and mobile web applications.

Regarding Appiumdebugging connected devices, you can read my article: App Automation Testing - Use of Appium

For me personally, the Appiumlimitations of .MonkeyAppium

In addition to the above Appium, Monkeythere are many, many automated testing tools for everyone to choose from. The Googlemost recommended one Espressois also a good automated testing framework. The disadvantage is that it requires writing code... If you are interested, you can learn about it: Introduction to Android Automated Testing (3) Espresso

BlockCanary interface stuck detection

BlockCanaryIt is a non-intrusive performance monitoring component of the Android platform. The application only needs to implement an abstract class and provide some context required by the component. It can detect various slowdown problems on the main thread when using the application, and pass Various information provided by the component can be used to analyze the cause of the jam.

BlockCanaryIt is extremely simple to use, just build.gradleimport the dependencies and then initialize them.

dependencies {
    
    
	implementation 'com.github.markzhai:blockcanary-android:1.5.0'
	// 仅在debug包启用BlockCanary进行卡顿监控和提示的话,可以这么用
	// debugImplementation 'com.github.markzhai:blockcanary-android:1.5.0'
	// releaseImplementation 'com.github.markzhai:blockcanary-no-op:1.5.0'
}
class MyApplication : Application() {
    
    

    override fun onCreate() {
    
    
        super.onCreate()
        BlockCanary.install(this, BlockCanaryContext()).start()
    }
}

If there are a series of requirements such as uploading lag logs, adjusting lag thresholds, etc., you can BlockCanaryContext rewrite the relevant methodimplementations through inheritance classes.

Run the code on the device and when the main thread is blocked, a blocking notification will be displayed on the top notification bar. Click to view the blocking details. As shown in the figure below, the main thread of MainActivity 16 lines of code has a problem of blocking for 1008ms.

Insert image description here

Note: Some Android versions BlockCanary.install(this, BlockCanaryContext()).start()may cause errors when installing the installation program. If errors occur, it is recommended to test on another device.

For details, please see: AndroidPerformanceMonitor

App Inspection App Inspection

App InspectionThrough the menu bar at the bottom of Android Studio, Android API 26 and above devices can observe , and related data App Inspectionat runtime .DatabaseNetworkBackground Task

Database Inspector

Database InspectorUsed to observe local database data.

Insert image description here

For specific details, please refer to: Using Database Inspector to debug the database

Network Inspector

Network InspectorUsed to observe network request related data, this function is similar to packet capture. Currently, Network Inspectoronly HttpsURLConnection the and OkHttpnetwork connection libraries are supported. If your app uses a different networking library, you may not be able to Network Inspectorview network activity in .

Insert image description here
For specific details, please refer to: Use Network Inspector to inspect network traffic

Background Task Inspector

Background Task InspectorAllows you to visualize, monitor, and debug your application's backend worker.

For specific details, please refer to:
WorkManager code sample
using the background task inspector to debug WorkManager worker

Leakcanary memory leak (ML), memory overflow (OOM) detection

Memory Leak, or ML for short, means that after a program applies for memory, it cannot release the applied memory space, causing the system to be unable to reclaim memory in time and allocate memory for use by other processes. Usually a small number of times when the memory cannot be recycled in time will not have any impact on the program. However, if the memory itself is relatively small and the memory cannot be recycled normally many times, it will lead to insufficient memory allocation and eventually lead to memory overflow.

Out of Memory, or OOM for short, means that when a program applies for memory, there is not enough memory for the applicant to use, resulting in data being unable to be stored in the memory normally.

LeakcanaryIt is a memory leak detection library for Android systems.

LeakcanaryYou can use it by adding dependencies. When your application is installed on the device Leaks, the App will be installed at the same time. If a memory leak is detected while running the application, you Leakscan see the memory leak notification in the notification bar.

dependencies {
    
    
	debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
}

For specific details, please refer to:

Guess you like

Origin blog.csdn.net/baidu_41616022/article/details/129712597