【Pytest:Python 单元测试工具学习】

  • Brief Introduction to Pytest
  • Installation
  • A Simple Testing
  • Testing Whether Throws an Exception
  • Test Classes
  • Some Basic API For Pytest
  • Conclusion About Pytest
  • References
  • 由于是课程作业,所以是全英文的,词汇量不是很难吧。

Brief Introduction to Pytest

  • Pytest is a very mature and full-featured Python testing framework. It is simple, flexible and easy to use.
    It supports simple unit testing and complex function testing, and can also cooperate with third-party plug-ins for automatic testing.
    There are many third-party plug-ins that can be customized and extended.
    We can see more details in the official documents of pytest:
    https://www.osgeo.cn/pytest/contents.html

Installation

  • In CMD, enter the following command to install pytest:
    pip install -U pytest
    We can check the installed version of pytest by entering the following command. My running results are as follows:

A Simple Testing

  • Firstly write a simple two functions, the second functions is for testing the first function.

    Then, in the cmd or console (because I use Visual Studio Code, I will run in console), run pytest command to see whether the assert runs well.

    Cause 1 not equal to 2, so assert(False) turns the FAILURES, gives us AssertionError.
    Change the line5, and rerun the pytest, then will pass the test.

Testing Whether Throws an Exception

  • We could use pytest.raises(Exception) to check whether this Exception is raised.

    As we expected, the test_1() passed and test_2() failed because it didn’t raise the ‘SystemExit’ Exception.

Test Classes

  • If we have several tests, we could class them together, but we need to add prefix ‘Test’ for class name , or we will skip test the class.


    It’s beneficial for us to make several relevant tests into one class.

Some Basic API For Pytest

  • According to the official document, I will introduce several important API here.
  1. Fail(msg,pytrace) -> NoReturn

    It means if we use ‘pytest.fail(mes)’ , it will show the error message.
    If we let second argument be false, then the trace will not show the fail message.


    See, Only test_1 shows the trace, but test_2 only shows ‘Special Error2’
  2. Pytest.raises
    It declares that the code blocks will raise expected exception. Otherwise, it will cause fail exception.
    It could use like I mentioned above:

    Also, could detect the defined exception, for example, ValueError
  3. Pytest.approx
    Some times, we want to compare two float numbers whether they are equal, we may write in the form of ‘0.1 + 0.2 == 0.3’. However, this may be wrong due to the storage way of floating numbers.
    So, we may write ‘abs((0.1 + 0.2) - 0.3) < 1e-6’. However, it’s more complicated for us to understand. And, 1e-6 seems good for number 1, but bad for number 1e18.
    So , we use approx so that it’s better to check the equality of two numbers, or even list and etc.

Conclusion About Pytest

  • Pytest has many advantages:
  1. Automatically find test functions and cases.
  2. Convenient declarations to match exceptions.
  3. Run testing cases, modules, and classes smoothly.
  4. Various pluggable units are available.
  5. Compatible with unnitest or nose.

For testing part, it will show all the traces so that we could know which testing cases are not pass, as well as their exceptions.

References

  1. Pytest 官方文档:https://www.osgeo.cn/pytest/contents.html

【Pytest:Python 单元测试工具学习】相关推荐

  1. python逻辑与界面分离_一种数据与逻辑分离的Python单元测试工具

    一种数据与逻辑分离的Python单元测试工具 几个概念 TestCase TestCase是一个完整的测试单元,最小的测试执行实体,就是我们常说的测试用例. TestSuite 以某种特性将测试用例组 ...

  2. python 单元测试 工具_Django单元测试工具test client使用详解

    The test client test client是一个python类,来模拟一个简单的"哑"浏览器,允许你来测试你的view函数.你可以使用test client完成下列事情 ...

  3. python单元测试工具

    1. unittest 简介 这是标准库也是最常见的Python单元测试框架,它基于Java的JUnit框架,最初由Steve Purcell编写(以前称为PyUnit) unittest基本上提供了 ...

  4. python单元测试工具_Python的单元测试工具——unittest小结

    简介 unittest是Python的内建模块,是Python单元测试的事实标准,也叫PyUnit.使用unittest之前,先了解如下几个概念: test case:测试用例,可以通过创建unite ...

  5. Python测试工具-Pytest使用详解

    一.简介 Pytest是一个全功能Python测试工具,支持第三方扩展插件,能够使用其开展单元测试和复杂功能测试.可以和selenium.requests.appium等模块结合使用实现WEB UI. ...

  6. 31.Python的单元测试工具——doctest

    转载请注明原始出处:http://blog.csdn.net/a464057216/article/details/51866748 后续此博客不再更新,欢迎大家搜索关注微信公众号"测开之美 ...

  7. Python单元测试框架之pytest 3 -- fixtures

    From: https://www.cnblogs.com/fnng/p/4769020.html Python单元测试框架之pytest -- fixtures 2015-08-29 13:05 b ...

  8. 推荐学习《Python与量化投资从基础到实战》PDF及代码+《量化投资以Python为工具》PDF及代码...

    利用python分析量化投资问题是现在研究的热点,推荐两份资料用于学习 <Python与量化投资:从基础到实战>主要讲解如何利用Python进行量化投资,包括对数据的获取.整理.分析挖掘. ...

  9. Python科学计算学习:从入门到放弃系列(3)工具篇 IPython (Jupiter Notebook) 使用介绍

    1.前言 兵欲善其事,必先利其器. 发现这句话真的好有道理,我是按着张若愚的教材学Python科学计算的,他整本书,对,整本书,均是用IPython写的,也就是,编码演示和书籍撰写两不误.在这之前,我 ...

最新文章

  1. cc.AudioSource
  2. 12月21日云栖精选夜读:阿里云总裁胡晓明:AI泡沫过后,下一站是“产业AI”...
  3. hdu3518 后缀数组
  4. 审计日志_Oracle审计日志过大?如何清理及关闭审计机制?
  5. linux c mysql教程_Linux c访问mysql 编写入门
  6. Python 到底是怎么火起来的??网友:真的值!
  7. 在 IE 中使用 Windows 窗体控件
  8. react进行状态管理的几种方式
  9. 基于 Flink 的实时数仓生产实践
  10. 某人说自己是佛菩萨转世,什么情况下才可信
  11. 高程3总结#第23章离线应用与客户端存储
  12. 高效记忆/形象记忆(15)110数字编码表 91-00
  13. 兼容所有浏览器的Web打印控件的设计方案
  14. 微信小程序实现举报功能
  15. 给老师发邮件的格式参考
  16. 火山视窗opencv计算机视觉入门
  17. 16.为什么进行抓包测试
  18. 如何去除谷歌的人机身份验证
  19. KF32A146之IIC读写外部EEPROM
  20. 《网络安全2022:守望高质量》报告之数据安全热点事件与趋势解读

热门文章

  1. G2 定制仪表盘实践
  2. appstore开发者 名称修改
  3. 管理经济学学习之初探
  4. 揭开色彩营销中隐藏在品牌运营下的“变脸”戏法!
  5. 集合后台排序(java)——按照汉字中文拼音
  6. 软件工程导论课程总结
  7. PKI体系简易JAVA实现(一):时间戳服务器TSA
  8. 一个完整直播app功能分析
  9. 不要只怀揣梦想,而是要努力实现梦想!
  10. 使用Java编写《拳皇97》,致敬经典,还原八神庵大战草稚京