Py之pytest-shutil:Python库之pytest-shutil简介、安装、使用方法之详细攻略
目录
pytest-shutil简介
pytest-shutil安装
pytest-shutil函数使用
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
functiondescriptionset_envcontextmanager to set env varsunset_envcontextmanager to unset env varsno_envcontextmanager to unset a single env varno_covcontextmanager to disable coverage in subprocesses2、pytest_shutil.cmdline: Command-line helpers
functiondescriptionumaskcontextmanager to set the umaskchdircontextmanager to change to a directoryTempDircontextmanager for a temporary directoryPrettyFormattersimple text formatter for drawing title, paragrahs, hrs.copy_filescopy all files from one directory to anothergetchcross-platform read of a single character from the screenwhichanaloge of unix whichget_real_python_executablefind our system Python, useful when running under virtualenv3、pytest_shutil.run: Running things in subprocesses
functiondescriptionrunrun a command, with options for capturing output, checking return codes.run_as_mainrun a function as if it was the system entry pointrun_module_as_mainrun a module as if it was the system entry pointrun_in_subprocessrun a function in a subprocessrun_with_coveragerun a command with coverage enabled