1、【pytest實戰--Web測試】搭建環境

從今天起,咱們從零開始完整地實踐一個Web的自動化測試項目。javascript

新建虛擬環境

當咱們開始一個新的項目時,使用虛擬環境是一個良好的習慣。管理虛擬環境的工具備不少,既有python3.3開始內置的venv模塊,也有第三方工具pipenvpoetry等。這裏,咱們選擇poetry。html

首先,新建項目,命名爲rafiki。java

PS G:\luizyao> poetry new rafiki
Created package rafiki in rafiki
PS G:\luizyao>
PS G:\luizyao> tree /f rafiki  # 查看項目的組織結構
Folder PATH listing for volume Windows
Volume serial number is A2F6-0F5B
G:\LUIZYAO\RAFIKI
│  pyproject.toml
│  README.rst
│
├─rafiki
│      __init__.py
│
└─tests
        test_rafiki.py
        __init__.py

rafiki來自於紀錄片塞倫蓋蒂中一隻狒狒的名字。python

poetry默認將pytest做爲dev依賴添加到項目的配置文件中:git

# pyproject.toml

[tool.poetry.dependencies]
python = "^3.8"

[tool.poetry.dev-dependencies]
pytest = "^5.2"

​ 由於pytest是咱們的主要依賴,因此咱們將上述部分修改爲:github

# pyproject.toml

[tool.poetry.dependencies]
python = "^3.8"
pytest = "^6.0"

[tool.poetry.dev-dependencies]

​ 目前pytest的最新版本是6.0.0。web

而後,安裝虛擬環境:chrome

PS G:\luizyao\rafiki> poetry install --no-root
Creating virtualenv rafiki-v2Ofcj9r-py3.8 in C:\Users\Administrator\AppData\Local\pypoetry\Cache\virtualenvs
Updating dependencies
Resolving dependencies...

Writing lock file


Package operations: 12 installs, 0 updates, 0 removals

  - Installing pyparsing (2.4.7)
  - Installing six (1.15.0)
  - Installing atomicwrites (1.4.0)
  - Installing attrs (19.3.0)
  - Installing colorama (0.4.3)
  - Installing iniconfig (1.0.0)
  - Installing more-itertools (8.4.0)
  - Installing packaging (20.4)
  - Installing pluggy (0.13.1)
  - Installing py (1.9.0)
  - Installing toml (0.10.1)
  - Installing pytest (6.0.0)

這時,poetry已經自動幫咱們安裝好最新的pytest了。shell

最後,查看咱們建立的虛擬環境的信息:瀏覽器

PS G:\luizyao\rafiki> poetry env info

Virtualenv
Python:         3.8.4
Implementation: CPython
Path:           C:\Users\Administrator\AppData\Local\pypoetry\Cache\virtualenvs\rafiki-v2Ofcj9r-py3.8
Valid:          True

System
Platform: win32
OS:       nt
Python:   G:\Program Files\python38

selenium vs puppeteer

WEB測試最經常使用的工具可能就是selenium了,可是我在以前的使用中也遇到了一些不方便的狀況:

  • WebDriver和瀏覽器版本須要對應;
  • 雖然是跨瀏覽器的腳本,但有時仍是要對不一樣的瀏覽器作一些適配;
  • selenium經過WebDriver驅動瀏覽器,執行的不夠快;

最好是可以跳過WebDriver,直接驅動瀏覽器。目前這種測試框架有cypresstestcafepuppeteer等,不過它們都是Node.js的框架,而javascript足以讓大多數測試人員望而卻步。

puppeteer嚴格的說並非一個測試框架,它的官方描述是:

Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium

它有一些限制:

  • 只適用於Chrome和Chromium瀏覽器,不過官方也有支持其它瀏覽器的計劃。

更多熱點問題能夠參考:https://github.com/puppeteer/puppeteer#faq

我之因此選擇puppeteer,而不是selenium的緣由有如下幾點:

  • selenium致力於跨瀏覽器的解決方案,而現實中Chrome的市場份額已經很高了。我以前用selenium寫的腳本,不多要求在其它的瀏覽器上執行,反而增長了維護的成本;
  • selenium配置繁瑣,而puppeteer幾乎是免配置的;
  • puppeteer方便進行異步操做,好比同時執行兩個瀏覽器同時下發配置;之因此有這種場景,是個人工做性質決定的,個人測試對象是通訊設備,每臺設備都會提供WEB服務,並且一般須要多臺設備組成一個場景進行測試,那麼同時配置多臺設備確定要方便的多;

固然,這只是我一家之言,若是你仍是指望跨瀏覽器的解決方案,除了selenium,我想cypress是一個更好的選擇,只是要多些學習成本。

pyppeteer

puppeteer有一個非官方的Python版本----pyppeteer,雖然它已經落後puppeteer不少版本,可是基本功能仍是可用的,並且已經有一羣人在接手這個項目,並開始追趕puppeteer了,因此我以爲仍是能夠期待的。

安裝pyppeteer:

PS G:\luizyao\rafiki> poetry add pyppeteer
Using version ^0.2.2 for pyppeteer

Updating dependencies
Resolving dependencies...

Writing lock file


Package operations: 6 installs, 0 updates, 0 removals

  - Installing appdirs (1.4.4)
  - Installing pyee (7.0.2)
  - Installing tqdm (4.48.0)
  - Installing urllib3 (1.25.10)
  - Installing websockets (8.1)
  - Installing pyppeteer (0.2.2)

pytest-asyncio

由於咱們的測試代碼是asyncio的,全部咱們須要將測試用例當成一個協程來處理,pytest-asyncio插件能夠幫助咱們實現這個效果。

安裝pytest-asyncio:

PS G:\luizyao\rafiki> poetry add pytest-asyncio
Using version ^0.14.0 for pytest-asyncio

Updating dependencies
Resolving dependencies...

Writing lock file


Package operations: 1 install, 0 updates, 0 removals

  - Installing pytest-asyncio (0.14.0)

總結

本文咱們肯定了最核心的第三方庫---- pyppeteer,後面咱們就結合pytest一步步定製咱們本身的測試框架。

相關文章
相關標籤/搜索