------------恢复内容开始------------

1.pytest需要测试多个py文件,这些文件有一定的依赖关系,同时执行的时候,需要只执行一次初始化setup,结束再执行一次teardown;

2.多个py文件需要使用pytest的conftest.py,这个文件名是固定的,在同级目录还需要__init__.py;

3.由于setup这种经典的初始化方法只支持module,class.method,function,setup

  module: 一个py文件只运行一次,里面可以是class和function;

  class: 每个class执行一次;

  method: class里面的每个函数执行一次;

  function : 每个函数执行一次(函数不在class中);

  setup: 最小层级,每个函数运行一次;在allure报告中,现在在Test body中;其他级别显示在的setup中;

4.经典初始化不支持多个py初始化,只能选用fixture;

  scope值 :默认是function;

  session 多个py文件;

  module 一个py文件里面多个class或function;

  class: class级别;

  function:函数级别;

由于我们是多个py所以,只能选择session;

yield 是setup和teardown分界线;

@pytest.fixture(scope="session")
def start_up_session():
try: case_name = os.path.basename(__file__).replace('.py', '')
print(case_name + " start")
# yield 前面的是tearup的操作
yield case_name
# yield 后面的是teardown的操作
print(case_name + " finish")

  autouse = True   fixture里面有个参数autouse,默认是Fasle没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了;

@pytest.fixture(scope="function", autouse=True)
def get_status():
case_name = os.path.basename(os.path.abspath(__file__)).replace(".py", "")
print(case_name + " begin")
yield 1 if case_name.isalnum() else 0
print(case_name + " finish")

# 不传参数get_tstus也会运行
def test_1():
print(1111) def test_name():
print(2222) if __name__ == "__mian__":
pytest.main(["-s", "test_base.py"])
=========================================
============================= test session starts =============================
platform win32 -- Python 3.6.8, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Program Files\Python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.8', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '5.4.2', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.8.13', 'cov': '2.8.1', 'forked': '1.1.3', 'html': '2.1.1', 'metadata': '1.9.0', 'ordering': '0.6', 'rerunfailures': '9.0', 'timeout': '1.3.4', 'xdist': '1.32.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_202'}
rootdir: F:\ShenjuCloudTest\APP
plugins: allure-pytest-2.8.13, cov-2.8.1, forked-1.1.3, html-2.1.1, metadata-1.9.0, ordering-0.6, rerunfailures-9.0, timeout-1.3.4, xdist-1.32.0
collecting ... collected 2 items test_base.py::test_1 test_base begin
PASSED [ 50%]1111
test_base finish test_base.py::test_name test_base begin
PASSED [100%]2222
test_base finish ============================== 2 passed in 0.03s ============================== Process finished with exit code 0

autouse = True   fixture里面有个参数autouse,默认是Fasle没开启的,需要显示调用;

import pytest
import os # 默认autouse=Fasle
@pytest.fixture(scope="function")
def get_status():
case_name = os.path.basename(os.path.abspath(__file__)).replace(".py", "")
print(case_name + " begin")
yield 1 if case_name.isalnum() else 0
print(case_name + " finish") # 显式调用
@pytest.mark.usefixtures("get_status")
def test_1():
print(1111) # 显式调用
@pytest.mark.usefixtures("get_status")
def test_name():
print(2222) if __name__ == "__mian__":
pytest.main(["-s", "test_base.py"])

5.fixture 需要返回值:用传参数调用:在函数参数中,插入fixture的参数;

import pytest
import os @pytest.fixture(scope="function", autouse=True)
def get_status():
case_name = os.path.basename(os.path.abspath(__file__)).replace(".py", "")
print(case_name + " begin")
yield 1 if case_name.isalnum() else 0
print(case_name + " finish") def test_1(get_status):
num = get_status
assert num == 0
print(1111) def test_name(get_status):
num = get_status
assert num < 1
print(2222) if __name__ == "__mian__":
pytest.main(["-s", "test_base.py"])
=================================== ============================= test session starts =============================
platform win32 -- Python 3.6.8, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Program Files\Python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.8', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '5.4.2', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.8.13', 'cov': '2.8.1', 'forked': '1.1.3', 'html': '2.1.1', 'metadata': '1.9.0', 'ordering': '0.6', 'rerunfailures': '9.0', 'timeout': '1.3.4', 'xdist': '1.32.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_202'}
rootdir: F:\ShenjuCloudTest\APP
plugins: allure-pytest-2.8.13, cov-2.8.1, forked-1.1.3, html-2.1.1, metadata-1.9.0, ordering-0.6, rerunfailures-9.0, timeout-1.3.4, xdist-1.32.0
collecting ... collected 2 items test_base.py::test_1 test_base begin
PASSED [ 50%]1111
test_base finish test_base.py::test_name test_base begin
PASSED [100%]2222
test_base finish

