Pytest学习笔记

参考链接

1、pytest执行用例规则

  • 目录下执行所有的用例
pytest
// or
py.test
  • 执行单独一个pytest模块
pytest fileName.py
  • 运行某个模块里面的某个类
pytest fileName.py::className
  • 运行某个模块里面某个类里面的方法
pytest fileName.py::className::methodName
  • -v打印运行日志信息(详细)
pytest -v fileName.py
  • -q打印运行日志信息(简略)
pytest -q fileName.py
  • -s控制台输出结果
pytest -v -s fileName.py
//s是带控制台输出结果,也是输出详细运行日志
  • -m标记表达式
pytest -m login
//将运行用 @pytest.mark.login 装饰器修饰的所有测试
  • -x用例运行失败则立即停止执行
pytest -x fileName.py
  • -k运行包含关键字的用例
pytest -v -k "one" fileName.py
//执行测试用例名称包含 one 的所有用例
  • -k运行排除关键字的用例
pytest -v -k "not one" fileName.py
//执行测试用例名称不包含 one 的所有用例
  • -k运行匹配多个关键字的用例
pytest -v -k "one or two" fileName.py
//执行测试用例名称包含 one 或 two 的所有用例
  • --maxfail=num错误个数达到指定数量停止测试
pytest fileName.py --maxfail=1
//用例运行时允许的最大失败次数,超过则立即停止

2、setup和teardown

参考链接

unittest pytest
setup()
setupClass()
teardown()
teardownClass()
模块级(开始于模块始末,全局的):setup_module()、teardown_module()
函数级(只对函数用例生效,不在类中):setup_function()、teardown_function()
类级(只在类中前后运行一次,在类中):setup_class()、teardown_class()
方法级(开始于方法始末,在类中):setup_method()、teardown_method()
方法细化级(运行在调用方法的前后):setup()、teardown()
# test_setup_teardown.py
#!-*- coding: utf-8 -*-
import pytest
def setup_module():
print("----all py module start run once setup_module eg. open safari----")
def teardown_module():
print("----all py module end run once teardown_module eg. close safari----")
def setup_function():
print("----every functional testcase start run everytime setup_function----")
def teardown_function():
print("----every functional testcase end run everytime teardown_function----")
def test_one():
print("one")
def test_two():
print("two")
class TestCase():
def setup_class(self):
print("====all test class start run once setup_class====")
def teardown_class(self):
print("====all test class end run once teardown_class====")
def setup_method(self):
print("====class testcase run start run setup_method====")
def teardown_method(self):
print("====class testcase run end run teardown_method====")
def setup(self):
print("====class testcase run start run setup")
def teardown(self):
print("====class testcase run end run teardown")
def test_three(self):
print("three")
def test_four(self):
print("four")
if __name__ == '__main__':
pytest.main(["-q", "-s", "-ra", "test_setup_teardown.py"])

3、fixture

参考链接
fixture的优势

  • 命名方式灵活,不局限于 setup 和 teardown 这几个命名
  • conftest.py 配置里可以实现数据共享,不需要 import 就能自动找到 fixture
  • scope="module" 可以实现多个 .py 跨文件共享前置
  • scope="session" 可以实现多个 .py 跨文件使用一个 session 来完成多个用例
@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)
def test():
print("fixture初始化的参数列表")
  • scope:可以理解成 fixture 的作用范围,默认:function,还有 class、module、package、session
    • function 的作用域:每一个函数或方法都会调用
    • class 的作用域:每一个类调用一次,一个类中可以有多个方法
    • module 的作用域:每一个 .py 文件调用一次,该文件内又有多个 function 和 class
    • session 的作用域:是多个文件调用一次,可以跨 .py 文件调用,每个 .py 文件就是 module
  • params:一个可选的参数列表,它将导致多个参数调用 fixture 功能和所有测试使用它
  • autouse:默认:False,需要用例手动调用该 fixture;如果是 True,所有作用域内的测试用例都会自动调用该 fixture
  • ids:每个字符串 id 的列表,每个字符串对应于 params,这样他们就是测试ID的一部分。如果没有提供ID,它们将从 params 自动生成
  • name:默认:装饰器的名称,同一模块的 fixture 相互调用建议写不同的名称
# test_fixture.py
#!-*- coding: utf-8 -*-
import pytest
# first
@pytest.fixture
def login():
print("input id login, passport login")
def test_s1(login):
print("case 1: after login do test_s1")
def test_s2():
print("case 2: no login, do test_s2")
# second
@pytest.fixture
def login2():
print("input id login2, passport login")
@pytest.mark.usefixtures("login2", "login")
def test_s3():
print("case 3: after login do test_s3")
# third
@pytest.fixture(autouse=True)
def login3():
print("====login3====")
# not start with 'test', decoration don'r execute fixture
@pytest.mark.usefixtures("login2")
def login4():
print("====login4====")
posted @   CestLaVie0019  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示