打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Py之pytest-shutil:Python库之pytest-shutil简介、安装、使用方法之详细攻略

Py之pytest-shutil:Python库之pytest-shutil简介、安装、使用方法之详细攻略


pytest-shutil简介

    这个库是一个很好的用于自动测试的Unix shell和环境管理工具包。下面是可用函数的摘要,请查看完整列表的源代码。

pytest-shutil安装

pip install pytest-shutil

pytest-shutil函数使用

import shutil

copy()

功能:复制文件
格式:shutil.copy('来源文件','目标地址')
返回值:复制之后的路径

copy2()

功能:复制文件,保留元数据
格式:shutil.copy2('来源文件','目标地址')
返回值:复制之后的路径

copyfileobj()

将一个文件的内容拷贝的另外一个文件当中
格式:shutil.copyfileobj(open(来源文件,'r'),open('目标文件','w'))
返回值:无

copyfile()

功能:将一个文件的内容拷贝的另外一个文件当中
格式:shutil.copyfile(来源文件,目标文件)
返回值:目标文件的路径

copytree()

功能:复制整个文件目录
格式:shutil.copytree(来源目录,目标目录)
返回值:目标目录的路径
注意:无论文件夹是否为空,均可以复制,而且会复制文件夹中的所有内容

copymode()

功能:拷贝权限

copystat()

功能:拷贝元数据(状态)

rmtree()

功能:移除整个目录,无论是否空
格式:shutil.rmtree(目录路径)
返回值:无

move()

功能:移动文件或者文件夹
格式:shutil.move(来源地址,目标地址)
返回值:目标地址

which()

功能:检测命令对应的文件路径
格式:shutil.which('命令字符串’)
返回值:命令文件所在位置
注意:window和linux不太一样。 window的命令都是.exe结尾,linux则不是

disk_usage()

功能:检测磁盘使用信息
格式:disk_usage('盘符’)
返回值:元组

pytest-shutil使用方法

def test_something(workspace):
# Workspaces contain a handle to the path.py path object (see https://pythonhosted.org/path.py) path = workspace.workspace script = path / 'hello.sh’ script.write_text('#!/bin/shn echo hello world!’)
# There is a 'run’ method to execute things relative to the workspace root workspace.run('hello.sh’)

1、pytest_shutil.env: Shell helpers

functiondescription
set_envcontextmanager to set env vars
unset_envcontextmanager to unset env vars
no_envcontextmanager to unset a single env var
no_covcontextmanager to disable coverage in subprocesses

2、pytest_shutil.cmdline: Command-line helpers

functiondescription
umaskcontextmanager to set the umask
chdircontextmanager to change to a directory
TempDircontextmanager for a temporary directory
PrettyFormattersimple text formatter for drawing title, paragrahs, hrs.
copy_filescopy all files from one directory to another
getchcross-platform read of a single character from the screen
whichanaloge of unix which
get_real_python_executablefind our system Python, useful when running under virtualenv

3、pytest_shutil.run: Running things in subprocesses

functiondescription
runrun a command, with options for capturing output, checking return codes.
run_as_mainrun a function as if it was the system entry point
run_module_as_mainrun a module as if it was the system entry point
run_in_subprocessrun a function in a subprocess
run_with_coveragerun a command with coverage enabled
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
tep完整教程帮你突破pytest
pytest
docker学习7-Dockerfile制作自己的镜像文件
pytest入门 -2-分组执行
linux之如何在任意目录执行我常用的脚本文件
入坑 Python 后强烈推荐的一套工具库
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服