如何测试shell命令?最近,我遇到了一些情况,我想运行shell命令进行测试,Python称为万能胶水语言,一些自动化测试都可以完成,目前手头的工作都是用python完成的.但是无法从Python中找到有关如何进行测试的教程。在google上搜索了很久之后,我找到了一个适合我的解决方案,也许它也适合你!

为什么使用Python进行测试?

您可以使用专用工具来测试shell命令。为什么选择Python而不是其他语言或者工具?但是如果您经常使用Python,那么使用Python是有意义的,因为Python已经包含了健壮的测试功能,这些功能很容易与其他工具集成。通过测试Python中的shell命令,您可以利用这些工具,并避免跟踪不同地方的测试。另外,如果您已经熟悉用Python编写测试,那么为shell命令编写测试就变得轻而易举了。

Use cases

For the Netherlands eScience Center Python package template, I wanted tests to verify that the generated package can be installed, that the tests can be run, and that the documentation can be generated without errors. My Python text processing package nlppln contains CWL specifications of text mining tools, that can be validated by running them using a command line tool called cwltool. Another use case would be testing your package’s console scripts(although in this case it might be more convenient to use a package for creating command line interfaces that comes with built-in testing functionality, such as Click).

The sh package

You can run shell commands from Python using the subprocess module from the Python standard library. However, using this module is a hassle, because you have to do all the error handling yourself. Sh is a Python package that takes care of all that and allows you to run shell commands using a single line of code. If you want to run python setup.py install, all you have to do is:

import sh
sh.python(['setup.py', 'install'])

If you want to run foo and it is installed on your system, you can just do sh.foo().

Writing a test

So how can we use this for testing? Let’s look at an example. For the Python template, I want to test whether a project generated from the cookiecuttertemplate can be installed without errors. The goal of the template is to help users write high quality code with less effort, and having an installable empty project is a good first step. The code for the test that tests the installation is:

import pytest
import os
import sh
def test_install(cookies):
# generate a temporary project using the cookiecutter
# cookies fixture
project = cookies.bake()
  # remember the directory where tests should be run from
cwd = os.getcwd()
# change directories to the generated project directory
# (the installation command must be run from here)
os.chdir(str(project.project))
  try:
# run the shell command
sh.python(['setup.py', 'install'])
except sh.ErrorReturnCode as e:
# print the error, so we know what went wrong
print(e)
# make sure the test fails
pytest.fail(e)
finally:
# always change directories to the test directory
os.chdir(cwd)

That is all there is to it!

More examples

Of course there is a lot more you can do, e.g., checking whether files existafter running a shell command, or verifying the contents of generated files. What use cases can you come up with?

On Windows: use subprocess

Sh does not work on Windows. If you need to test shell commands on Windows, you are stuck with subprocess. Provenance tracking package recipycontains some nice examples of tests using subprocess that might help you on your way.

安装sh

https://amoffat.github.io/sh/

Testing shell commands from Python的更多相关文章

  1. Run Shell Commands in Python

    subprocess.call This is the recommended way to run shell commands in Python compared with old-fashio ...

  2. linux下Tab及shell 补全python

    Python自动补全 Python自动补全有vim编辑下和python交互模式下,下面分别介绍如何在这2种情况下实现Tab键自动补全. vim python自动补全插件:pydiction 可以实现下 ...

  3. shell如何向python传递参数,shell如何接受python的返回值

    1.shell如何向python传递参数 shell脚本 python $sendmailCommandPath $optDate python脚本 lastDateFormat = sys.argv ...

  4. Frequently Used Shell Commands

    [Common Use Shell Commands] 1.ps aux:查看当前所有进程 ,以用户名为主键.可以查看到 USER.PID.COMMAND(binary所有位置) 2.netstat ...

  5. A/B Testing with Practice in Python (Part Two)

    This is the second part of A/B testing notes, which contains the practical issues and alternatives o ...

  6. shell脚本安装python、pip--这种写法是错误的---每一个命令执行完都要判断是否执行成功,否则无法进行下一步

    shell脚本安装python.pip--不需要选择安装项目--不管用总报错,必须带上判断符号,while没有这种用法,写在这里为了以后少走弯路,所以不要用下面的执行了 首先把pip-.tgz 安装包 ...

  7. 有米实习-用到的shell脚本和Python脚本记录

    Shell:LOG_DATE=`date -d "1 day ago" +%Y-%m-%d` #以指定格式设置一天前的年份月份日期 aws s3 ls $LAST5_BASE_PA ...

  8. 【原】Gradle调用shell脚本和python脚本并传参

    最近由于项目自动化构建的需要,研究了下gradle调用脚本并传参的用法,在此作个总结. Pre build.gradle中定义了$jenkinsJobName $jenkinsBuild两个Jenki ...

  9. windows python3.2 shell环境(python叫做解释器)

    [进入python的shell 环境:](python里称作命令解释器,windows叫做cmd,unix叫做shell) cmd  输入set path=%path%;e:\python2.7然后输 ...

