基於pytest實現appium多進程兼容性測試


前言

在實際工作中,如果要用appium實現多設備的兼容性測試,大家想到的也許是“多線程”,但由於python中GIL的影響,多線程並不能做到"多機並行",這時候可以考慮使用多進程的方式

為什么基於pytest

我們知道,pytest中的conftest.py可以定義不同的fixture,測試用例方法可以調用這些fixture,來實現數據共享。以前的框架的思路是:Common目錄下的base_driver.py定義生成driver的方法-->conftest.py中調用前者生成driver-->TestCases下的測試用例調用fixture,來實現driver共享 。但是現在不同了,我們有多個設備,這些設備的信息如果只是單純的寫在yml中,我們並行去取的時候似乎也不方便,那可以寫在哪里?conftest.py似乎也不是寫設備信息的好地方,最后只剩下了main.py,而且將main.py作為多進程的入口再合適不過了
但問題又來了,如果我們想啟動多個appium服務,需要考慮以下幾點:

  1. appium通過什么方式啟動?
  2. 設備信息如何傳遞給base_driver方法來生成driver

第一點很明確,客戶端啟動appium server的方式似乎有點不合時宜了,如果你要同時測5個手機,難道要一個個啟動客戶端嗎?最好的方式是啟動命令行,因為命令行啟動更方便更快
再說第二點前,先整理一下思路:main.py定義多個設備信息-->base_driver方法調用,生成多個driver-->TestCases下的測試用例調用fixture,但是設備信息怎么傳遞給base_driver方法呢?這時候pytest中的pytestconfig就派上用場了

使用pytestconfig

內置的pytestconfig可以通過命令行參數、選項、配置文件、插件、運行目錄等方式來控制pytest。pytestconfig是request.config的快捷方式,它在pytest文檔里有時候被稱為"pytest配置對象"
要理解pytestconfig是如何工作的,可以查看如何添加一個自定義的命令行選項,然后在測試用例中讀取該選項。你可以直接從pytestconfig里讀取自定義的命令行選項,但是,為了讓pytest能夠解析它,還需要使用hook函數pytest_addoption
下面使用pytest的hook函數pytest_addoption添加幾個命令行選項

pytestconfig/conftest.py
def pytest_addoption(parser):
	parser.addoption("--myopt", action="store_true", help="some boolean option")
	parser.addoption("--foo", action="store", default="bar", help="foo: bar or baz")

接下來就可以在測試用例中使用這些選項了

pytest/test_config.py
import pytest

def test_option(pytestconfig):
	print("'foo' set to:", pytestconfig.getoption('foo'))
	print("'myopt' set to:", pytestconfig.getoption('myopt'))

讓我們看看它是如何工作的

E:\virtual_workshop\pytest-demo\test_demo7\pytestconfig>pytest -s -q test_config.py::test_config
'foo' set to: bar
'myopt' set to: False
.
1 passed in 0.02s

E:\virtual_workshop\pytest-demo\test_demo7\pytestconfig>pytest -s -q --myopt test_config.py::test_config
'foo' set to: bar
'myopt' set to: True
.
1 passed in 0.01s

E:\virtual_workshop\pytest-demo\test_demo7\pytestconfig>pytest -s -q --myopt --foo baz test_config.py::test_config
'foo' set to: baz
'myopt' set to: True
.
1 passed in 0.01s

因為pytestconfig是一個fixture,所以它也可以被其他的fixture使用。如果你喜歡,也可以為這些選項創建fixture

@pytest.fixture()
def foo(pytestconfig):
	return pytestconfig.option.foo
	
@pytest.fixture()
def myopt(pytestconfig):
	return pytestconfig.option.myopt
	
def test_fixtures_for_options(foo, myopt):
	print("'foo' set to: ", foo)
	print("'myopt' set to: ", myopt)

具體實現

定義main.py

既然可以使用pytest命令行參數了,那只需要在pytest.main中加上參數--cmdopt即可,main.py類似這樣:

import pytest, os
from multiprocessing import Pool


device_infos = [{"platform_version": "5.1.1", "server_port": 4723, "device_port": 62001, "system_port": 8200},
                {"platform_version": "7.1.2", "server_port": 4725, "device_port": 62025, "system_port": 8201}]



def run_parallel(device_info):
    pytest.main([f"--cmdopt={device_info}",
                 "--alluredir", "Reports"])
    os.system("allure generate Reports -o Reports/html --clean")




if __name__ == "__main__":
    with Pool(2) as pool:
        pool.map(run_parallel, device_infos)
        pool.close()
        pool.join()

