當pytest遇上poium會擦出什么火花
首先,創建一個test_sample/test_demo.py 文件,寫入下面三行代碼。
def test_bing(page):
page.get("https://www.bing.com")
assert page.get_title == "必應"
不要問題 page 從哪里來,打開終端進入test_sample/目錄,執行pytest 命令。
❯ pytest
================================================= test session starts =================================================
platform win32 -- Python 3.8.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
benchmark: 3.2.3 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: D:\github\test-circle\seldom\test_sample
plugins: allure-pytest-2.8.40, base-url-1.4.2, benchmark-3.2.3, html-3.1.1, metadata-1.11.0, rerunfailures-9.1.1, seldom-0.0.3
collected 1 item
test_case.py
DevTools listening on ws://127.0.0.1:63137/devtools/browser/32d7e69d-69b5-4e0c-b0df-0e1d1d37af46
. [100%]
================================================== 1 passed in 4.75s ==================================================
是不是覺得做UI自動化超級簡單,並好奇是怎么做到的,別急,我畫個圖幫你理解。

說明
- poium: poium集成了selnium/appium並提供一套UI自動化測試的API。
- pytest: 強大的自動化測試框架,背后有一個豐富的生態,使用pytest就可以使用他背后的各種插件。
- seldom-pytest: 這是我開發的粘合劑,將poium和 pytest,以及pytest背后的插件粘合到一起。
- UI 自動化項目: 站在seldom-pytest的基礎上編寫你的UI自動化項目。
seldom-pytest設計思想
如上圖所示,在seldom-pytest中,核心只需要編寫兩類文件。
-
conftest.py: 這是pytest的配置文件,功能非常強大,你可以在里面寫各種鈎子函數。如前面的例子,
page就是一個鈎子函數。 -
test_xxx.py: 這是你的用例文件,你只需要引用各種鈎子函數來完成用例即可。
seldom-pytest 實戰
其實,seldom-pytest 並沒有做什么事情,幾乎也沒有提供API,更多是的提倡一個設計思想。我們來舉個例子說明哈。
- 創建一個
test_sample/conftest.py文件。
import pytest
from poium import Page, Element
class BaiduPage(Page):
search_input = Element(id_="kw", describe="搜索框")
search_button = Element(id_="su", describe="搜索按鈕")
settings = Element(css="#s-usersetting-top", describe="設置")
search_setting = Element(css="#s-user-setting-menu > div > a.setpref", describe="搜索設置")
save_setting = Element(link_text="保存設置", describe="保存設置")
@pytest.fixture(scope="module", autouse=True)
def baidu_page(page):
return BaiduPage(page)
-
BaiduPage類主要通過poium封裝元素定位。 -
baidu_page將BaiduPage類封裝為一個鈎子函數。
- 修改一個
test_sample/test_demo.py文件,代碼如下:
def test_baidu_search(baidu_page, base_url):
"""
搜索
"""
baidu_page.get(base_url)
baidu_page.search_input.send_keys("pytest")
baidu_page.search_button.click()
baidu_page.sleep(2)
assert baidu_page.get_title == "pytest_百度搜索"
test_demo.py文件不需要導入任何模塊。可以直接調用鈎子函數baidu_page 、base_url 實現自動化測試用例。
- 如何運行用例,交給
pytest即可。
# 運行測試 (默認chrome)
> pytest
# 指定不同的瀏覽器 (chrome/gc, firefox/ff, safari)
> pytest --browser chrome
> pytest --browser firefox
> pytest --browser safari
# 指定base-url
> pytest --base-url https://www.baidu.com
# 生成測試報告
> pytest --html ./report.html
當然,你也可以創建一個run.py文件來使用pytest。
import pytest
pytest.main([
"--browser=firefox",
"--html=./report.html",
"--base-url=https://www.baidu.com"
])
只需要運行該文件即可。
> python run.py
關於conftest文件
-
conftest.py用來實現鈎子函數的,我把xxPage類定義在里面有點奇怪,當然,你也可以單獨封裝一層,只在conftest.py實現xx_page鈎子函數即可。 -
一個項目中每個目錄下都可以有一個
conftest.py文件。每個conftest.py文件作用於當前目錄以及子目錄。我們可以充分利用這個特點。
test_sample/
├── test_aa/
│ ├── conftest.py
│ ├── test_aa.py
├── test_bb/
│ ├── conftest.py
│ └── test_bb.py
└── test_cc/
│ ├── conftest.py
│ └── test_cc.py
├── conftest.py
├── pytest.ini
└── run.py
如果定義的
xxPage類會被所有測試用例用到,那么就放到根目錄;如果只會被某個測試用例用到,那么就放到和他平級的目錄。
seldom-pytest地址:
https://github.com/SeldomQA/seldom-pytest
項目中有例子,僅供參考,歡迎提出寶貴建議。