随机推荐

  1. VMware+CentOS7+jdk1.7+hadoop2.4.1

    1.工具 CentOS7:去官网下载,然后找到阿里的镜像,DVD版本就好,4个G大小https://www.centos.org/download/ vmware:去官网下载最新版本 2.要点 先装V ...

  2. WEB前端研发工程师编程能力成长之路(1)

    [背景] 如果你是刚进入WEB前端研发领域,想试试这潭水有多深,看这篇文章吧: 如果你是做了两三年WEB产品前端研发,迷茫找不着提高之路,看这篇文章吧: 如果你是四五年的前端开发高手,没有难题能难得住 ...

  3. PL/SQL Developer 登录 Oracle 12c和Win10下安装Oracle 11g

    安装了Oracle 12c 后使用PL/SQL Developer怎么也不能连接到Oracle 12c.网上找一下,按照fcflying所说的操作成功了,所以做个笔记: 1)安装Oracle 12c ...

  4. PKU 2531 Network Saboteur(dfs+剪枝||随机化算法)

    题目大意:原题链接 给定n个节点,任意两个节点之间有权值,把这n个节点分成A,B两个集合,使得A集合中的每一节点与B集合中的每一节点两两结合(即有|A|*|B|种结合方式)权值之和最大. 标记:A集合 ...

  5. web前端基础——初识JavaScript

    1 JavaScript概述 JavaScript是一种属于网络的脚本语言,已经被广泛用于Web应用开发,常用来为网页添加各式各样的动态功能,为用户提供更流畅美观的浏览效果.通常JavaScript脚 ...

  6. windows上使用clang编译程序

    环境:windows7,64位 1.下载并安装llvm,安装包里除了llvm,也有clang: http://releases.llvm.org/5.0.0/LLVM-5.0.0-win64.exe ...

  7. [原创] css中的绝对定位和相对定位

    我对博客的认识是:记录问题,解决问题,分享知识.如果有轮子,我不需要造轮子.   首先,定位无论是相对定位还是绝对定位,必须有一个参考项,而这个参考项,专业术语称之为 包含块,这里的包含块是指在定位时 ...

  8. 【c++ primer, 5e】返回类型和return语句

    [无返回值函数] 1.在c++的void函数中,可以显式地使用return;语句来提前结束函数的调用. [有返回值函数] 1.值是如何被返回的:返回一个值的方式和初始化一个变量或者形参的方式完全一样. ...

  9. iframe自适应高度(兼容多种浏览器)

    http://jingyan.baidu.com/article/b87fe19eaeb2cf5218356896.html 让iframe自适应高度,下面是实现的源码: <div id=&qu ...

  10. 编码解码--url编码解码

    url编码解码,又叫百分号编码,是统一资源定位(URL)编码方式.URL地址(常说网址)规定了常用地数字,字母可以直接使用,另外一批作为特殊用户字符也可以直接用(/,:@等),剩下的其它所有字符必须通 ...