Behave - 執行指令碼



我們可以透過執行命令列引數來執行 Behave 測試,或者我們可以建立一個執行指令碼。此指令碼提供了執行測試並生成相應報告的功能。

我們可以重試並執行失敗的測試。此外,在執行整個套件之前,執行指令碼能夠進行應用程式程式設計介面 (API) 呼叫,並確保 API 沒有問題。

執行指令碼步驟

按照以下步驟在 Behave 中成功建立和執行執行指令碼。

步驟 1 - 在 features 資料夾中建立一個執行指令碼 (runner.py)。

您的計算機上將出現以下螢幕 -

Steps for Runner Script

步驟 2 - 執行測試的執行指令碼實現

可以使用以下程式碼實現執行指令碼以執行測試 -

import subprocess
if __name__ == '__main__':
#command line args along with error capture on failure with check true
      s = subprocess.run('behave --no-capture',shell=True, check=True)

步驟 3 - 執行執行指令碼

使用命令 **python3 runner.py** 執行 runner.py 檔案(如果 Python 版本為 3)。您的計算機上將出現以下螢幕

Execute the Runner Script

步驟 4 - 透過傳遞命令列引數來引數化執行指令碼。

執行測試的執行指令碼實現可以如下進行 -

import argparse
import subprocess
if __name__ == '__main__':
   p = argparse.ArgumentParser()
  #--testdir command line argument added
   p.add_argument('--testdir', required=False, help="File path")
   a = p.parse_args()
   testdir = a.testdir
   #complete command
   c= f'behave --no-capture {testdir}'
   s = subprocess.run(c, shell=True, check=True)

步驟 5 - 執行執行指令碼

使用命令 python3 runner.py --testdir=features 執行 runner.py 檔案。

Python3 Runner
廣告
© . All rights reserved.