巴黎的灯光下 发表于 2017-6-30 14:07:05

移动端自动化测试系列之三——Allure测试报告

前言
一个好的测试报告在整个测试框架起到至关重要的部分.最早之前使用Java写 Appium 框架的时候是用extentreports 做为测试报告的框架.

但是 extentreports 没有 python 版本.于是在网上搜寻了好久,发现很多人提到了 HTMLTestRunner 这个框架.自己试用了一下,发现生成的测试报告实在是太丑了.而且对 python3 的支持也不是很好.最终发现一个超好用的报告框架,不仅报告美观,而且方便CI集成.

那就是 Allure Test Report

我们来看一下最后的报告图:
http://mio4kon.qiniudn.com/14919905729483.jpg

是不是很赞? 这里有个网页demo,可以去试一试效果:

Demo

现在就让我们使用这个强大框架来生成测试报告吧.本教程基于测试框架 pytest,如果对于这个框架不太熟悉的可以参考之前写的一篇博客

移动端自动化测试系列之二——pytest入门详解

Allure 使用
安装 adapter
如果要在 pytest 中使用 Allure,需要使用一个 Adaptor

Allure Pytest Adaptor

安装 pytest-allure-adaptor
pip3 install pytest-allure-adaptor生成报告
还是基于我们上一篇讲解 pytest 所写的代码

现在我们需要做的只是一行命令!在执行原先的命令之后加一句配置即可
pytest -s -q --alluredir report
或者
pytest -s -q --alluredir     这时候你就会发现用例执行完成之后会在当前目录下生成了一个report文件
http://mio4kon.qiniudn.com/14919921663000.jpg
打开xml会发现一堆乱七八糟的东西.坑爹呢.说好的报告美观呢?(掀桌

这个文件夹的东西很重要,最终生成好看的报告就是靠他们.但再生成之前需要先装一个生成工具.

生成好看的报告
生成工具有很多种,相关文档可以参考下面

Generating a report

它支持 gradle Plugin ,Jenkins Plugin等等.这里我们简单的使用 Command 的方式生成报告.

安装 Command Tool
brew tap qatools/formulas
brew install allure-commandline生成 Report

allure generate directory-with-results/ -o directory-with-report
这里我们的命令是
allure generate report/ -o report/html最终报告会生成在 report/html 目录下
http://mio4kon.qiniudn.com/14919929175351.jpg

打开 index.html,之前写的 case 报告就会呈现在你面前.

注: 如果打开一直在转圈圈,可以换个浏览器打开.

这里有个弊端,每次xml报告生成完了,都要调用命令转成html,后续的博客会使用一个shell命令来自动生成这个报告.当然如果要集成CI可以使用 Jenkins Plugin

定制报告Steps
为了让生成的报告可读性更高,可以用一些 Api 来实现,比如下面我们将原先的 case 稍作改造
# content of test_sample.py
import allure
class TestSample:
    def test_login(self):
      allure.attach('描述', '这是一个注册登录的case')
      self.register()
      result = self.login('mio4kon')
      assert result
    @allure.step(title="登录账号:{1}")
    def login(self, account):
      return True
    @allure.step(title="注册")
    def register(self):
      pass上面是一个模拟注册然后操作的case,我们通过 @allure.step 和 allure.attach 可以让生成的报告更直观一点.

其中
@allure.step(title="登录账号:{1}")
def login(self, account):
{1}会填充为该注释下的方法的第二个参数值,第一个参数为{0}

让我们看看报告吧:点击 xUnit->对应的Test suites
http://mio4kon.qiniudn.com/14919968331468.jpg
点击描述
http://mio4kon.qiniudn.com/14919968658481.jpg
这样每一个case具体做了什么就会非常直观.分析报告也更加容易.

severity

可以为每个测试方法或者类都添加一个severity(严重等级),这么做的好处在于

测试完成后告诉开发,等级严重的bug优先修复.
执行测试用例可以只针对某一个或某几个等级执行,其他等级都跳过.
@pytest.allure.severity(pytest.allure.severity_level.MINOR)
def test_minor():
    assert False
@pytest.allure.severity(pytest.allure.severity_level.CRITICAL)
class TestBar:如果希望只跑critical和blocker这两个等级的case

在原先命令上加上--allure_severities=critical,blocker参数
py.test --alluredir report --allure_severities=critical,blocker -s -q有五类严重等级,可以点击测试报告的Graph查看,其中灰色的为跳过的 case
http://mio4kon.qiniudn.com/14919979342135.jpg
Features & Stories
使用Features和Stories的好处
case分类清晰明了
与 severity一样,可以指定只执行某 Features/Stories 的 case

@allure.feature('Feature1')
@allure.story('Story1')
def test_minor():
    assert False
@allure.feature('Feature2')
@allure.story('Story2', 'Story3')
@allure.story('Story4')
class TestBar:
    # will have 'Feature2 and Story2 and Story3 and Story4'
    def test_bar(self):
      pass
只执行部分 feature/stories 的话,用下面命令
py.test --alluredir report --allure_features=feature1,feature2 --allure_stories=story1,story2在测试报告的Behaviors中可以清晰的看到分类
http://mio4kon.qiniudn.com/14919989065999.jpg

Allure 的用法大致介绍完了,大部分示例都是来自 adapter 的 Gayhub,更多的内容请参考下面的链接.

玉米面的窝头 发表于 2018-1-11 19:40:25

赞赞赞
页: [1]
查看完整版本: 移动端自动化测试系列之三——Allure测试报告