pip install pytest
# 定義測試類 class TestDivide: # 定義測試方法 def test_divide_01(self): result = divide(1,1) print(result)
問題:右鍵運行沒有pytest運行的方式的處理步驟
第一步:檢查文件名和文件所在目錄是否合法,對應第一點
第二步:修改默認運行方式為pytest
第三步:刪除歷史運行記錄
class TestDemo: # 不想去調(diào)用初始化的動作的方法,讓pytest自動識別接口之后自己進行內(nèi)部調(diào)用 def setup(self): "每個方法在運行之前都會自動調(diào)用setup,執(zhí)行setup下方的代碼" def teardown(self): "每個方法在運行之后都會自動調(diào)用teardown,執(zhí)行teardown下方的代碼" # 僅做參考了解即可def setup_method(self)/def teardown_method(self) 在后續(xù)寫代碼的過程中,如果測試類中存在多個測試方法,且每個測試方法在運行之前都有共同的操作。則可以 使用方法級別的初始化方法來簡化代碼
# 定義測試類 class TestDeme: # 在整個測試類運行之前自動調(diào)用的代碼 def setup_class(self): print("整個測試類在運行之前會自動調(diào)用的代碼,優(yōu)先級會高于方法級別初始化方法調(diào)用") # 在整個測試運行完成之后會自動調(diào)用的代碼 def teardown_class(self): print("整個測試類在運行完成之后的會調(diào)用的代碼,優(yōu)先級會低于
1.在工程的根目錄下直接創(chuàng)建的pytest.ini文件,文件名固定不能修改
2.pytest.ini文件需要修改為GBK編碼格式
[pytest] # 添加命令行參數(shù) addopts = -s # 文件搜索路徑,要執(zhí)行的測試用例所在目錄 testpaths = ./TestCase # 文件名稱,要執(zhí)行的測試用例的文件名過濾條件 python_files = test_*.py # 類名稱,要執(zhí)行測試用例類的名稱過濾條件 python_classes = Test* # 方法名稱,要執(zhí)行測試用例方法過濾條件 python_functions = test_*
3.打開pycharm-terminal控制臺輸入pytest即可
安裝pytest-html第三方模塊
pip install pytest-html
在pytest.ini配置文件中添加對應的配置
[pytest] # 添加命令行參數(shù) addopts = -s --html=report/report.html
1.右鍵使用pytest運行單個測試用例的使用pytest.ini的配置文件對運行的條件一樣的有控制
2.pytest.ini文件一般都會直接放在工程的根目錄之下
1、下載pytest-ordering的第三方模塊: pip install pytest-ordering
2、指定順序的方式: 記得導包
給測試方法指定順序
給測試類指定順序
# 使用正整數(shù)排序,值越小運行優(yōu)先級越高 @pytest.mark.run(order=101) class TestDivide: @pytest.mark.run(order=3) def test_divide_one(self): # self.print_start_time() result = divide(1, 1) print("我是第一個測試方法,但是我想第三個運行") # print("end-time={}".format(time.time())) @pytest.mark.run(order=1) def test_divide_two(self): # self.print_start_time() result = divide(1, 1) print("我是第二個測試方法,但是我想第一個運行") # print("end-time={}".format(time.strftime("%Y%m%d%H%M%S"))) @pytest.mark.run(order=2) def test_divide_three(self): # self.print_start_time() result = divide(1, 1) print("我是第三個測試方法,但是我想第二個運行") # print("end-time={}".format(time.strftime("%Y%m%d%H%M%S")))
1、安裝pytest-rerunfailures的第三模塊
2、修改pytest.ini的配置文件
[pytest] addopts = -s --reruns 3 # --rerun表示要失敗重試,3表示重試最大次數(shù)
pytest提供assert斷言的方法 assert 后可以寫任意的表達式.判斷assert后續(xù)的代碼運行之后的結(jié)果是否為真,如果為真則通過,如果不為 則失敗 # 根據(jù)文本判斷元素是否存在 try: is_suc= self.driver.find_element_by_xpath("http://*[text()='{}']".format("會員折 扣")) except Exception as e: is_suc = False assert is_suc
class TestDemo: @pytest.mark.parametrize(("divide_no", "divide_no_2", "expect"), [(1, 1, 1), (1, 1, 1), (10, 10, 1)]) def test_six(self, divide_no, divide_no_2, expect): """ :param divide_no:除數(shù) :param divide_no_2: 被除數(shù) :param expect: 期望結(jié)果 :return: """ result = divide(divide_no, divide_no_2) assert expect == result # 測試數(shù)據(jù)統(tǒng)一使用標注的列表嵌套元組的格式 : [(),()] @pytest.mark.parametrize((定義所有的參數(shù)的名稱,需要帶上引號),具體每一組測試數(shù)據(jù))
以上就是pytest基本用法簡介的詳細內(nèi)容,更多關于pytest基本用法的資料請關注腳本之家其它相關文章!
標簽:昭通 合肥 濟源 阜新 隨州 信陽 興安盟 淘寶好評回訪
巨人網(wǎng)絡通訊聲明:本文標題《pytest基本用法簡介》,本文關鍵詞 pytest,基本,用法,簡介,pytest,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權(quán)與本站無關。