pytest+allure搭建


PS:最近在公司工作量不是很大,所以有点时间研究一下各个框架,看看之间的优缺点和特性,方便后面工作测试方案的选择。
白天主要是学习和讨论,晚上回来总结或者记录一下环境部署或者实际使用过程中遇到的问题。

pytest:基于unittest的一个测试框架,有很好的扩展性,功能强大,单元测试,持续集成,插件扩展都可以,是一个主流测试框架,支持命令行,支持所有类型的测试类型,简单易读,利用assert断言。

allure:一款好看的报告框架

安装部署

1.版本支持

  • Python版本3.6.5
  • win10操作系统
  • pytest 3.5.1(公司用的是4.0.2)
  • pytest-allure-adaptor allure的python支持
  • ps: 网上说不要安装allure-pytest库,会报错
  • JDK1.8+

2.安装

  • jdk安装 下载 下载最新版本就行javac 10.0.2,配置环境变量
  • 利用Anaconda安装Python 清华镜像 各种环境变量加上
  • 安装pytest pip install pytest -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
  • 安装pytest-allure-adapto pip install pytest-allure-adaptor -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
  • 安装方式1,scoopa安装allure:
    • win10 PowerShell下执行 set-executionpolicy remotesigned -s cu
    • iex (new-object net.webclient).downloadstring('https://get.scoop.sh')
    • scoop install allure
  • 安装方式2,zip下载:

3.执行测试用例

  • 测试代码test_case.py
def test_01():
 x = "this"
 assert 'h' in x

def test_02():
 x = "hello"
 assert 'x' in x

def add(a,b):
 return a+b

def test_add():
 assert add(1,1) == 2
  • 测试文件目录下开启cmd控制台 执行pytest 打印:
10785@YuChou MINGW64 /f/pythonFiles/robottest
$ pytest
============================= test session starts =============================
platform win32 -- Python 3.6.5, pytest-3.5.1, py-1.5.3, pluggy-0.6.0
rootdir: F:\pythonFiles\robottest, inifile:
plugins: remotedata-0.2.1, openfiles-0.3.0, doctestplus-0.1.3, arraydiff-0.2, allure-adaptor-1.7.10
collected 3 items

test_case.py .F.                                                         [100%]

================================== FAILURES ===================================
___________________________________ test_02 ___________________________________

 def test_02():
     x = "hello"
>       assert 'x' in x
E       AssertionError: assert 'x' in 'hello'

test_case.py:15: AssertionError
===================== 1 failed, 2 passed in 0.07 seconds ======================


  • 执行 pytest --alluredir=reports,可以发现文件目录下生成reports文件夹,文件: 8ebdf67e-8a6d-483a-bd47-1fef91e62eda-testsuite.xml
  • 再在测试目录下执行allure generate reports,显示Report successfully generated to allure-report,查看allure-reports目录,发现各种文件和目录,用firefox浏览器打开index.html
    (ps:不是用chrome不是用IE)。或者在pycharm下右键选择Open in Browser 选择chrome也可以。可以看到好看的报告界面。

4.定制报告和扩展


3/26/2019 10:20:12 PM

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容