Pytest与unittest区别

api的编写:完整的api测试用例中,要考虑到每个测试点的初始化,测试步骤,测试断言还有清理的操作

unittest和pytest框架之间的区别:

1,unittest需要继承unitttest.TestCase类,pytest不需要继承,可以是函数也可以是类。

2,unittest参数化需要依赖第三方库比如说ddt,pytest参数化直接用内部的parametrize

3,unittest测试报告是HTMLTestRunner,pytest是Pytest-html或allure

4,unittest没有插件,pytest有丰富的插件如:pytest-cov

5,unittest不支持失败重试,pytest支持失败重试

 Pytest使用规范:

 测试用例的结果信息:

import pytest
 
 
def test_passing():
   assert  (1,2,3)==(1,2,3)
 
 
def test_failing():
   assert  1==2
 
@pytest.mark.skip() # 跳过
def test_skip():
   assert 1==1
 
@pytest.mark.xfail() # 预期失败
def test_xfail():
   assert 1==2
 
 
@pytest.mark.xpass() # 预期成功
def test_xpass():
   assert 1==1

pytest测试用例的执行顺序是按从上往下的顺序执行的 

 pytest的执行方式:

def test_login_001():
   assert 1==1
 
 '''tests包名  test_login.py:文件名'''
 
class TestBaidu(object):
   def test_baidu_001(self):
      pass
 
   def test_baidu_002(self):
      pass

        package级别:python -m pytest -v tests

        model级别:python -m pytest -v tests/test_login.py::test_login_001

        class级别:python -m pytest -v tests/test_login.py::TestBaidu

 pytest执行命令

        按分类执行测试用例

        python3 -m pytest -v  -k   "profile or register"

         添加分组:@pytest.mark.login(组名)          分组执行python -m pytest -v  -m   "login"

        执行失败立刻停止:python3 -m pytest -v -x test_login.py

        指定失败次数:--maxfail:pytest -v -x --maxfail=2 -m login

        定位错误:--lf          pytest --lf -m login

        忽略执行: -rs

        pytest -rs

import  pytest
 
@pytest.mark.login
def test_login_001():
   pass
 
@pytest.mark.skip(reason='忽略执行该测试点')
def test_login_002():
   pass

Pytest与WebDriver 

import pytest


def test_baidu_title(selenium):
    selenium.get('https://www.baidu.com/')
    assert selenium.title == '百度一下,你就知道'


def test_baidu_url(selenium):
    selenium.get('http://www.baidu.com/')
    assert selenium.current_url == 'https://www.baidu.com/'

'''执行命令:pytest -v pytest-selenium.py --driver Firefox'''
  • 6
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟学识

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值