python学习

部署运行你感兴趣的模型镜像

1.pytest

Microsoft Windows [版本 10.0.22631.5472]
(c) Microsoft Corporation。保留所有权利。

C:\Users\86136>pytest --version
'pytest' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

C:\Users\86136>pip install pytest
Collecting pytest
  Downloading pytest-8.4.1-py3-none-any.whl.metadata (7.7 kB)
Collecting colorama>=0.4 (from pytest)
  Downloading colorama-0.4.6-py2.py3-none-any.whl.metadata (17 kB)
Collecting iniconfig>=1 (from pytest)
  Downloading iniconfig-2.1.0-py3-none-any.whl.metadata (2.7 kB)
Requirement already satisfied: packaging>=20 in d:\mypython\python\lib\site-packages (from pytest) (25.0)
Collecting pluggy<2,>=1.5 (from pytest)
  Downloading pluggy-1.6.0-py3-none-any.whl.metadata (4.8 kB)
Collecting pygments>=2.7.2 (from pytest)
  Downloading pygments-2.19.2-py3-none-any.whl.metadata (2.5 kB)
Downloading pytest-8.4.1-py3-none-any.whl (365 kB)
Downloading pluggy-1.6.0-py3-none-any.whl (20 kB)
Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Downloading iniconfig-2.1.0-py3-none-any.whl (6.0 kB)
Downloading pygments-2.19.2-py3-none-any.whl (1.2 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 3.1 MB/s eta 0:00:00
Installing collected packages: pygments, pluggy, iniconfig, colorama, pytest
Successfully installed colorama-0.4.6 iniconfig-2.1.0 pluggy-1.6.0 pygments-2.19.2 pytest-8.4.1

C:\Users\86136>pip install-html
ERROR: unknown command "install-html" - maybe you meant "install"

C:\Users\86136>pip install pytest-html
Collecting pytest-html
  Downloading pytest_html-4.1.1-py3-none-any.whl.metadata (3.9 kB)
Requirement already satisfied: jinja2>=3.0.0 in d:\mypython\python\lib\site-packages (from pytest-html) (3.1.6)
Collecting pytest-metadata>=2.0.0 (from pytest-html)
  Downloading pytest_metadata-3.1.1-py3-none-any.whl.metadata (8.6 kB)
Requirement already satisfied: pytest>=7.0.0 in d:\mypython\python\lib\site-packages (from pytest-html) (8.4.1)
Requirement already satisfied: MarkupSafe>=2.0 in d:\mypython\python\lib\site-packages (from jinja2>=3.0.0->pytest-html) (3.0.2)
Requirement already satisfied: colorama>=0.4 in d:\mypython\python\lib\site-packages (from pytest>=7.0.0->pytest-html) (0.4.6)
Requirement already satisfied: iniconfig>=1 in d:\mypython\python\lib\site-packages (from pytest>=7.0.0->pytest-html) (2.1.0)
Requirement already satisfied: packaging>=20 in d:\mypython\python\lib\site-packages (from pytest>=7.0.0->pytest-html) (25.0)
Requirement already satisfied: pluggy<2,>=1.5 in d:\mypython\python\lib\site-packages (from pytest>=7.0.0->pytest-html) (1.6.0)
Requirement already satisfied: pygments>=2.7.2 in d:\mypython\python\lib\site-packages (from pytest>=7.0.0->pytest-html) (2.19.2)
Downloading pytest_html-4.1.1-py3-none-any.whl (23 kB)
Downloading pytest_metadata-3.1.1-py3-none-any.whl (11 kB)
Installing collected packages: pytest-metadata, pytest-html
Successfully installed pytest-html-4.1.1 pytest-metadata-3.1.1

C:\Users\86136>pip install pytest-xdist
Collecting pytest-xdist
  Downloading pytest_xdist-3.7.0-py3-none-any.whl.metadata (3.0 kB)
Collecting execnet>=2.1 (from pytest-xdist)
  Downloading execnet-2.1.1-py3-none-any.whl.metadata (2.9 kB)
Requirement already satisfied: pytest>=7.0.0 in d:\mypython\python\lib\site-packages (from pytest-xdist) (8.4.1)
Requirement already satisfied: colorama>=0.4 in d:\mypython\python\lib\site-packages (from pytest>=7.0.0->pytest-xdist) (0.4.6)
Requirement already satisfied: iniconfig>=1 in d:\mypython\python\lib\site-packages (from pytest>=7.0.0->pytest-xdist) (2.1.0)
Requirement already satisfied: packaging>=20 in d:\mypython\python\lib\site-packages (from pytest>=7.0.0->pytest-xdist) (25.0)
Requirement already satisfied: pluggy<2,>=1.5 in d:\mypython\python\lib\site-packages (from pytest>=7.0.0->pytest-xdist) (1.6.0)
Requirement already satisfied: pygments>=2.7.2 in d:\mypython\python\lib\site-packages (from pytest>=7.0.0->pytest-xdist) (2.19.2)
Downloading pytest_xdist-3.7.0-py3-none-any.whl (46 kB)
Downloading execnet-2.1.1-py3-none-any.whl (40 kB)
Installing collected packages: execnet, pytest-xdist
Successfully installed execnet-2.1.1 pytest-xdist-3.7.0

C:\Users\86136>pip install pytest-ordering
Collecting pytest-ordering
  Downloading pytest_ordering-0.6-py3-none-any.whl.metadata (1.1 kB)
Requirement already satisfied: pytest in d:\mypython\python\lib\site-packages (from pytest-ordering) (8.4.1)
Requirement already satisfied: colorama>=0.4 in d:\mypython\python\lib\site-packages (from pytest->pytest-ordering) (0.4.6)
Requirement already satisfied: iniconfig>=1 in d:\mypython\python\lib\site-packages (from pytest->pytest-ordering) (2.1.0)
Requirement already satisfied: packaging>=20 in d:\mypython\python\lib\site-packages (from pytest->pytest-ordering) (25.0)
Requirement already satisfied: pluggy<2,>=1.5 in d:\mypython\python\lib\site-packages (from pytest->pytest-ordering) (1.6.0)
Requirement already satisfied: pygments>=2.7.2 in d:\mypython\python\lib\site-packages (from pytest->pytest-ordering) (2.19.2)
Downloading pytest_ordering-0.6-py3-none-any.whl (4.6 kB)
Installing collected packages: pytest-ordering
Successfully installed pytest-ordering-0.6

C:\Users\86136>pip install pytest-returnfailures
ERROR: Could not find a version that satisfies the requirement pytest-returnfailures (from versions: none)
ERROR: No matching distribution found for pytest-returnfailures

C:\Users\86136>pip install pytest-rerunnfailures
ERROR: Could not find a version that satisfies the requirement pytest-rerunnfailures (from versions: none)
ERROR: No matching distribution found for pytest-rerunnfailures

C:\Users\86136>pip install allure-pytest
Collecting allure-pytest
  Downloading allure_pytest-2.14.3-py3-none-any.whl.metadata (3.0 kB)
Requirement already satisfied: pytest>=4.5.0 in d:\mypython\python\lib\site-packages (from allure-pytest) (8.4.1)
Collecting allure-python-commons==2.14.3 (from allure-pytest)
  Downloading allure_python_commons-2.14.3-py3-none-any.whl.metadata (5.7 kB)
Collecting attrs>=16.0.0 (from allure-python-commons==2.14.3->allure-pytest)
  Downloading attrs-25.3.0-py3-none-any.whl.metadata (10 kB)
Requirement already satisfied: pluggy>=0.4.0 in d:\mypython\python\lib\site-packages (from allure-python-commons==2.14.3->allure-pytest) (1.6.0)
Requirement already satisfied: colorama>=0.4 in d:\mypython\python\lib\site-packages (from pytest>=4.5.0->allure-pytest) (0.4.6)
Requirement already satisfied: iniconfig>=1 in d:\mypython\python\lib\site-packages (from pytest>=4.5.0->allure-pytest) (2.1.0)
Requirement already satisfied: packaging>=20 in d:\mypython\python\lib\site-packages (from pytest>=4.5.0->allure-pytest) (25.0)
Requirement already satisfied: pygments>=2.7.2 in d:\mypython\python\lib\site-packages (from pytest>=4.5.0->allure-pytest) (2.19.2)
Downloading allure_pytest-2.14.3-py3-none-any.whl (11 kB)
Downloading allure_python_commons-2.14.3-py3-none-any.whl (16 kB)
Downloading attrs-25.3.0-py3-none-any.whl (63 kB)
Installing collected packages: attrs, allure-python-commons, allure-pytest
Successfully installed allure-pytest-2.14.3 allure-python-commons-2.14.3 attrs-25.3.0

C:\Users\86136>pip install pytest-rerunfailures
Collecting pytest-rerunfailures
  Downloading pytest_rerunfailures-15.1-py3-none-any.whl.metadata (20 kB)
Requirement already satisfied: packaging>=17.1 in d:\mypython\python\lib\site-packages (from pytest-rerunfailures) (25.0)
Requirement already satisfied: pytest!=8.2.2,>=7.4 in d:\mypython\python\lib\site-packages (from pytest-rerunfailures) (8.4.1)
Requirement already satisfied: colorama>=0.4 in d:\mypython\python\lib\site-packages (from pytest!=8.2.2,>=7.4->pytest-rerunfailures) (0.4.6)
Requirement already satisfied: iniconfig>=1 in d:\mypython\python\lib\site-packages (from pytest!=8.2.2,>=7.4->pytest-rerunfailures) (2.1.0)
Requirement already satisfied: pluggy<2,>=1.5 in d:\mypython\python\lib\site-packages (from pytest!=8.2.2,>=7.4->pytest-rerunfailures) (1.6.0)
Requirement already satisfied: pygments>=2.7.2 in d:\mypython\python\lib\site-packages (from pytest!=8.2.2,>=7.4->pytest-rerunfailures) (2.19.2)
Downloading pytest_rerunfailures-15.1-py3-none-any.whl (13 kB)
Installing collected packages: pytest-rerunfailures
Successfully installed pytest-rerunfailures-15.1

2.运行测试用例

    pytest.main(['-vs','./web_test_case'])
    #pytest.main(['-vs', './web_test_case/test_login.py','--reruns=2'])#失败重跑,重跑2次
    # pytest.main(['-vs', './web_test_case./test_login.py','-n=2']) 多进程运行
主函数模式,参数是列表类型,-s表示输出调试信息,包括打印信息,v是详细信息(类名,方法名),
    # 第二个参数是指定某个模块或者文件夹测试,
    #1.运行所有:pytest.main()
    #2.指定模块:pytest.main(['-vs', './web_test_case./test_login.py'])
    #3.指定文件夹:pytest.main(['-vs', './api_test_case'])
    #4.通过nodeid指定用例运行:nodeid由模块名,分隔符,类名,函数,方法名组成:
        #pytest.main(['-vs', './api_test_case/test_api.py::test_04_fun'])
        #pytest.main(['-vs', './api_test_case/test_api.py::TestApi::test_03'])
命令行模式:
    #1.运行所有:pytest
    #2.指定模块:pytest -vs ./web_test_case./test_login.py
    #3.指定文件夹:pytest -vs ./api_test_case
    #4.通过nodeid指定用例运行:nodeid由模块名,分隔符,类名,函数,方法名组成:
        #pytest -vs ./api_test_case/test_api.py::test_04_fun

通过读取pytest.ini全局配置文件运行,在项目根目录,创建pytest.ini文件,必须是ANSI编码格式,作用是改变pytest默认规则

#参数:
  #-s:输出调试信息,包括打印信息
  #-v:详细信息
  #-vs: 输出调试信息,详细信息
  #-n: 支持多线程或分布式运行用例:pytest -vs ./web_test_case -n 2 这个2是2个线程
  # --reruns 2 : 失败重跑,重跑2次: pytest -vs ./web_test_case/test_login.py --reruns 2
  # -x: 有报错,停止运行
  # -k: 运行指定用例:pytest -vs ./web_test_case/test_login.py -k "test_01"
  # --maxfail==2 : 2个失败,用例停止: pytest -vs ./web_test_case/test_login.py --maxfail 2
  # --html=./report/report.html 生成HTML报告

  #测试用例执行顺序默认从上到下执行,若顺序调换则用@pytest.mark.run(order=1)
  分组执行:
    # 冒烟测试,分布在各个模块pytest -vs -m 'smoke or api'

跳过测试用例
无条件跳过:
@pytest.mark.skip(reason='此用例被跳过')
有条件跳过:
@pytest.mark.skipif(condition=True, reason='此用例被跳过')
@pytest.mark.skipif(age>18, reason='此用例被跳过')

插件安装:创建文件requirements.txt,内容如下:

pytest
pytest-xdist
pytest-ordering
pytest-rerunfailures
pytest-html
allure-pytest

运行文件:

#pip install -r requirements.txt 执行安装插件
知识点2
setup/teatdown,setup_class/teardown_class:框架实现前后置处理:
class TestSetupTeardown:
#所有的用例前执行一次
    def setup_class(self):
        print('\nsetup_class:在每个类执行前的初始化工作,例如创建日志对象,数据库连接,接口的请求等')
    #每个用例前执行一次
    def setup(self):
        print('\nsetup:在执行测试用例前初始化工作:打开浏览器,加载网页等')
    def test_fun_01(self):
        print('\nfun_01执行')
    def test_fun_02(self):
        print('\nfun_02执行')
    def teardown(self):
        print('\nteardown:在执行测试用例后清理工作:关闭浏览器,保存日志等')
    def teardown_class(self):
        print('\nteardown_class:在每个类执行后的清理工作,例如关闭日志对象,断开数据库连接,关闭接口的请求等')


知识点3:@pytest.fixture装饰器实现部分前后置处理
@pytest.fixture()
scope:作用域
param:参数化,支持列表[],元组(),字典列表[{},{}],字典元组({},{})
autouse=True,自动使用装饰器,默认false
ids:使用params时,给每个值设置变量名
name:表示被标记的方法取个变量别名, 设置前置方法的别名,后续代码应用别名,原名不能再用
import pytest

#@pytest.fixture(scope='function')
#@pytest.fixture(scope='function',autouse= True)
#@pytest.fixture(scope='class',autouse= True)
#@pytest.fixture(scope='module',autouse= True)
@pytest.fixture(scope='function',params=['lili',3,'红红'],ids=['lili',3,'red'],name= 'aa')
#name 设置前置方法的别名,后续代码应用别名,原名不能再用
def my_test_fixture(request):
    print('这是前置的方法')
    yield  request.param#yield之后,后面的代码将不再执行,return和yield是返回的意思
    print('这是后置的方法')
    #return request.param

class TestFixture:
    def test_01(self):
        print("test_01")

    def test_02(self,aa):
        print("test_02")
        print('---------------',aa)

class TestFixture2:
    def test_01(self):
        print("TestFixture2:test_01")

    def test_02(self):
        print("TestFixture2:test_02")
知识点4:通过conftest.py和@pytest.fixture结合使用实现全局前置应用(项目全局登录,模块的全局处理),conftest.py文件内容如下:
@pytest.fixture(scope='function')
def all_fixture(request):
    print('全局前置')
    yield
    print('全局后置')
知识点5 断言 assert:列如:assert 1==2
知识点6:pytest结合allure-pytest插件生成测试报告
下载、解压、配置path路径
https://repo.maven.apache.org/maven2/io/qameta/allure/
环境变量:D:\mypython\allure-2.13.7\bin
cmd验证:
C:\Users\86136>allure --version
2.13.5
--alluredir ./temp :生成json格式的临时报告
生成allure报告:
os.system('allure generate ./temp -o ./report --clean')
./temp:临时json报告的路径
-o 输出output
./report 生成allure的报告路径
--clean 清空report路径原来的报告
if __name__ == '__main__':
    #pytest.main(['-vs', './web_test_case/test_login.py','--reruns=2'])#失败重跑,重跑2次
    # pytest.main(['-vs', './web_test_case./test_login.py','-n=2']) 多进程运行
    pytest.main()
    os.system('allure generate ./temp -o ./report --clean')
知识点7:
@pytest.mark.parametrize('参数名','参数值(支持列表[],元组(),字典列表[{},{}],字典元组({},{}))')
  @pytest.mark.parametrize('args',['args1','args2'])
    def test_04(self,args):
        print('test_api_test_04开始测试:',args)

#类似unpack解包
    @pytest.mark.parametrize('name,age',[('张三',18),('理理',19)])
    def test_05(self,name,age):
        print('test_api_test_05开始测试:',name,age)


结果:
api_test_case/test_api.py::TestApi::test_05[\u5f20\u4e09-18] test_api_test_05开始测试: 张三 18
PASSED
api_test_case/test_api.py::TestApi::test_05[\u7406\u7406-19] test_api_test_05开始测试: 理理 19
PASSED

8.yaml四种方式:

lsy:
  name: lsy
  sex: man

lyz: {name: lyz, sex: man}

-
  lax:
    - name: lax
    - sex: man

lax: [{name: lax}, {sex: man}]

Jmeter:

csv批量添加,csv.txt文件:加入导入数据的列表名

name,age,pwd
lili,20,000000
稻草,67,456799

加密接口:

AES ESC Base64

RSA

MD5 SHA HmacSHA (不能解密)

生成报告:

jmeter -n -t [脚本路径] -l [结果文件] -e -o [报告输出目录]

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值