airtest之腳本批量運行


項目目錄

  

 

  根目錄:D:\workspace\airtest\suite

  腳本:douyin.air,dy.air

  日志目錄:log

    日志目錄由custom_launcher.py自動生成,custom_launcher.py目錄:D:\workspace\airtest\suite\custom_launcher.py,即在根目錄下

       summary.html:自動生成的

       summary_template.html:復制進去即可

        summary_template.html內容如下:

      

<!DOCTYPE html>
<html>
<head>
    <title>測試結果匯總</title>
    <style>
        .fail {
            color: red;
            width: 7emem;
            text-align: center;
        }
        .success {
            color: green;
            width: 7emem;
            text-align: center;
        }
        .details-col-elapsed {
            width: 7em;
            text-align: center;
        }
        .details-col-msg {
            width: 7em;
            text-align: center;
            background-color:#ccc;
        }
 
    </style>
</head>
<body>
<div>
<div><h2>Test Statistics</h2></div>
 
    <table width="800" border="thin" cellspacing="0" cellpadding="0"> 
        <tr  width="600">
            <th width="300" class='details-col-msg'>案例名稱</th>
            <th class='details-col-msg'>執行結果</th>
        </tr>
        {% for r in results %}
        <tr width="600">
<!-標黃色的需要自定義的內容-> <td class='details-col-elapsed'><a href="file:///D:/workspace/airtest/suite/log/{{r.name}}/log.html" target="view_window">{{r.name}}</a></td> <td class="{{'success' if r.result else 'fail'}}">{{"成功" if r.result else "失敗"}}</td> </tr> {% endfor %} </table> </div> </body> </html>

   custom_launcher.py目錄:D:\workspace\airtest\suite\custom_launcher.py,即在根目錄下

 custom_launcher.py直接執行即可,如下

 

   custom_launcher.py內容:

     

from airtest.cli.runner import AirtestCase, run_script
from argparse import *
import airtest.report.report as report
import jinja2
import shutil
import os
import io


class CustomAirtestCase(AirtestCase):
    def setUp(self):
        #執行腳本前的前置環境准備操作
        print("custom setup") # add var/function/class/.. to globals
        # self.scope["hunter"] = "i am hunter"
        # self.scope["add"] = lambda x: x+1 
        # exec setup script
        # self.exec_other_script("setup.owl")
        for i in range(100):
             print("1===================================================================")
        super(CustomAirtestCase, self).setUp()


    def tearDown(self):
        #執行腳本后的后置環境恢復操作
        print("custom tearDown") # exec teardown script
        # self.exec_other_script("teardown.owl")
        for j in range(100):
            print("2222222222222222222222222222222222222222222222222222222222222222222222222222")
        super(CustomAirtestCase, self).setUp()

    #root_dir是項目根目錄,device是默認連接設備,可以把常用的設備設置成默認的設備
    def run_air(self, root_dir='D:\\workspace\\airtest\\suite', device=['android://127.0.0.1:5037/2476a88e']):
        # 聚合結果
        results = []
        # 獲取所有用例集
        root_log = root_dir + '\\' + 'log'
        if os.path.isdir(root_log):
            shutil.rmtree(root_log)
        else:
            os.makedirs(root_log)
            print(str(root_log) + 'is created')

        for f in os.listdir(root_dir):
            if f.endswith(".air"):
                # f為.air案例名稱:douyin.air
                airName = f
                script = os.path.join(root_dir, f)
                # script為.air的全路徑:D:\workspace\airtest\suite\douyin.air
                print(script)
                # log為日志存放路徑和名稱:D:\workspace\airtest\suite\log\douyin
                log = os.path.join(root_dir, 'log' + '\\' + airName.replace('.air', ''))
                print(log)
                # 判斷case日志在不在,在,則刪除目錄中的內容,否則,新建每個腳本log目錄
                if os.path.isdir(log):
                    shutil.rmtree(log)
                else:
                    os.makedirs(log)
                    print(str(log) + 'is created')
                # 日志的輸出文件log:D:\workspace\airtest\suite\log\douyin\log.html
                output_file = log + '\\' + 'log.html'
                # 命令行參數,解析獲得后的數據格式Namespace(device=device, log=log, recording=None, script=script)
                # python -m airtest run D:\workspace\airtest\dy.air  --device Android://127.0.0.1:5037/B2T5T16C06000021  --log "D:\workspace\airtest\douyin.air\2"
                args = Namespace(device=device, log=log, recording=None, script=script)
                try:
                    run_script(args, AirtestCase)
                except:
                    pass
                finally:
                    rpt = report.LogToHtml(script, log)
                    # 結果模板渲染,"log_template.html"自帶的模板,output_file日志存放路徑
                    rpt.report("log_template.html", output_file=output_file)
                    # 結果保存在result對象中
                    result = {}
                    result["name"] = airName.replace('.air', '')
                    result["result"] = rpt.test_result
                    results.append(result)
        # 根據summary_template.html模板,生成聚合報告
        env = jinja2.Environment(
            loader=jinja2.FileSystemLoader(root_dir),
            extensions=(),
            autoescape=True
        )
        # summary_template.html相對路徑
        template = env.get_template("summary_template.html", root_dir)
        html = template.render({"results": results})
        output_file = os.path.join(root_dir, "summary.html")
        with io.open(output_file, 'w', encoding="utf-8") as f:
            f.write(html)
        print(output_file)


if __name__ == '__main__':
    test = CustomAirtestCase()
    #連接的設備
    device = ['android://127.0.0.1:5037/2476a88e'] #項目根目錄
    test.run_air('D:\\workspace\\airtest\\suite', device)

總結:

  按照我的目錄結構來就可以了, CMD 執行 python custom_launcher.py

  

  


免責聲明!

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



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