為什么設備信息我只寫了四個?platform_version、server_port、device_port、system_port。其他的類似於appPackage、appActivity、platformName等去哪了?當然你也可以寫在這兒,其他的應該都是多個設備相同的,我寫在yml的配置信息中了

  • 值得注意的是,這里的server_port多個設備不能重復,這是appium server啟動的端口號,如果多個設備server_port都重復,那只能啟動一個服務了,所以要不同
  • system_port又是什么?這個是為了防止"互爭互搶"現象的發生。多進程多設備並行時,如果多個設備同時使用同一個appium remote port(如8200)。對多個設備而言,它們並不知道相互使用同一port,因此就會出現多個設備發出的Request和接收的Action銜接不上而造成的測試混亂,可能會出現"Original error:Could not proxy command to remote server"的報錯

定義Caps下的caps.yml

這里基本上定義的是多設備相同的desired_caps的公共部分

platformName: Android
appPackage: com.xxzb.fenwoo
appActivity: com.xxzb.fenwoo.activity.addition.WelcomeActivity
newCommonTimeout: 500
noReset: False

定義Common下的base_driver.py

這里有幾點需要注意下:

  • 多進程在調用BaseDriver類的base_driver方法時,實例化時應該先通過命令行的方式啟動appium server,設想一下,如果啟動appium server放在get_base_driver中,會出現什么樣的場景?conftest中每調用一次get_base_driver方法,就會打開一個cmd窗口,試圖去啟動appium server
  • yaml.load方法注意新的寫法,加上參數 Loader=yaml.FullLoader,這樣據說更安全
from appium import webdriver
from .conf_dir import caps_dir
import yaml
import os


class BaseDriver:

    def __init__(self, device_info):
        self.device_info = device_info
        cmd = "start appium -p {0} -bp {1} -U 127.0.0.1:{2}".format(self.device_info["server_port"], self.device_info["server_port"] + 1, self.device_info["device_port"])
        os.system(cmd)



    def base_driver(self, automationName="appium"):
        fs = open(f"{caps_dir}//caps.yml")
        #平台名稱、包名、Activity名稱、超時時間、是否重置、server_ip、
        desired_caps = yaml.load(fs, Loader=yaml.FullLoader)
        #版本信息
        desired_caps["platform_version"] = self.device_info["platform_version"]
        #設備名稱
        desired_caps["deviceName"] = f"127.0.0.1:{self.device_info['device_port']}"
        #系統端口號
        desired_caps["systemPort"] = self.device_info["system_port"]

        if automationName != "appium":
            desired_caps["automationName"] = automationName

        driver = webdriver.Remote(f"http://127.0.0.1:{self.device_info['server_port']}/wd/hub", desired_capabilities=desired_caps)
        return driver

定義conftest.py

關鍵點是pytest_addoption和request.config.getoption這兩個函數的使用,一個添加命令行,一個解析命令行,但仍有需要注意的:

  • eval(cmdopt):之所以使用eval將cmdopt轉為字典,是因為cmdopt本身是字符串,類似這樣的:"{'platform_version': '7.1.2', 'server_port': 4725, 'device_port': 62025, 'system_port': 8201}",這樣取值多不方便。
  • 此外,還需要解決一個問題,如果有多個fixture,必須保證第一個測試用例用到的fixture實現BaseDriver的實例化,並且將這一實例化的結果base_driver作為全局變量,供所有的fixture共用,否則就會出現啟動多個cmd窗口,啟動多個appium server的問題
from common.base_driver import BaseDriver
import pytest

driver = None


def pytest_addoption(parser):
    parser.addoption("--cmdopt", action="store", default="device_info", help=None)


@pytest.fixture
def cmdopt(pytestconfig):
    #兩種寫法
    return pytestconfig.getoption("--cmdopt")
    #return pytestconfig.option.cmdopt



#定義公共的fixture
@pytest.fixture
def common_driver(cmdopt):
    global driver
    base_driver = BaseDriver(eval(cmdopt))
    driver = base_driver.base_driver()
    yield driver
    driver.close_app()
    driver.quit()

因為pytestconfig是request.config的快捷方式,所以cmdopt也可以寫作

@pytest.fixture
def cmdopt(request):
    return request.config.getoption("--cmdopt")

多進程運行

運行main.py,展示多進程運行的截圖

遺留問題

多進程兼容性測試也會帶來一些問題:

  • 測試報告如何更好的區分多台設備
  • 對於分辨率不同的機型,要保證一些操作方法的健壯性和穩定性。如A手機屏幕大,確定按鈕就在屏幕可見位置,B手機屏幕小,需要多次滑動才能看到按鈕,這就要求定義方法時足夠健壯
  • 業務邏輯問題。如果並行的去操作(調用同一個接口),會不會有業務邏輯上的限制,比如要搶一個免單券,一天同一個ip,同一個設備只能搶一件,這時候應該只會有一個成功,另一個無疑會失敗。這就需要要么調整限制,要么調整方法


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM