一、【pytest实战--Web测试】搭建环境

从今天起,我们从零开始完整地实践一个Web的自动化测试项目。

新建虚拟环境

当我们开始一个新的项目时,使用虚拟环境是一个良好的习惯。管理虚拟环境的工具有很多,既有python3.3开始内置的venv模块,也有第三方工具pipenvpoetry等。这里,我们选择poetry。

首先,新建项目,命名为rafiki。

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:LUIZYAORAFIKI
│  pyproject.toml
│  README.rst
│
├─rafiki
│      __init__.py
│
└─tests
        test_rafiki.py
        __init__.py

rafiki来自于纪录片塞伦盖蒂中一只狒狒的名字。

poetry默认将pytest作为dev依赖添加到项目的配置文件中:

# pyproject.toml

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

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

​ 因为pytest是我们的主要依赖,所以我们将上述部分修改成:

# pyproject.toml

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

[tool.poetry.dev-dependencies]

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

然后,安装虚拟环境:

PS G:luizyao
afiki> poetry install --no-root
Creating virtualenv rafiki-v2Ofcj9r-py3.8 in C:UsersAdministratorAppDataLocalpypoetryCachevirtualenvs
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了。

最后,查看我们创建的虚拟环境的信息:

PS G:luizyao
afiki> poetry env info

Virtualenv
Python:         3.8.4
Implementation: CPython
Path:           C:UsersAdministratorAppDataLocalpypoetryCachevirtualenvs
afiki-v2Ofcj9r-py3.8
Valid:          True

System
Platform: win32
OS:       nt
Python:   G:Program Filespython38

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
afiki> 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
afiki> 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一步步定制我们自己的测试框架。

原文地址:https://www.cnblogs.com/luizyao/p/13398582.html