6.fixture 不需要返回值:除了用传参数调用,还可以用@pytest.mark.usefixtures();对class和函数都可以使用,记得和scope搭配;

import pytest
import os @pytest.fixture(scope="function", autouse=True)
def get_status():
case_name = os.path.basename(os.path.abspath(__file__)).replace(".py", "")
print(case_name + " begin")
yield 1 if case_name.isalnum() else 0
print(case_name + " finish") @pytest.mark.usefixtures("get_status")
def test_1():
print(1111) @pytest.mark.usefixtures("get_status")
def test_name():
print(2222) if __name__ == "__mian__":
pytest.main(["-s", "test_base.py"])
=======================================
============================= test session starts =============================
platform win32 -- Python 3.6.8, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Program Files\Python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.8', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '5.4.2', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.8.13', 'cov': '2.8.1', 'forked': '1.1.3', 'html': '2.1.1', 'metadata': '1.9.0', 'ordering': '0.6', 'rerunfailures': '9.0', 'timeout': '1.3.4', 'xdist': '1.32.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_202'}
rootdir: F:\ShenjuCloudTest\APP
plugins: allure-pytest-2.8.13, cov-2.8.1, forked-1.1.3, html-2.1.1, metadata-1.9.0, ordering-0.6, rerunfailures-9.0, timeout-1.3.4, xdist-1.32.0
collecting ... collected 2 items test_base.py::test_1 test_base begin
PASSED [ 50%]1111
test_base finish test_base.py::test_name test_base begin
PASSED [100%]2222
test_base finish

7. 多个py文件使用conftest.py,名字固定,不要改;

  同级需要有__init__.py;

  千万不要通过import导入conftest,否则每个py文件都会运行一遍conftest.py, 无法起到多个py只执行一次作用;pytest执行的时候自动去读conftest.py配置;

import pytest
import os # conftest.py @pytest.fixture(scope="session")
def get_status():
case_name = os.path.basename(os.path.abspath(__file__)).replace(".py", "")
print(case_name + " begin")
yield 1 if case_name.isalnum() else 0
print(case_name + " finish")
import pytest
import os # test_base @pytest.mark.usefixtures("get_status")
def test_1():
print(1111) @pytest.mark.usefixtures("get_status")
def test_name():
print(2222) if __name__ == "__mian__":
pytest.main(["-s", "test_base.py"])
====================================================================

============================= test session starts =============================
platform win32 -- Python 3.6.8, pytest-5.4.2, py-1.8.1, pluggy-0.13.1 -- C:\Program Files\Python36\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.6.8', 'Platform': 'Windows-10-10.0.18362-SP0', 'Packages': {'pytest': '5.4.2', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'allure-pytest': '2.8.13', 'cov': '2.8.1', 'forked': '1.1.3', 'html': '2.1.1', 'metadata': '1.9.0', 'ordering': '0.6', 'rerunfailures': '9.0', 'timeout': '1.3.4', 'xdist': '1.32.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_202'}
rootdir: F:\ShenjuCloudTest\APP
plugins: allure-pytest-2.8.13, cov-2.8.1, forked-1.1.3, html-2.1.1, metadata-1.9.0, ordering-0.6, rerunfailures-9.0, timeout-1.3.4, xdist-1.32.0
collecting ... collected 2 items

test/test_base.py::test_1 conftest begin
PASSED [ 50%]1111

test/test_base.py::test_name PASSED [100%]2222
conftest finish

============================== 2 passed in 0.03s ==============================

Process finished with exit code 0

------------恢复内容结束------------

