12.python开源——pytest自动化测试框架

 

1      下载链接

https://docs.pytest.org/en/latest/getting-started.html

或者使用pip安装

命令如下:
pip install pytest

更新命令:pip install -U pytest

查看版本pytest —version

2      pytest使用

2.1   基本测试

创建test_func.py

内容如下:

def func(x):return x + 1

def test_answer(): assert func(3) == 5

然后执行

#pytest test_func.py

collected 1items

 

test_func.py F

 

===================================FAILURES ===================================

_________________________________test_answer __________________________________

 

>  def test_answer(): assert func(3) == 5

E  assert 4 == 5

E   +  where 4 = func(3)

 

test_func.py:3: AssertionError

=========================== 1 failedin 0.02 seconds ===========================

测试结束,该用例测试失败。

因为函数结果因为4,不等于5嘛。

 

2.2   运行多个测试

        pytest会在当前目录中运行所有文件以及子文件夹,需要以test_*.py或*_test.py文件。遵循标准规则。

-q参数表示quiet

 

创建test_class.py内容如下:

 

 

class TestClass:

    def test_one(self):

            x = "this"

            assert 'h' in x

    def test_two(self):

            x = "hello"

            assert hasattr(x, 'check')

 

 测试结果如下:

1failed, 1 passed in 0.02 seconds

2.3   请求一个临时文件

创建test_temp.py内如如下:

 

def test_needsfiles(tmpdir):

    print (tmpdir)

assert 0

然后执行,会报错

在测试函数中列出了tmpdir。

 

2.4   查看内置的参数

# pytest --fixtures


 

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值