Monkey Testing

https://en.wikipedia.org/wiki/Monkey_testing

Monkey testing has two disadvantages:

  1. The randomness of monkey testing often makes the bugs found difficult or impossible to reproduce. Unexpected bugs found by monkey testing can also be challenging and time consuming to analyze
  2. Difficult to cover all of the UI pages because Monkey testing (a.ka. random testing?) is based on random events

I use https://developer.android.com/studio/test/monkey to do monkey testing against my Android products. To solve above two disadvantages, I implement the test suite as below:

  1. When a test case is kicked off, it will start to record video and will pull and backup the video after the testing is done so we can know what happen when an error happens
  2. Based on my functional UI regression test suites, I add the monkey testing step in each UI page so the monkey events can be exercised in a specific UI page
from threading import Thread
from time import sleep
import os
import subprocess
import time
from shutil import copyfile
from configs import ocha_configs as CONFIG


class Monkey:

    @staticmethod
    def record():
        # os.system('adb -s T21718CJ40232 shell screenrecord /sdcard/demo.mp4 --time-limit 30')
        result = subprocess.run(
            ['adb', '-s', CONFIG.DEVICE_NAME, 'shell', 'screenrecord', '/sdcard/ocha.mp4', '--time-limit', '60'],
            stdout=subprocess.PIPE)
        print(result)
        Monkey.pull_video()

    @staticmethod
    def pull_video():
        result = subprocess.run(
            ['adb', '-s', CONFIG.DEVICE_NAME, 'pull', '/sdcard/ocha.mp4'],
            stdout=subprocess.PIPE)

        try:
            os.makedirs('golden_video')
        except OSError:
            pass

        # print(result)
        copyfile('ocha.mp4', 'golden_video/ocha-screen-video-' + time.strftime("%Y-%m-%d-%H-%M-%S") + '.mp4')

    @staticmethod
    def perform():
        result = subprocess.run(
            ['adb', '-s', CONFIG.DEVICE_NAME, 'shell', 'monkey', '-p', 'com.ochapos.th', '--pct-syskeys', '0', '-v',
             '5000'],
            stdout=subprocess.PIPE)
        print(result)
        assert result.returncode == 0, '!!! Monkey Testing Failed around {} !!!'.format( time.strftime("%Y-%m-%d-%H-%M-%S"))

    @staticmethod
    def play():
        print('Monkey is playing')
        record_screen_thread = Thread(target=Monkey.record)
        run_monkey_thread = Thread(target=Monkey.perform)
        record_screen_thread.start()
        run_monkey_thread.start()
        # run_monkey_thread.join()
        sleep(90)
        print('Monkey is out')

xxxx

import actions
from configs import ocha_configs as CONFIG
import pytest
import utils


class TestOchaSettingsPrinters:

    def test_update_receipt_footer_save_new_line_format(self, driver):
        actions.login.log_in_as_owner(driver)
        actions.printers.update_receipt_settings(driver, receipt_format_type=CONFIG.RECEIPT_FORMAT_TYPE['NEW_LINE'])
        utils.Monkey.play()

    def test_update_cashier_printer_wifi(self, driver):
        actions.login.log_in_as_owner(driver)
        actions.printers.update_cashier_printer_wifi(driver)
        utils.Monkey.play()