zoukankan      html  css  js  c++  java
  • pytest

    pytest框架要执行用例,必须以模块test开头,函数test开头,类Test开头,否则不被执行;

    pytest的执行 :

    执行package:python -m pytest -v test/ 

    执行模块下的测试用例:python -m pytest -v tests/test_login.py::test_login_001

    执行模块下的类:python -m pytest -v test/test_login.py::TestBaidu

    执行类下的方法:python -m pytest -v test/test_login.py::TestBaidu::test_login_001s

    按照方法名来筛选要执行的方法用k,后面加--collect-only查看筛选情况:python -m pytest -v -k "profile or register" --collect-only,取消--collect-only后进行执行,如果将前面的or换成and,那么执行的就是名字中既有profile又有register的函数;

    -m是以增加装饰器的形式进行分组,python -m pyetst -v -m "login or logout“

    import pytest
    @pytest.mark.login
    def test_login_001():
    pass
    @pytest.mark.login
    def test_login_002():
    pass
    @pytest.mark.login
    def test_login_003():
    pass
    @pytest.mark.logout
    def test_logout_001():
    pass
    @pytest.mark.logout
    def test_logout_002():
    pass
    @pytest.mark.profile
    def test_profile_001():
    pass
    @pytest.mark.profile
    def test_profile_002():
    pass
    

     -x失败后立即停止:

    python3 -m pytest -v -x test_login.py

    pytest.mark.skip(reason="写跳过的原因") 

    pytest参数化,如果类列表中的数据类型是字典,那么参数化时字典中的key值不能被一一写出,所有都要用data代替@pytest.mark.parametrize('data',被调用函数)

    def dataDict():
    datas=[
    {'a':1,'b':1,'result':2},
    {'a':'a','b':'b','result':'ab'},
    {'a':0,'b':0,'result':0},
    {'a':2,'b':2,'result':4},
    {'a':'x','b':'y','result':'xy'},
    ]
    return datas
    @pytest.mark.parametrize('data',dataDict())
    def test_add_dict(data):
    assert add(a=data['a'],b=data['b'])==data['result']
    

      

  • 相关阅读:
    多多挣钱,多多攒钱。
    平安鸿运英才少儿教育金保障计划
    沙河订奶
    排序算法--参考
    《程序设计基础》考试大纲 复习-C语言
    There is no Action mapped for namespace [/demo1] and action name [doLogin] associated with context path [/SSO_same_domain]
    Android报错 The connection to adb is down, and a severe error has occured.
    解析库beautifulsoup
    request模块使用
    爬虫基本原理
  • 原文地址:https://www.cnblogs.com/manma/p/15480807.html
Copyright © 2011-2022 走看看