接触过单元测试的朋友肯定知道unittest的,那个不需要安装的单元测试框架,可以秒秒钟新建简单例子。
| |
| import unittest |
| |
| class MyTestCase(unittest.TestCase): |
| def test_something(self): |
| self.assertEqual(True, False) |
| |
| if __name__ == '__main__': |
| unittest.main() |
运行结果如下:
| (testops) D:\Coding\testops\Chapter04>python -m unittest reunittest.py |
| F |
| ====================================================================== |
| FAIL: test_something (reunittest.MyTestCase) |
| ---------------------------------------------------------------------- |
| Traceback (most recent call last): |
| File "D:\Coding\testops\Chapter04\reunittest.py", line 6, in test_something |
| self.assertEqual(True, False) |
| AssertionError: True != False |
| |
| ---------------------------------------------------------------------- |
| Ran 1 test in 0.000s |
| |
| FAILED (failures=1) |
那么pytest比unittest更简单吗?更快吗?
pytest官方网站: http://docs.pytest.org/en/latest/
看完这个文章你就知道了。
也许有人也没听过unittest,那么笔者也写过unittest的介绍,也在这一章节,你可以翻一翻。
| |
| pytestday01.py |
| reunittest.py |
| test_main.py |
| test_sample.py |
./test_sample.py
| |
| def inc(x): |
| return x + 1 |
| |
| def test_answer(): |
| assert inc(3) == 5 |
这是官方给出的例子。inc()函数接收了一个参数x,返回 x+1。test_answer()是一个测试用例,调用inc()方法并传参数为3,使用assert断言返回结果是否为5 .
接下来我们运行测试,看一下运行结果。
| (testops) D:\Coding\testops\Chapter04>pytest test_sample.py |
| ======================================================== test session starts ======================================================== |
| platform win32 -- Python 3.7.1, pytest-5.2.2, py-1.8.0, pluggy-0.13.0 |
| rootdir: D:\Coding\testops\Chapter04 |
| collected 1 item |
| |
| test_sample.py F [100%] |
| |
| ============================================================= FAILURES ============================================================== |
| ____________________________________________________________ test_answer ____________________________________________________________ |
| |
| def test_answer(): |
| > assert inc(3) == 5 |
| E assert 4 == 5 |
| E + where 4 = inc(3) |
| |
| test_sample.py:7: AssertionError |
| ========================================================= 1 failed in 0.02s ========================================================= |
运行结果中有 用例的执行结果描述 【 test_sample.py F 】,
错误的追踪记录 【test_sample.py:7: AssertionError】,
还有结果总述 【 1 failed in 0.02s 】 这是我们最直观的展现方式了。
./test_main.py
| import pytest |
| |
| def inc(x): |
| return x + 1 |
| |
| def test_answer(): |
| assert inc(3) == 5 |
| |
| if __name__ == '__main__': |
| pytest.main() |
运行命令: pytest test_main.py
| (testops) D:\Coding\testops\Chapter04>pytest test_main.py |
| ======================================================== test session starts ======================================================== |
| platform win32 -- Python 3.7.1, pytest-5.2.2, py-1.8.0, pluggy-0.13.0 |
| rootdir: D:\Coding\testops\Chapter04 |
| collected 1 item |
| test_main.py F [100%] |
| |
| ============================================================= FAILURES ============================================================== |
| ____________________________________________________________ test_answer ____________________________________________________________ |
| |
| def test_answer(): |
| > assert inc(3) == 5 |
| E assert 4 == 5 |
| E + where 4 = inc(3) |
| |
| test_main.py:8: AssertionError |
| ========================================================= 1 failed in 0.03s ========================================================= |
有了main()那就可以用IDE来直接执行了。
在学习了unittest单元测试框架之后,还有必要学习pytest吗?答案是肯定的。
pytest是一个第三方单元测试框架,更加简单、灵活,而且提供了更加丰富的扩展,弥补了unittest在做web自动化测试的一些不足。
验证一下
pytest 命令在安装pytest测试框架时默认生成于 …\Python37\Scripts
通过上面的例子,你已经感受到了pytest的优点,它更加简单。
首先不必像unittest一样必须创建测试类。其次断言assert的使用也比unittest提供的断言方法更加简单。
1 什么时候选择pytest?什么时候选择unittest?