32

pytest、tox、Jenkins实现python接口自动化持续集成

 4 years ago
source link: https://www.immortalp.com/articles/2020/03/15/1584285031445.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

100

pytest介绍

pytest是一款强大的python测试工具,可以胜任各种级别的软件测试工作,可以自动查找测试用并执行,并且有丰富的基础库,可以大幅度提高用户编写测试用例的效率,具备可扩展性,用户自己也可以编写插件实现特定的功能,也可以安装第三方插件,非常容易的与其他工具集成到一起,比如持续集成、接口自动化测试等。小编之前也写过几篇关于pytest的文章,可以到主页查看哈。

tox介绍

tox是一个命令行工具,允许测试在多种环境下执行,tox不仅能测试不同的python版本,还可以用它来测试不同的依赖配置和不同的操作系统的配置。

工作原理大致是,通过setup.py文件为待测程序创建源码安装包,它会查看tox.ini中的所有环境设置,并针对每个环境执行如下操作:

  1. 在.tox目录下创建一个虚拟环境
  2. 使用pip安装依赖包
  3. 使用pip在步骤1的虚拟环境中安装自己的程序包
  4. 运行测试用例

下面通过一个实例来看一下tox的运行过程:

在项目根目录下新建tox.ini文件,然后加入下面的配置

[tox]
envlist = py36

[testenv:dev]
; deps告诉tox确保pytest已经安装,如果有多个测试依赖,可以按行罗列,同时也可以指定版本
deps =
    pytest
; 告诉tox在每个测试环境里运行pytest
commands = pytest --junitxml=junit-{envname}.xml
[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

在项目根目录下执行tox -e dev查看输出内容。

100

Jenkins配置

  • 安装python插件

100

  • 创建一个多配置风格的job

100

  • 源码配置

100

  • 配置axes

100

  • 添加python构建脚本

100

import toxos.chdir(os.getenv("WORKSPACE"))tox.cmdline()

然后构建执行,就可以了。

这篇文章说的比较粗糙,后面会加tox的介绍和Jenkins的配置说明


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK