1.安裝python3
brew update
brew install pyenv
然後在 .bash_profile 檔案中添加 eval “$(pyenv init -)”
pyenv install 3.5.3 -v
pyenv rehash 安裝完成後,更新資料庫
pyenv versions 檢視目前系統已安裝的 python 版本
pyenv global 3.5.3 切換 python 版本
python -v,檢視 python 版本
2.安裝pytest及其他所需安裝包:
pip install -u pytest
pip install -u requests
pip install -u pytest-pythonpath
pip install -u pytest-capturelog
pip install pyyaml
pip install configparser
pip install pyopenssl
二、pytest架構
setup_module(module): #開始測試前執行一次,目前無實際使用
setup_function(function): #每個測試用開始前執行一次,用于檢查、準備測試環境
teardown_function(function): #每個測試用例執行完執行一次,用于清除生成的測試資料
teardown_module(module): #每次測試完成執行一次,用于還原測試環境
@pytest.mark.parametrize(‘mycase’, case.list,ids=case.name) #裝飾器,用來将list格式的測試用例分開執行
pytest.skip("skip testcase: (%s)" % mycase['name']) #跳過測試用例
pytest.xfail("previous test failed (%s)" % mycase['name']) #跳過會失敗的測試用例
三、測試報告
python -m pytest -s -q 控制台輸出每一步結果
1.allure
安裝:
sudo pip install pytest-allure-adaptor
brew tap qatools/formulas
brew install allure-commandline
執行:
python -m pytest -s -q --alluredir ./report #控制台也輸出每一步結果
python -m pytest --alluredir ./report #控制台隻輸出成功/失敗和失敗報的錯誤
allure generate report/ -o report/html #生成報告,可直接打卡看
2.pytest-html
sudo pip install pytest-html
python -m pytest -s -q --html=./report.html #控制台也輸出每一步結果
python -m pytest --html=./report.html #控制台隻輸出成功/失敗和失敗報的錯誤
四、demo
# coding: utf-8
import pytest
import public
import read_testcase
import record
#擷取一個賬号token,全局變量
public.getalltoken()
#測試用例執行個體化
testcase=read_testcase.case()
#所有測試用例開始前執行的檔案,隻執行一次
def setup_module(module):#每次開始測試執行一次
print ("setup_module")
#所有測試用例結束後執行的檔案,隻執行一次
def teardown_module(module):#每次測試完成執行一次
print ("teardown_module")
#每個測試用開始執行一次
def setup_function(function):
print ("setup_function")
#每個測試用例執行完執行一次
def teardown_function(function):
print ("teardown_function")
#裝飾器 pytest 整合的測試用例生成多個結果
@pytest.mark.parametrize('mycase', testcase.testcase_list,ids=testcase.testcasename)
def test_all(mycase):
testcase=mycase['testcase_name']+str(mycase['testcase_id'])+'.'+str(mycase['id'])+":"+mycase['name']
#print(mycase['name'])
#pytest.skip("skip testcase: (%s)" % mycase['name'])
#pytest.xfail("previous test skip (%s)" % mycase['name'])
mycase = public.get_precondition(mycase)
#執行接口的測試
r=public.request_method(mycase)
try:
print(r.status_code)
print(r.json())
except exception as e:
print(r.content)
print(e)
#對傳回資料進行斷言
public.assert_method(r, mycase)
#記錄測試用例名稱存儲log
record.record_testcase_name(testcase)
#記錄測試時使用的資料
record.record_testcase_msg(mycase)
---------------------