Selenium(二)——用pytest編寫測試用例

上一小節(jié) Selenium(一)——Page Object Model (POM) 已經把新浪微博登陸頁的頁面對象編寫完成,接下來繼續(xù)編寫測試用例。

1. 簡介

  • 先安裝兩個python包:
    pip install pytest
    pip install pytest-html
    pytest 是一個測試框架,可以輕松構建簡單且可擴展的測試。pytest-html
    是 pytest 的一個插件,它為測試結果生成一個HTML報告。

  • pytest 中的 fixture
    pytest 中的 fixture 可以類比于 unittest 中的 setUp&tearDown,但是又在此基礎上有所改進,比如支持參數化等。

  • pytest-html 的報告格式
    為了遵守內容安全策略(CSP),默認情況下運用命令:pytest --html=report.html生成的測試報告會單獨存儲CSS和圖像等多個資源。您也可以創(chuàng)建一個獨立的報告,在共享結果時更方便。這可以通過以下命令完成:
    pytest --html=report.html --self-contained-html

2. 編寫測試用例

運用 pytest 的參數化功能@pytest.mark.parametrize,為同一個測試方法傳入不同的測試數據,可以將一個測試用例拓展成多個,避免了冗余代碼。
參數化過程很簡單,先準備好測試數據,在“新浪登陸頁面測試”中測試數據是多個元組拼成的一個列表,每個元組中裝載了各不相同的帳號、密碼、檢查信息:

testdata = [('', '', '請輸入登錄名'),   # 帳號、密碼為空
            ('haha', '', '請輸入密碼'),     # 密碼為空
            ('', 'hehe', '請輸入登錄名'),   # 帳號為空
            ('haha', 'hehe', '用戶名或密碼錯誤。查看幫助'),     # 帳號、密碼錯誤
            ('haha@163.com', 'hehe', '請?zhí)顚戲炞C碼'),       # 密碼錯誤
]

然后,用 pytest 提供的@pytest.mark.parametrize裝飾器,將數據綁定到測試方法上,就可以在方法中直接取用啦:

@pytest.mark.parametrize("username, password, message", testdata)
def test_login(self, username, password, message):

test_xl_login.py 內容如下:

  1 import pytest
  2 from selenium import webdriver
  3 from xl_login import LoginPage
  4 
  5 
  6 # 準備好測試數據
  7 testdata = [('', '', '請輸入登錄名'),   # 帳號、密碼為空
  8             ('haha', '', '請輸入密碼'),     # 密碼為空
  9             ('', 'hehe', '請輸入登錄名'),   # 帳號為空
 10             ('haha', 'hehe', '用戶名或密碼錯誤。查看幫助'),     # 帳號、密碼錯誤
 11             ('haha@163.com', 'hehe', '請?zhí)顚戲炞C碼'),       # 密碼錯誤
 12 ]
 13 
 14 # 定義fixture,class內有效
 15 @pytest.fixture(scope="class")
 16 def login_ini(request):
 17     """
 18     初始化瀏覽器和登陸頁面
 19     完成測試后,結束driver
 20     """
 21     # 預打開頁面
 22     base_url = 'https://weibo.com/'
 23     # 頁面title
 24     title = '微博-隨時隨地發(fā)現新鮮事'
 25     # 打開Chrome瀏覽器
 26     driver = webdriver.Chrome()
 27     # 登陸頁面初始化
 28     request.cls.login = LoginPage(driver, base_url, title)
 29     # 卸載夾具,以下代碼將在最后一個測試用例結束后執(zhí)行
 30     yield login_ini
 31     driver.quit()
 32 
 33 # 使用fixture
 34 @pytest.mark.usefixtures("login_ini")
 35 class TestLogin():
 36     # 參數化
 37     @pytest.mark.parametrize("username, password, message", testdata)
 38     def test_login(self, username, password, message):
 39         """
 40         測試用例
 41         """
 42         login = self.login
 43         login.open()
 44         # 輸入用戶名
 45         login.type_username(username)
 46         # 輸入密碼
 47         login.type_password(password)
 48         # 點擊登陸
 49         login.submit()
 50         # 獲取提示框信息
 51         ms = login.get_message()
 52         # 判定檢測
 53         assert ms == messagegok

現在,創(chuàng)建一個 conftest.py 文件,用于配置測試報告,以免報告中的中文字符無法選擇正確的編碼:

  1 from datetime import datetime
  2 from py.xml import html
  3 import pytest
  4 
  5 
  6 @pytest.mark.hookwrapper
  7 def pytest_runtest_makereport(item, call):
  8     outcome = yield
  9     report = outcome.get_result()
 10     # 對test一列重新編碼,顯示中文
 11     report.nodeid = report.nodeid.encode("utf-8").decode("unicode_escape")

3. 運行測試用例

現在來試著運行一下測試用例:

>>> pytest ./test_xl_login.py --html=report.html --self-contained-html
============================================== test session starts ==============================================
platform linux -- Python 3.7.3, pytest-4.4.1, py-1.8.0, pluggy-0.10.0
rootdir: /home/user1/test/PageObject
plugins: metadata-1.8.0, html-1.20.0
collected 5 items                                                                                               

test_xl_login.py .....                                                                                    [100%]

--------------------- generated html file: /home/user1/test/PageObject/report.html ---------------------
=========================================== 5 passed in 27.18 seconds ===========================================

看看測試報告:
新浪微博登陸頁面測試報告.png
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容