Pytest学习day1

1、类必须以Test开头,不然运行的时候该类中的内容会被忽略
2、不指定运行路径的话,函数要以test_开头或以_test结尾
3、在类中管理tests有几点好处:方便管理、类中的fixture都可以使用、类层级的marks都可以使用
4、解释器指定的pytest,运行时报错,网上找了各种方法,最后发现竟然只是因为没有把方法test_th放到类里面,一定要注意对齐哈

Pytest学习day1
5、命令行运行参数中-q和-k的区别,-q是输出简洁的运行结果
Pytest学习day1
6、临时路径tmp_path
pytest会有个临时路径,可以直接用tmp_path这个变量
Pytest学习day1
7、pytest –fixtures可查看有哪些内置的pytest fixture
8、方法或者类前使用@pytest.mark.slow使用命令行pytest -m slow运行,就只会运行带有slow这个mark的类或者方法;若类的mark为slow,其下函数test_one的mark为fast,运行命令仍为pytest -m slow,则仍会运行test_one函数,即只要类的mark匹配,就会运行该类下面的所有函数
Pytest学习day1
9、命令行运行包下面的文件
pytest –pyargs pkg
其中pkg为包名,上述命令运行的是当前文件夹下的包及其下的子目录文件 指定是包名称,不能跟包下面的文件名称,例如:
pytest –pyargs pkg.test_run.py或者pytest –pyargs pkg.test_run都不可以
![在这里插入图片描述](https://img-blog.csdnimg.cn/3fcf16535b814953a5e5cdae6d5f70cd.pn
10、pytest查看版本等命令
pytest –version # shows where pytest was imported from
pytest –fixtures # show available builtin function arguments
pytest -h | –help # show help on command line and config file options
11、展示运行时间
pytest –durations=10 –durations-min=1.0
显示运行时间超过一秒的最慢的10个test,加上-vv参数就会显示所有test的运行时间

只有一个在这个区域里,这里设置的是0.01s

Pytest学习day1
12、pytest -p pluginname加载插件
pytest -p no:pluginname 禁用插件还没遇到过该命令的使用场景
13、调用pytest的方式:
1)命令行调用:
命令行调用pytest的两种方式:pytest […]
或者通过python命令调用 python -m pytest […]
区别是通过python调用会把当前目录加到sys.path
2)python代码调用:
retcode = pytest.main()这样调用不会raise SystemExit而会返回exit code
可以像下面这样添加参数
retcode = pytest.main([“-x”,”mytestdir”])
import pytest
import sys

class MyPlugin:
    def pytest_sessionfinish(self):
        print("*** test run reporting finishing")

if __name__ == "__main__":
    sys.exit(pytest.main(["-qq"], plugins=[MyPlugin()]))

14、conftest.py文件有什么用

Pytest学习day1
下面是加了conftest.py文件后运行test_foocompare.py文件的结果
Pytest学习day1

from test_foocompare import Foo

def pytest_assertrepr_compare(op, left, right):
    if isinstance(left, Foo) and isinstance(right, Foo) and op == "==":
        return [
        "Comparing Foo instances:",
        " vals: {} != {}".format(left.val, right.val),
        ]

import pytest

class Foo:
    def __init__(self, val):
        self.val = val
    def __eq__(self, other):
        return self.val == other.val

def test_compare():
    f1 = Foo(1)
    f2 = Foo(2)
    assert f1 == f2

pytest_assertrepr_compare自定义失败的断言结果
15、一个函数前面加上@pytest.fixture就可以把这个函数A当作一个参数传给另一个函数B,且B可以接收多个fixture作为其参数
16、fixture可自动调用@pytest.fixture(autouse=True)
加上autouse=True后,即使没有函数调用该fixture也会自动执行
17、fixture的scope有function、class、module、class、session
把fixture函数放到conftest.py函数中,当前目录下及其子目录下的所有文件都可以调用该fixture函数
每个.py文件就是一个module,session相当于多个.py文件

  • 动态配置scope的值还没理解??????
    18、前置和后置操作,用yield区分,yield前的代码是前置,yield后面的代码是后置,下面代码执行顺序如下:注意4和5的执行顺序,前置的时候先执行的sending_over,或者的时候就会倒序,即先执行receving_user
    Pytest学习day1
    安全地teardown:request.addfinalizer(delete_user)可以确保在代码中出现exception时仍然会执行delete_user
    19、自定义带参数的fixture,只能接收参数request,如下图:
    Pytest学习day1
  • 详细用法待进一步查看????
    20、

Original: https://blog.csdn.net/u010397519/article/details/124835879
Author: 不停歇的小鸟
Title: Pytest学习day1

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/775702/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球