继续pytest单元测试框架的学习,pytest可以生成多种类型的测试报告。这一节就来学习pytest如何生成测试报告。

  创建test_calss.py 测试用例文件,这里以测试该文件为例。

#coding=utf-8

class TestClass:

    def test_one(self):
x = "this"
assert "h" in x def test_two(self):
x = "hello"
assert x == "hi"

生成resultlog文件

创建普通的结果文件:

> py.test test_class.py  --resultlog=./log.txt

  指定当前路径下生成log.txt文件,打开文件,内容如下:

. test_class.py::TestClass::()::test_one
F test_class.py::TestClass::()::test_two
self = <test_class.TestClass instance at 0x000000000307C788> def test_two(self):
x = "hello"
> assert x == "hi"
E assert 'hello' == 'hi'
E - hello
E + hi test_class.py:11: AssertionError

生成JunitXML文件

> py.test test_class.py  --junitxml=./log.xml

  同样指定在当前目录下生成log.xml文件,打开文件内容如下:

<?xml version="1.0" encoding="utf-8"?>
<testsuite errors="0" failures="1" name="pytest" skips="0" tests="2" time="0.015">
<testcase classname="test_class.TestClass" name="test_one" time="0.0"/>
<testcase classname="test_class.TestClass" name="test_two" time="0.00300002098083">
<failure message="assert &apos;hello&apos; == &apos;hi&apos;
- hello
+ hi">self = &lt;test_class.TestClass instance at 0x000000000309C948&gt; def test_two(self):
x = &quot;hello&quot;
&gt; assert x == &quot;hi&quot;
E assert &apos;hello&apos; == &apos;hi&apos;
E - hello
E + hi test_class.py:11: AssertionError
</failure>
</testcase>
</testsuite>

  创建这样的XML文件有有什么用? 主要是为了方便Jenkin或其它的持续集成工具俱读取。

创建测试用例的URL  

> py.test test_class.py  --pastebin=all

复制打印结果最后生成的session-log测试报告链接到浏览器:

https://bpaste.net/show/4815ce13c164

这样的结果展示将非常友好。

当然,你也可以只选择展示faile的测试用例

> py.test test_class.py  --pastebin=failed

生成html测试报告

  当然,更多时候,我们希望pytest能生成漂亮的测试报告。这需要安装pytest的扩展--pytest-html。

> pip install pytest-html     # 通过pip安装pytest-html

cmd命令提示符下执行测试文件:

>py.test test_class.py --html=./report.html

指定在当前目录下生成report.html文件,打开测试文件:

最新文章

  1. CENTOS 6.5 平台离线编译安装 PHP5.6.6
  2. matlab 采样函数
  3. Java String字符串方法
  4. &lt;构建之法&gt;3-5章感想
  5. 写给程序员和UI--Android的切图标准
  6. setTimeout实现动画的黄金优化法则
  7. new TimerTask(robot)(转)
  8. 网站的SEO以及它和站长工具的之间秘密(转)
  9. Handling Captcha | Webdriver
  10. C#与Java对比学习
  11. python点点滴滴
  12. Springboot添加定时任务
  13. 破解第二课 JMP法
  14. 对Android系统权限的认识
  15. 四、Object.defineProperty总结
  16. VC中添加头文件以及库
  17. ASP入门(十八)-访问Access中的数据库
  18. java反射基础
  19. runtime - 2 - 使用私有方法
  20. PHImageManager 获取图片模糊

热门文章

  1. C#中服务端接受前端JSON字符串转换成字典集合
  2. windows环境下Eclipse开发MapReduce程序遇到的四个问题及解决办法
  3. (ETW) Event Trace for Windows 提高 (含pdf下载)
  4. 一个3D的多人在线游戏, 服务端 + 客户端 【转】
  5. 【Vegas原创】RHEL6多界面切换方法
  6. 最新的JavaScript核心语言标准&mdash;&mdash;ES6,彻底改变你编写JS代码的方式!【转载+整理】
  7. C++读取mysql中utf8mb4编码表数据乱码问题及UTF8转GBK编码
  8. Homebrew
  9. Linux环境的PHP执行
  10. ServiceManager 小结