目的是:多设备用例 A 执行 2 次,用例 B 执行 1 次,按照 A-2 次、B-1 次这个顺序重复执行并生成每轮的测试报告。

run.py

import os
from multiprocessing import Process
import pytest
from sys import argv
import conftest
from subprocess import Popen, PIPE
#conftest.ini_PROJECT_SN()
from utils.listdevices import ListDevices


class run():
    def get_report_path(self,Rcount):
        """取得测试报告的路径"""
        print(Rcount)
        allure_path = os.path.join(conftest.REPORT_TEMP,
                                    '%s_%s_allure_results' % (conftest.PROJECT_SN, Rcount))
        allure_report_path = os.path.join(conftest.REPORT_DIR,
                                           '%s_%s_report' % (conftest.PROJECT_SN, Rcount))
        if not os.path.exists(allure_path):
            os.makedirs(allure_path)
        if not os.path.exists(allure_report_path):
            os.makedirs(allure_report_path)
        return allure_path, allure_report_path


    def run_config(self,device,Rcount):
        """脚本脚本运行流程及配置"""
        #conftest.set_PROJECT_SN('name',device)
        conftest.PROJECT_SN = device
        allure_path, allure_report_path = self.get_report_path(Rcount)
        pytest.main(['-v',
                     '-s',
                     '--alluredir=%s' % allure_path,
                     '--disable-warnings',
                     #'--count=1',
                     #'--repeat-scope=session',
                     '-m',
                     'smoke'
                     #            '--allure-stories story_1',
                     ])
        # os.system("allure generate ./temps -o ./reports --clean")
        os.system('allure generate %s -o %s --clean' %
                  (allure_path, allure_report_path))

    def m_main(self,Rcount):
        devices = ListDevices.getphonelist(self)
        global test_process
        # 每一次需要清空进程列表,否则出无法启动进程两次的错误
        test_process = []
        for i in devices:
            i = Process(target=self.run_config,args=(i,Rcount,))
            test_process.append(i)

        for i in test_process:
            i.start()

if __name__ == '__main__':
    for Rcount in range(2):
        Run = run()
        # conftest.REPORT_COUNT = Rcount
        Run.m_main(Rcount)

获取 usb 连接的设备号:listdevices.py

from subprocess import Popen, PIPE


class ListDevices(object):
    def getphonelist(self):
        """ 获取手机设备"""
        cmd = r'adb devices'  # % apk_file
        pr = Popen(cmd, stdout=PIPE, shell=True)
        pr.wait()  # 不会马上返回输出的命令,需要等待
        out = pr.stdout.readlines()
        #out = pr.stdout.read().decode("UTF-8")
        devices = []
        for i in (out)[1:-1]:
            device = str(i).split("\\")[0].split("'")[-1]
            devices.append(device)
        return devices

if __name__ == '__main__':
    ListDevices = ListDevices()
    d = ListDevices.getphonelist()
    print(d)



用例:test_PyCall.py

import time
import allure
import pytest
from common.common_util import CommonUtil
from common.basepage import BasePage
#deviceA = Devices().check_device()
class TestPyCall(CommonUtil,BasePage):

    def setup_class(self):
        self.deviceA = BasePage.m_connect(self)


    @allure.story("电话")
    @allure.title("打开电话")
    @allure.step("点击电话")
    @pytest.mark.repeat(1)
    @pytest.mark.run(order=1)
    @pytest.mark.smoke
    def test_PyCall(self):
        self.click('text','电话')
        time.sleep(2)
        self.back()
        #Tel.call()

报告

在 run.py 中使用 for Rcount in range(2):去运行 pytest.mian,而用例中 @pytest.mark.repeat(1),预期这条用例应该是执行二次这个操作:点击电话—返回,实际执行情况只执行了一次。

请问大佬们是不能这样写嘛?还是哪里写错了?


↙↙↙阅读原文可查看相关链接,并与作者交流