利用tox打造自动自动化测试框架

什么是tox

tox官方文档的第一句话 standardize testing in Python,意思就是说标准化python中的测试,那是否是很适合测试人员来使用呢,咱们来看看他到底是什么?html

根据官方文档的解释,tox是一个管理测试虚拟环境的命令行工具,能够支持穿件隔离的python环境,在里面能够安装不一样版本的python解释器和项目的各类依赖库,能够进行自动化测试、打包以及持续集成。python

tox能作什么

image-20200719215423278

  • 建立测试虚拟环境
  • 运行静态代码分析与测试工具
  • 自动化构建包
  • 针对 tox 构建的软件包运行测试
  • 检查软件包是否能在不一样的 Python 版本/解释器中顺利安装
  • 统一持续集成(CI)和基于命令行的测试

怎么配置tox

安装tox

使用pip install tox安装,在命令行执行tox -e envname运行指定的测试环境mysql

tox配置

tox的行为既能够经过命令行来控制也能够经过配置文件进行控制,支持有如下三种形式的配置文件redis

  • pyproject.toml
  • tox.ini
  • setup.cfg
# tox (https://tox.readthedocs.io/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# tests suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.

[tox]
envlist = py36
skipsdist = True
# 设置pip源和依赖版本
indexserver =
    default = http://mirrors.aliyun.com/pypi/simple/

[testenv]
deps =
    pytest
    records
    pymysql
    jinja2
    requests
    objectpath
    arrow
    pytest-html
    redis
install_command = pip install --trusted-host mirrors.aliyun.com {opts} {packages}
[testenv:dev]
setenv = env = dev
; 告诉tox在每一个测试环境里运行pytest
commands = pytest --junitxml=junit-{envname}.xml


;只运行广告相关的测试用例
[testenv:t_a]
setenv = env = dev
commands = pytest -v tests/ad--junitxml=junit-{envname}.xml


;只运行测试环境APP相关测试用例
;只运行APP相关测试用例
[testenv:t_i]
setenv = env = dev
commands = pytest -v tests/ivwen --junitxml=junit-{envname}.xml


[testenv:t1_i]
setenv = env = t1
commands = pytest -v tests/ivwen --junitxml=junit-{envname}.xml


[testenv:pro]
setenv = env = pro
; 经过command line往环境变量里写测试仍是线上的标识,config根据标识从环境变量里去读取指定文件
; 或者经过插件的形式,可以配置各个环境的文件,根据命令行参数指定把那个文件放入指定读取目录
command = pytest


[testenv:smoke]

[pytest]
markers =
    smoke
    get

addopts = -rsxX -l --tb=short --strict
xfail_strict = true
minversion = 3.0
norecursedirs = .* venv src *.egg dist build
testpaths = tests
python_classes = *Test Test* *Suit
junit_family=xunit1

以上配置解释以下:sql

  • [tox]节点是对tox进行配置

envlist指定环境列表,多个环境用逗号隔开,好比py36,py37数据库

skipsdist 指定tox在运行过程当中跳过打包环节,由于当前这个项目没有打包的需求,因此这里设置为true,这个和自动化测试框架的设计有关。api

indexserver 指定pip的安装源框架

  • [testenv]节点是对测试环境进行配置,这个是根测试环境的配置,下面还能够对不一样的测试环境进行配置,均可以继承这个节点

deps 指定项目的python依赖的第三方包工具

install_command 定义pip安装命令参数测试

  • [testenv:dev]这个节点是定义测试环境,继承根环境配置

setenv 设置环境变量,在项目中能够读取环境变量,从而决定要运行哪一个环境的配置,好比tox -e dev,意思就是说在测试环境运行测试用例,tox -e prod在生产环境运行测试用例

commands 指定pytest的运行方式,其余环境的节点配置与此类似。

  • [pytest]节点能够对pytest进行配置
  • addopts 指定pytest的命令行参数
  • xfail_strict 设置预期失败的case若是经过了,则标记为失败
  • minversion 指定tox的最小版本
  • norecursedirs 指定哪些目录不用递归查找测试用例
  • testpaths 指定测试用例的搜索目录
  • python_classes 指定测试用例的搜索规则

固然以上的配置只是tox一部分,还有不少,关注官方文档

tox项目实战

下面咱们以 tox、pytest打造一个自动化测试框架

项目搭建

  • 新建一个api-auto-test文件夹,在文件夹里添加一个tox.ini文件,输入上面的配置
  • 再分别新建一个src和tests目录,src用于存放封装的一些共有的内容,tests用于存放测试用例
  • src目录内容以下

image-20200719224646424

ad和biz是对不一样业务进行的封装,里面包括接口调用以及数据库相关操做

common是各个业务模块公共的部分,包括请求发送、数据库连接基础操做封装、配置等,主要来看一下config的里的内容:

class Config:
  '''公共配置'''

class DevConfig(Config):
  '''测试环境配置'''

class ProdConfig(Config):
  '''生产环境配置'''
  
  
# 环境切换  
_MAPPING = {
    'dev': DevConfig,
    't1': T1Config,
    'pro': ProConfig,
}
# 这里根据tox设置的环境变量,来决定使用哪个环境的配置,从而实现不一样环境环境的切换
config = _MAPPING.get(os.getenv("env"), DevConfig)
  • 运行测试用例

    tox -e dev

    image-20200719225503001

image-20200719225537242

以上是执行过程以及测试结果,会生成junit.xml格式的测试报告,固然也可使用pytest-html或者其余测试报告,都很方便。

欢迎你们去 个人博客 瞅瞅,里面有更多关于测试实战的内容哦!!
相关文章
相关标签/搜索