pytest用例查找原理

当执行pytest命令时,pytest会从project中查找test来执行。本文先从几个路径相关的概念讲起,这样便于理解pytest在遍历路径时的走向。

pytest断言技巧

pytest的断言把Python语言简洁的优点发挥的淋漓尽致,因为它用的就是Python的标准断言assert。

2026-01-06 · 13 分钟 · 2583 字 · 测试开发刚哥 · Python生态 · #Pytest

pytest参数化进阶

用过unittest的朋友,肯定知道可以借助DDT实现参数化。用过JMeter的朋友,肯定知道JMeter自带了4种参数化方式(见参考资料)。pytest同样支持参数化,而且很简单很实用。

pytest内置和自定义marker

可以通过命令行查看所有marker,包括内置和自定义的

pytest精通fixture

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 fixture是什么 看看源码 def fixture( callable_or_scope=None, *args, scope="function", params=None, autouse=False, ids=None, name=None ): """Decorator to mark a fixture factory function. This decorator can be used, with or without parameters, to define a fixture function. The name of the fixture function can later be referenced to cause its invocation ahead of running tests: test modules or classes can use the ``pytest.mark.usefixtures(fixturename)`` marker. Test functions can directly use fixture names as input arguments in which case the fixture instance returned from the fixture function will be injected. Fixtures can provide their values to test functions using ``return`` or ``yield`` statements. When using ``yield`` the code block after the ``yield`` statement is executed as teardown code regardless of the test outcome, and must yield exactly once. :arg scope: the scope for which this fixture is shared, one of ``"function"`` (default), ``"class"``, ``"module"``, ``"package"`` or ``"session"`` (``"package"`` is considered **experimental** at this time). This parameter may also be a callable which receives ``(fixture_name, config)`` as parameters, and must return a ``str`` with one of the values mentioned above. See :ref:`dynamic scope` in the docs for more information. :arg params: an optional list of parameters which will cause multiple invocations of the fixture function and all of the tests using it. The current parameter is available in ``request.param``. :arg autouse: if True, the fixture func is activated for all tests that can see it. If False (the default) then an explicit reference is needed to activate the fixture. :arg ids: list of string ids each corresponding to the params so that they are part of the test id. If no ids are provided they will be generated automatically from the params. :arg name: the name of the fixture. This defaults to the name of the decorated function. If a fixture is used in the same module in which it is defined, the function name of the fixture will be shadowed by the function arg that requests the fixture; one way to resolve this is to name the decorated function ``fixture_<fixturename>`` and then use ``@pytest.fixture(name='<fixturename>')``. """ if params is not None: params = list(params) fixture_function, arguments = _parse_fixture_args( callable_or_scope, *args, scope=scope, params=params, autouse=autouse, ids=ids, name=name, ) scope = arguments.get("scope") params = arguments.get("params") autouse = arguments.get("autouse") ids = arguments.get("ids") name = arguments.get("name") if fixture_function and params is None and autouse is False: # direct decoration return FixtureFunctionMarker(scope, params, autouse, name=name)( fixture_function ) return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name) 总结一下 ...

pytest132个命令行参数用法

在Shell执行pytest -h可以看到pytest的命令行参数有这10大类,共132个

pytest快速入门

本书作者:测试开发刚哥,文章版权归个人所有,未经授权请勿转载。 安装Python和编辑器PyCharm Python安装包下载地址是: https://www.python.org/downloads/ PyCharm安装包下载地址是: https://www.jetbrains.com/pycharm/download/ 下载后按常规软件安装。 pytest pytest是Python内置包,无需单独安装,但是需要设置。 创建1个Project和1个test.py来试试,左上角点击 File | New Project | create,创建项目。在项目中右键选择 New | File 创建test.py 接着设置PyCharm,默认PyCharm自带的测试框架是Unittests,在 File | Settings | Tools | Python Integrated Tools | Testing | Default test runner,修改为pytest 在test.py文件中添加代码 def test(): pass 这个时候def的左边会出现一个绿色的箭头,点击之后就可以使用pytest运行,注意一定要是 pytest for… 才可以,如果只是 Run test.py,是不走pytest执行的(右上角Edit Configurations删掉来解决) 执行之后控制台输出信息就可以了 Testing started at 11:10 AM ... "D:\Program Files (x86)\Python38-32\python.exe" "D:\Program Files\JetBrains\PyCharm Community Edition 2020.1.2\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --target test.py::test Launching pytest with arguments test.py::test in D:\PycharmProjects\untitled ============================= test session starts ============================= platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1 -- D:\Program Files (x86)\Python38-32\python.exe cachedir: .pytest_cache rootdir: D:\PycharmProjects\untitled collecting ... collected 1 item test.py::test PASSED [100%] ============================== 1 passed in 0.14s ============================== Process finished with exit code 0 至此,pytest的环境就准备好了。