码迷,mamicode.com
首页 > 编程语言 > 详细

python单元测试框架——pytest

时间:2017-11-01 01:01:47      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:version   好的   ifile   src   ssi   roo   ror   blog   tar   

官网:https://docs.pytest.org/en/latest/

pytest帮你写出更好的程序

1、安装:

  Ubuntu16.04安装pytest:

  pip3 install pytest

  pytest --version

    This is pytest version 3.2.3, imported from /usr/local/lib/python3.5/dist-packages/pytest.py

 

2、An example of a simple test:(一个简单的例子),命名为test_pytest1.py

1 def funx(x):
2     return x + 1
3 
4 
5 def test_answer():
6     assert funx(2) == 5

运行:

进入python脚本路径:pytest test_pytest1.py

  root@localhost:/home/ranxf/Python3单元测试/demo# pytest test_pytest1.py
============================= test session starts ==============================
platform linux -- Python 3.5.2, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /home/ranxf/Python3单元测试/demo, inifile:
collected 1 item                                                                

test_pytest1.py F

=================================== FAILURES ===================================
_________________________________ test_answer __________________________________

    def test_answer():
>       assert funx(2) == 5
E       assert 3 == 5
E        +  where 3 = funx(2)

test_pytest1.py:8: AssertionError
=========================== 1 failed in 0.02 seconds ===========================

 

进入python脚本路径:pytest -q test_pytest1.py(加一个参数-q),运行结果:

root@localhost:/home/ranxf/Python3单元测试/demo# pytest -q test_pytest1.py
F
=================================== FAILURES ===================================
_________________________________ test_answer __________________________________

    def test_answer():
>       assert funx(2) == 5
E       assert 3 == 5
E        +  where 3 = funx(2)

test_pytest1.py:8: AssertionError
1 failed in 0.02 seconds

两种运行结果有一点差异,就是少了一些版本信息。

3、一个测试类中创建多个测试用例:

技术分享
 1 # 一个测试类种创建多个测试用例
 2 
 3 
 4 class TestClass:
 5     def test_one(self):
 6         x = "this"
 7         assert "s" in x
 8 
 9     def test_two(self):
10         x = "hello"
11         assert x == "hi"
View Code

 

python单元测试框架——pytest

标签:version   好的   ifile   src   ssi   roo   ror   blog   tar   

原文地址:http://www.cnblogs.com/ranxf/p/7764568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!