pytest 多个PY文件执行共享变量及用fixture和conftest的更多相关文章

  1. pytest文档2-用例执行

    用例设计原则 1.文件名以test_******.py文件和*******_test.py 2.以test_****开头的函数 3.以Test***开头的类 4.以test_*****开头的方法 5. ...

  2. Pytest fixture及conftest详解

    前言 fixture是在测试函数运行前后,由pytest执行的外壳函数.fixture中的代码可以定制,满足多变的测试需求,包括定义传入测试中的数据集.配置测试前系统的初始状态.为批量测试提供数据源等 ...

  3. pytest:conftest.py文件

    一.fixture scope 为session 级别是可以跨 .py模块调用的,也就是当我们有多个 .py文件的用例时,如果多个用例只需调用一次fixture,可以将scope='session', ...

  4. 『德不孤』Pytest框架 — 14、Pytest中的conftest.py文件

    目录 1.conftest.py文件介绍 2.conftest.py的注意事项 3.conftest.py的使用 4.不同位置conftest.py文件的优先级 5.conftest.py中Fixtu ...

  5. 【linux】终端直接执行py文件,不需要python命令

    先将终端所在路径切换到python脚本文件的目录下然后给脚本文件运行权限,一般755就OK,如果完全是自己的私人电脑,也不做服务器什么的,给777的权限问题也不大(具体权限含义参考chmod指令的介绍 ...

  6. python顺序执行多个py文件

    python顺序执行多个py文件 假如我要执行code目录下的python程序,假设该目录下有1.py,2.py,3.py,4.py四个文件,但是我想执行1.py,2.py,4.py,则可在该目录下创 ...

  7. jenkins+pytest+ allure运行多个py文件测试用例

    jenkins的pytest运行多个py文件,导出allure报告方法,只需改下job的配置中的构建即可(pytest会运行指定文件下的所有test开头的py文件),如下:              ...

  8. selenium,unittest——自动化执行多个py文件脚本并生成报告

    将多个py文件的自动化脚本顺序运行,并生成报告,运行run_all_case后会自动运行文件内所有test开头的py文件并在指定文件夹report生成由脚本时间命名的报告 脚本执行后结果: 生成报告并 ...

  9. 整合多个py文件接口的unittest。suite执行方法

    1.每个接口用例为一个.py文件.内容如下: getAdMakeMoneyList文件: # coding=utf-8import xlrdimport requestsimport unittest ...

随机推荐

  1. flask之response

    import os from flask import Flask,render_template,redirect,jsonify,send_file app=Flask(__name__) #开发 ...

  2. Python 如何随机打乱列表(List)排序

    场景: 现在有一个list:[1,2,3,4,5,6],我需要把这个list在输出的时候,是以一种随机打乱的形式输出. 专业点的术语:将一个容器中的数据每次随机逐个遍历一遍. 注意:不是生成一个随机的 ...

  3. format函数格式化显示的方法

    数字 格式 输出 描述 3.1415926 {:.2f} 3.14 保留小数点后两位 3.1415926 {:+.2f} +3.14 带符号保留小数点后两位 -1 {:+.2f} -1.00 带符号保 ...

  4. celery异步消息队列的使用

    1.准备工作 1.1 流程图 2.环境安装 2.1.在Ubuntu中需要安装redis 安装redis $sudo apt-get update $sudo apt-get install redis ...

  5. dede列表页限制标题长度

    {dede:list pagesize ='10' titlelen="45"} <li><a href="[field:arcurl/]"& ...

  6. PAT 1032 Sharing (25分) 从自信到自闭

    题目 To store English words, one method is to use linked lists and store a word letter by letter. To s ...

  7. [批处理教程之MySQL]001.MySQL 常用命令大全

    连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MySQL 首先打开DOS窗口,然后进入目录mysql\bin,再键入命令mysql -u root -p ...

  8. Error C2079 'CMFCPropertySheet::m_wndOutlookBar' uses undefined class 'CMFCOutlookBar'

    Severity Code Description Project File Line Suppression StateError C2079 'CMFCPropertySheet::m_wndOu ...

  9. Python之TestLink篇

    如何让时间变慢? 你们不知道吧,这个时候翻开书,时间又变慢了一倍,可以这样延年益寿,哈哈哈 ------------------------------------------------------ ...

  10. 11 . Python3之异常,调试和测试

    12.Python3入门之异常.调试和测试 在程序运行过程中,总会遇到各种各样的错误. 有的错误是程序编写有问题造成的,比如本应该输出整数结果输出了字符串,这种错误我们通常称之为bug,bug是必须修 ...