分享

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

 处女座的程序猿 2021-09-28

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:///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

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多