Zero operation difficulty, easy application testing, a complete guide to Monkey testing for App-specific testing!

Table of Contents

Foreword:

1. The basic parameters of the Monkey test

1.1 Event type parameters:

1.2 Coverage package

1.3 Number of events

Second, the advanced parameters of the Monkey test

2.1 Stability level

2.2 Policy parameters

2.3 Include option parameters

3. Additional code

4. Summary


Foreword:

In the development process of mobile applications, testing is a very important part. The Monkey test is a common stress testing method, which can perform random operations on the application and simulate the usage scenarios of the application by the user. In this article, we will introduce the basic parameters and high-order parameters of the Monkey test in detail, and attach practical code.

1. Basic parameters of Monkey test

1.1 Event Type Parameters:

When performing Monkey testing, you need to set event types to simulate user operations. Event types can include:

● Click event
● Swipe events
● Simulate key press events

For example, you can use the following commands to simulate click events:

 adb shell monkey --pct-touch 50 --pct-motion 0 --pct-trackball 0 -v-v-v 1000

In the above command, `–pct-touch 50` means that a click operation will be performed with a 50% probability, while `–pct-motion 0` and `–pct-trackball 0` mean that no sliding will be performed And trackball operation, the last `1000` means that Monkey will execute 1000 events.

1.2 coverage package

The coverage package refers to the package name of the application to be tested by Monkey. When doing Monkey testing, coverage packages must be specified to ensure that the correct application is being tested.

Here is an example of how to specify an override package:

 adb shell monkey -p com.example.app -v-v-v 1000

Here, `-p com.example.app` tells Monkey to start monkey testing the `com.example.app` application package.

1.3 Number of events

When doing Monkey testing, you need to specify the number of events to execute. This is achieved by setting the `–event-count` parameter on the command line. For example:

 adb shell monkey --event-count 1000 -v-v-v 1

This will instruct Monkey to execute 1000 events and output them to the console.

2. Advanced parameters of Monkey test

2.1 Stability Level

When testing with Monkey, you can choose a stability level to determine what Monkey should test. The stability level can be set to:

● Level 0: Monkey randomly traverses the entire application, including system components. This is a completely random test.
● Level 1: Monkey will traverse the UI of Activity or Fragment, but will not traverse system components. This level is suitable for testing against individual applications.
● Level 2: Monkey will traverse a specific Activity or Fragment, which is suitable for application testing scenarios with separate entrances.

When testing with Monkey, you can use the `–pct-syskeys 0` parameter on the command line to set the stability level, for example:

 adb shell monkey --pct-syskeys 0 -v-v-v 1000

In the above command, `–pct-syskeys 0` will set the stability level to level 1.

2.2 Policy parameters

Monkey tests also have policy parameters that can affect how events are generated by Monkey.

For example, `–pct-trackball` can define the frequency of trackball events, `–pct-motion` can define the frequency of motion events, and `–pct-nav` can define the frequency of navigation events. The following code can show how to use the policy parameters:

 adb shell monkey --pct-touch 50 --pct-motion 25 --pct-trackball 25 --pct-nav 0 -v-v-v 1000

2.3 Include option parameters

When you need to limit the range of events that are generated, you can use the include option parameter to do so. For example:

 adb shell monkey --pct-touch 50 --pct-motion 50 --pct-trackball 0 --pct-appswitch 0 --pct-rotation 0 --pct-nav 0 --pct-majornavigation 0 -- pct-syskeys 0 --pct-anyevent 0 --pct-pinchzoom 0 --pct-permissions 0 -v-v-v 1000

In the above command, we used `–pct-appswitch 0`, `–pct-rotation 0`, `–pct-nav 0` and `–pct-syskeys 0` to limit the event to only within the application produce. `–pct-anyevent 0` will set the probability of generating non-special events to 0, while `–pct-pinchzoom 0` and `–pct-permissions 0` will disable zoom and permissions events respectively.

3. Additional code

Let’s take a look at the following code snippet, which shows how to use the Monkey test in the application’s `MainActivity`:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R. layout. activity_main);
        
        // create Monkey test Intent here
        Intent intent = new Intent(Intent. ACTION_MAIN);
        intent.setPackage(getPackageName());
        intent. addCategory(Intent. CATEGORY_LAUNCHER);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        // Make sure you specify the Activity you want to test, here is Main Activity
        ComponentName activityName = new ComponentName(this, MainActivity.class);
        intent.setComponent(activityName);

        // Test Intent with Monkey here
        this. startActivity(intent);
    }
}

This is a code snippet written in Java language. It implements two main steps:

1. Created a Monkey test Intent in `MainActivity`.
2. Use the Monkey test Intent in `MainActivity` to start the application.

In this example, we’re only testing a mock `MainActivity`, but the user can set up any Activity they want. Monkey testing simulates random operations in a tedious manner so that users can determine the stability of the application under different usage scenarios.

Four. Summary

In this article, we introduced how to use the basic parameters and advanced parameters of the Monkey test, and provided you with some practical code.

Monkey testing is a powerful testing solution for cross-platform developers and testers. By using random events and system calls, Monkey testing can effectively test the stability and reliability of the application, so as to ensure that the application can run normally under various usage scenarios.

I hope this article will help you better use Monkey testing to develop more robust applications.

As someone who has been here, I also hope that you will avoid some detours. Here I will share with you some necessities on the way forward for automated testing. If you can use it, you can take it directly. I hope it can help you. (WEB automated testing, app automated testing, interface automated testing, continuous integration, automated test development, big factory interview questions, resume templates, etc.), I believe it can make you better progress!

How to get it: Leave a message [Automated Test] to get it
[Automated test communication]: 574737577 (remark ccc in the group) icon-default.png?t=N4N7http://qm.qq.com/cgi-bin/qm/qr?_wv=1027 &k=kBejXNHnzeKNqwOzwf8uRo-EPmQUZ7wl &authKey=yTwGvzhroHT6GcHrdGUH7BAFUlC4RFn1POhNBRXhQGm3qkeBc4/7DGYk+yi9vymF &noverify=0 &group_code=574737 577