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
function    description
set_env    contextmanager to set env vars
unset_env    contextmanager to unset env vars
no_env    contextmanager to unset a single env var
no_cov    contextmanager to disable coverage in subprocesses

2、pytest_shutil.cmdline: Command-line helpers
function    description
umask    contextmanager to set the umask
chdir    contextmanager to change to a directory
TempDir    contextmanager for a temporary directory
PrettyFormatter    simple text formatter for drawing title, paragrahs, hrs.
copy_files    copy all files from one directory to another
getch    cross-platform read of a single character from the screen
which    analoge of unix which
get_real_python_executable    find our system Python, useful when running under virtualenv

3、pytest_shutil.run: Running things in subprocesses
function    description
run    run a command, with options for capturing output, checking return codes.
run_as_main    run a function as if it was the system entry point
run_module_as_main    run a module as if it was the system entry point
run_in_subprocess    run a function in a subprocess
run_with_coverage    run a command with coverage enabled

原文链接:https://blog.csdn.net/qq_41185868/article/details/80472368

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值