PytestPython 单元测试工具学习
- 由于是课程作业,所以是全英文的,词汇量不是很难吧。
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.
- 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’ - 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 - 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:
- Automatically find test functions and cases.
- Convenient declarations to match exceptions.
- Run testing cases, modules, and classes smoothly.
- Various pluggable units are available.
- 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
- Pytest 官方文档:https://www.osgeo.cn/pytest/contents.html
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfehihc
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01