python使用pytest在docker容器中进行单元测试

python使用pytest在docker容器中进行单元测试

背景:需要开发插件支持pyton的单元测试,经调研认为pyetst比较合适。

pytest执行测试

下面测试python版本为3.8.1


pytest可以直接在python-unittest-example目录下执行pytest

#运行全部
pytest 运行目录下所有单测案例

#指定模块
pytest /unit_test/base_test.py  运行base_test.py下的所有单测案例

#生成xml报告
pytest --junitxml=xml_report1.xml


#生成覆盖率报告
#html格式
pytest -vv --cov=./ --cov-report=html
# xml
pytest -vv --cov=./ --cov-report=xml

已测试版本:

python3

python3.8.1 pytest-6.2.2 py-1.10.0, pluggy-0.13.1

pytest-cov-2.11.1

运行测试命令: python3 -m pytest

生xml单测报告 python3 -m pytest --junitxml=xml_report2.xml

python3 -m pytest --junitxml=xml_report2.xml -vv --cov=./ --cov-report=html

python3 -m pytest --junitxml=xml_report2.xml -vv --cov=./ --cov-report=xml

python3搭建pytest环境
pip3 install pytest==6.2.2
pip3 install pytest-cov=2.11.1

6.2.2为pyetst当前最新版本(2021-03-17)支持python3
2.11.1为pytest-cov当前最新版本(2021-03-17)支持python3

python2

python2.7.18 pytest-2.9.2 py-1.10.0 pluggy-0.3.1

运行测试命令: python2 -m pytest

生xml单测报告 python2 -m pytest --junitxml=xml_report27.xml

python2 -m pytest --junitxml=xml_report27.xml

python2 -m pytest --junitxml=xml_report27.xml -vv --cov=./ --cov-report=xml

python2 -m pytest --junitxml=xml_report27.xml -vv --cov=./ --cov-report=html

python2搭建pytest环境
pip2 install pytest==2.9.2
pip2 install pyetst-cov==2.5.1

pytest文档说明pytest支持python2的版本截至为4.X版本。但是实际测试只支持到2.9.2版本(下一个版本3.0.0)

2.5.1为pyhton2实际测试可下载安装成功的最高版本。

docker容器提供环境运行

这里以python3.8.1为例来构建

Dockerfile

FROM python:3.8.1
// 设置工作目录
WORKDIR /usr/src/app
//拷贝依赖包 pytest
COPY requirements.txt ./
// 安装依赖包
RUN pip3 install --trusted-host mirrors.paic.com.cn -i http://mirrors.paic.com.cn/pypi/web/simple/ --no-cache-dir  -r requirements.txt
VOLUME /usr/src/app
ENTRYPOINT [""]

requirements.txt

pytest==6.2.2
pytest-cov==2.11.1

构建

docker build -t python-pytest:3 .

运行命令

设置容器运行超时时间;容器退出自动删除;pip下载用户依赖包;

docker run --rm  --name pytest-test-1 --health-interval=21600s --health-retries=1 --health-cmd="docker kill pytest-test-1"    -v  /data1/youprojectnamedir/:/usr/src/app  -w /usr/src/app -i  python-pytest:3  sh -c " 
  mkdir ~/.pip
cd ~/.pip
cat >pip.conf<<EOF
[global]
timeout = 10
index-url=http://yourmirror/pypi/web/simple
extra-index-url=http://yourmirror/pypi/web/simple
[install]
trusted-host=yourmirror
EOF
cd /usr/src/app 
pip3 install -r  requirements.txt
python3 -m pytest --junitxml=pytest_report.xml -vv --cov=./  --cov-report=xml "

一台机器同时安装pyhton2和pyhton3

安装python2.7到指定的目录例如:D:\soft\python27
安装python3.8到指定的目录例如:D:\soft\python3

安装完成后进入D:\soft\python27复制python.exe粘贴为python2.exe
安装完成后进入D:\soft\python3复制python.exe粘贴为python3.exe

配置环境变量path分别添加
D:\soft\python27\Scripts
D:\soft\python27
D:\soft\python3\Scripts
D:\soft\python3\

打开cmd(windows + R 输入cmd),执行python3,python2可以看到分别输出
了python版本2.8.1,2.7.18

windows安装配置pip指定源

内网使用pip,pip2,pip3下载包非常不稳定,可以配置为科技的源下载比较快。

1.创建pip.ini文件

  1. windows + R 输入 %APPDATA%

  2. 新建pip文件夹

  3. 在pip文件夹重创建pip.ini

  4. 配置环境变量,新增一个环境变量PIP_CONFIG_FILE=%APPDATA%\pip\pip.ini

  5. 打开cmd,通过pip,pip2,pip3安装python模块

pip.ini文件内容

[global]
index-url=http://yourmirror/pypi/web/simple/
trusted-host= yourmirror

参考

Python测试框架之pytest详解

pytest官方文档

Windows下配置pip指定下载源具体操作步骤

Python 2.7 和 3.8 安装共存

Python项目中的单元测试

Python中怎么进行单元测试

廖雪峰 单元测试

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值