美文网首页
干货|自动化测试工程师必须要掌握的测试框架-基础篇

干货|自动化测试工程师必须要掌握的测试框架-基础篇

作者: 吱吱菌啦啦 | 来源:发表于2022-04-19 13:44 被阅读0次
一、pytest命名规则
  • 文件名:test_开头,或_test结尾
  • 类名:Test开头
  • 方法/函数: test_开头
  • 测试类中不可以添加init构造函数
python命名规则
  • 文件名:小写字母,单词与单词之间下划线隔开
  • 类名:驼峰命名,每一个单词之间首字母大写
  • 方法/函数: 小写字母,单词与单词之间下划线隔开
二、pytest运行用例
  • 1.界面化运行
  • 2.命令行运行
    • 运行包下所有的用例:pytest或py.test [包名]
    • 运行某个用例模块:pytest 文件名.py
    • 运行某个模块里的某个类:pytest 文件名.py::类名
    • 运行某个模块里某个类的方法:pytest 文件名.py::类名::方法名
      命令后面加上-v查看执行了哪些用例
运行结果分析
  • fail--用例失败
  • error--代码写错了
  • pass--用例通过
  • warning--警告
  • deselect--用例没有被选中,有些用例不想执行加标签
三、pytest命令行常用参数
  • -x用例一旦失败,就立即停止
  • --maxfail=num 允许最大失败用例数达到num时停止执行
  • -m 标记用例
  • -k “关键字” 执行包含关键字的测试用例
  • -v 打印详细日志
  • -s 打印输出日志,一般-vs一块用
  • --collect-only 只收集
四、pytest测试用例调度与运行
  • --lf 只执行上一次失败的测试用例
  • --ff 先运行上一次失败的用例,再运行其他用例
五、python执行pytest
  • 1.使用main函数,入口函数if name == 'main':中
    • 1.运行当前目录下所有符合规则的测试用例,包括子目录(test_.py和_test.py)pytest.main()
    • 2.运行test_param.py::test_descart模块中的某一条用例pytest.main(['test_param.py::test_descart','-vs'])
    • 3.运行某个标签pytest.main(['test_param.py','-vs','-m','descart'])
      运行方式’python test_*.py‘
if __name__ == '__main__':
    pytest.main()
    pytest.main(['test_param.py::test_descart', '-vs'])
    pytest.main(['test_param.py', '-vs', '-m', 'descart'])
  • 2.使用python -m pytest命令行调用pytest(jenkins持续集成)
六、pytest测试框架结构
setup/teardown:前置条件(打开应用)、后置条件(关闭应用)
  • 模块级别:setup_module 、teardown_module 全局,优先级最高,最开头和最结尾执行一次
  • 类级别:setup_class 、teardown_class 只在类中前后执行一次
  • 函数级别:setup_function 、teardown_function 在类外
  • 方法级别:setup_method 、teardown_method 类内的每个方法前后执行,有几个执行几个
  • setup teardown 在类内,运行在调用方法的前后
七、pytest标记测试用例
场景:只运行符合要求的测试用例,可以把用例分为多个模块,,然后指定模块名称执行
解决:在测试用例方法上加@pytest.mark.标签名
执行:-m执行自定义标记的相关用例
  • pytest -s 文件名.py -m=webtest 只执行带webtest标签的用例
  • pytest -s 文件名.py -m webtest 只执行带webtest标签的用例
  • pytest -s 文件名.py -m "not ios" 执行不带ios标签的用例
八、pytest设置跳过及预期失败用例
pytest内置标签,可以处理一些特殊的测试用例,不能成功的测试用例
1.skip 始终跳过该测试用例
  • 调试时不想运行这个用例
  • 标记无法在某些平台上运行的测试功能
  • 在某些版本中执行,在其他版本中跳过
  • 当外部资源不可用时跳过
    • 1.添加装饰器
      @pytest.mark.skip(reason=”功能未开发完“)
      @pytest.mark.skipif

    • 2.代码中添加跳过代码
      pytest.skip(reason)

import sys
import pytest

@pytest.mark.skin
def test_case1():
    print("未开发完")

@pytest.mark.skin(reason="存在bug")
def test_case2():
    assert False

skipif 遇到特定情况跳过该测试用例

import sys
import pytest

@pytest.mark.skipif(sys.platform == 'win', reason="不运行window平台")
def test_case3():
    assert True

@pytest.mark.skipif(sys.platform == 'mac', reason="不运行mac平台")
def test_case4():
    assert True
2.xfail 遇到特定情况,产生一个’预期失败‘输出

与skip类似,预期结果为fail 标记用例为fail
用法:添加装饰器@pytest.mark.xfail

@pytest.mark.xfail
def test_case5():
    print("test_xfail")
    pytest.xfail(reason="未开发完") # 代码里也能执行xfail
    assert 1 == 1

@pytest.mark.xfail
def test_case6():
    print("test_xfail")
    assert 1 == 2
    
xfail = pytest.mark.xfail
@xfail(reason="bug001未改完")
def test_case7():
    assert 0
九、pytest参数化
参数化:将模型中的定量信息变量化,使之成为任意调整的参数
  • 1.参数化名字与方法中的参数名一一对应
  • 2.如果传多个参数,要放在列表中,列表中嵌套列表/元组
  • 3.多参数中,ids用于用例重命名,个数与传递参数的个数完全一致
  • 4.笛卡尔积
import pytest

search_list = ["appium", "面试题", "", ".,/、"]
@pytest.mark.parametrize('name',search_list)
def test_search(name):
    """
    单参数
    """
    assert name in search_list

@pytest.mark.parametrize("test_input,expected",[("3+5",8),("-1+(0.1)",-0.9),("0+0",0)],ids=["int","float","zero"])
def test_more(test_input, expected):
    """
    多参数
    """
    assert eval(test_input) == expected
    
@pytest.mark.parametrize("wd", ["appium","selenium","pytest"])
@pytest.mark.parametrize("code", ["utf-8", "gbk", "gb2312"])
def test_descart(wd,code):
    """
    笛卡尔积,wd有三种,code有三种,覆盖所有的情况,共9种
    """
    print(f"wd:{wd},code:{code}")
十、pytest异常处理
  • 1.try...except
try:
    a = int(input("输入被除数:"))
    b = int(input("输入除数:"))
    c = a/b
    print("输入的两束相处结果是:", c)
except(ValueError,ArithmeticError):
    print("程序发生异常")
except:
    print("未知异常")
print("继续")

执行结果:
输入被除数:10
输入除数:0
程序发生异常
继续

  • 2.异常处理方法:pytest.raises()
    • 捕获异常
    • 获取捕获异常的细节 异常类型 异常信息等
    • 发生异常,后面的代码将不执行
import pytest
def test_raise():
    """
    通过,结果等于预期
    :return:
    """
    with pytest.raises(ValueError, match="must be 0 or None"):
        raise ValueError("must be 0 or None")

def test_raise1():
    """
    通过,结果等于预期
    :return:
    """
    with pytest.raises(ValueError) as exc_info:
        raise ValueError("value must be 42")
    assert  exc_info.type is ValueError
    assert  exc_info.value.args[0] == "value must be 42"

def test_raise2():
    """
    不通过,ZeroDivisionError不等于预期
    """
    with pytest.raises(ValueError, match="must be 0 or None"):
        raise ZeroDivisionError("除数为零")

相关文章

网友评论

      本文标题:干货|自动化测试工程师必须要掌握的测试框架-基础篇

      本文链接:https://www.haomeiwen.com/subject/qyrjertx.html