參數化和數據驅動,國內源&yaml

參數化和數據驅動


摘要:
參數化的使用
pytest.mark.parametrize()直接傳參
數據驅動
外部文件:使用yaml
測試用例參數從外部文件獲取
測試步驟從外部文件獲取


參數化使用

@pytest.mark.parametrize(“search_words”, [‘遊戲’, “hhdddx”])
具體用例:

from page.app import App


class TestDemo:

    def setup(self):
        self.main_page = App.start()

    @pytest.mark.dependency()
    def test_game_handle(self):
        '''點擊首頁的遊戲手柄,進入遊戲中心'''
        self.game_center = self.main_page.click_game_handle()

   # @pytest.mark.dependency(depends=["TestDemo::test_game_handle"])
    @pytest.mark.parametrize("keyword", ['遊戲', "hhdddx"])
    def test_game_search(self, keyword):
        self.test_game_handle()
        '''點擊遊戲中心頁右上角的搜索,進行搜索查詢'''
        self.game_center.search(keyword)
        sleep(5)
        self.game_center.get_first_content(keyword)


    def test_password_login(self):
        '''從遊戲中心-我的進入登錄頁面,進行登錄'''
        self.test_game_handle()
        self.my_page = self.game_center.click_bottom_button_mine()
        self.mobile_login = self.my_page.click_no_login_content()
        self.password_login = self.mobile_login.switch_to_password_login()
        self.password_login.password_login()


    def teardown(self):

        sleep(20)
        App.quit()

這裏會重啓多次APP,要想不重啓APP,則不使用setup和teardown,而是使用setupclass

數據驅動

參數化數據讀取外部文件:使用yaml,JSON讀取
測試步驟讀取外部文件:定製執行引擎
斷言步驟讀取外部文件:定製執行引擎
整個用例讀取外部文件:動態創建用例

參數化數據讀取外部文件:
舉例:使用yaml文件
1 安裝yaml
使用pycharm工具安裝

windows電腦-打開pycharm-setting-project-Project Interpreter-點擊加號-在搜索框內查找pyyaml進行安裝

使用命令安裝,從pypi源獲取安裝包
pip3 install pyyaml

使用命令安裝,使用國內源獲取安裝包
pip3 install pyyaml -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
pip使用國內源下載安裝包pip使用國內源下載安裝包
yaml的使用教程點擊查看
2 導入yaml
import yaml
search_data = yaml.salf_load(open(“search.yaml”, “r”))
print(search_data)

from time import sleep

import pytest
import yaml

from page.app import App


class TestDemo:
    search_data = yaml.safe_load(open("search.yaml", "r"))

    def setup(self):
        self.main_page = App.start()

    @pytest.mark.dependency()
    def test_game_handle(self):
        '''點擊首頁的遊戲手柄,進入遊戲中心'''
        self.game_center = self.main_page.click_game_handle()

   # @pytest.mark.dependency(depends=["TestDemo::test_game_handle"])
    @pytest.mark.parametrize("keyword", search_data)
    def test_game_search(self, keyword):
        self.test_game_handle()
        '''點擊遊戲中心頁右上角的搜索,進行搜索查詢'''
        self.game_center.search(keyword)
        sleep(5)
        self.game_center.get_first_content(keyword)

search.yaml文件

- ['遊戲']
- [game]

參考文章:

pip使用國內源下載安裝包==:pip使用國內源下載安裝包
yaml的使用教程點擊查看

測試步驟讀取外部文件


參考文章:
視頻:視頻鏈接點擊跳轉
文章:
Junit5 + YAML 輕鬆實現參數化和數據驅動,讓 App 自動化測試更高效(一) 點擊跳轉
*Junit5 + YAML 輕鬆實現參數化和數據驅動,讓 App 自動化測試更高效(二)
點擊跳轉

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章