Testing shell commands from Python
如何测试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的更多相关文章
- Run Shell Commands in Python
subprocess.call This is the recommended way to run shell commands in Python compared with old-fashio ...
- linux下Tab及shell 补全python
Python自动补全 Python自动补全有vim编辑下和python交互模式下,下面分别介绍如何在这2种情况下实现Tab键自动补全. vim python自动补全插件:pydiction 可以实现下 ...
- shell如何向python传递参数,shell如何接受python的返回值
1.shell如何向python传递参数 shell脚本 python $sendmailCommandPath $optDate python脚本 lastDateFormat = sys.argv ...
- Frequently Used Shell Commands
[Common Use Shell Commands] 1.ps aux:查看当前所有进程 ,以用户名为主键.可以查看到 USER.PID.COMMAND(binary所有位置) 2.netstat ...
- 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 ...
- shell脚本安装python、pip--这种写法是错误的---每一个命令执行完都要判断是否执行成功,否则无法进行下一步
shell脚本安装python.pip--不需要选择安装项目--不管用总报错,必须带上判断符号,while没有这种用法,写在这里为了以后少走弯路,所以不要用下面的执行了 首先把pip-.tgz 安装包 ...
- 有米实习-用到的shell脚本和Python脚本记录
Shell:LOG_DATE=`date -d "1 day ago" +%Y-%m-%d` #以指定格式设置一天前的年份月份日期 aws s3 ls $LAST5_BASE_PA ...
- 【原】Gradle调用shell脚本和python脚本并传参
最近由于项目自动化构建的需要,研究了下gradle调用脚本并传参的用法,在此作个总结. Pre build.gradle中定义了$jenkinsJobName $jenkinsBuild两个Jenki ...
- windows python3.2 shell环境(python叫做解释器)
[进入python的shell 环境:](python里称作命令解释器,windows叫做cmd,unix叫做shell) cmd 输入set path=%path%;e:\python2.7然后输 ...
随机推荐
- Linux系统——本地yum仓库安装
一.yum仓库概述 yum是基于rpm包管理,能够从指定的服务器自动下载rpm包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软件包,无需繁琐地一次次下载.安装. 二.yum仓库安装的方式 ...
- XDU 1111
对于一排n个正方形,有f(n)种方案达成目标,若第n个块是白色,则有f(n-1)种方案,若第n个块是黑色,则第n-1个块必为白色,那么有f(n-2)+n-2种方案. 则f(n)=f(n-1)+f(n- ...
- 64. Minimum Path Sum(最小走棋盘 动态规划)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- TabLayout与ViewPager同步后Tab的标题不显示
一.概述 1.1 问题描述 TabLayout+ViewPager后,TabLayout的TabItem不显示的问题: 1.2 截图 二.结论 mTabs.setupWithViewPager(mVi ...
- 清除list或者map集合,减少内存的占用率
1.在编写程序对数据处理的时候我们经常会使用到list和map集合,有些时候我们存到集合里的数据只是暂时性的,在验证完或者使用完之后尽量对list或者map清空,及list.clear()后者map. ...
- 20145322 《Java程序设计》第6周学习总结
20145322 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 10.1.1 串流设计的概念 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. ...
- 图片上传oss--先拿server端签名再上传oss,返回id值
目前项目oss阿里云存储图片,图片上传主要步骤是:前端从服务端拿到签名signature,再上传到oss上busket里,上传成功返回图片id (imgId),最后再给server端: 注:官网上有个 ...
- 二叉树的层次遍历(Java代码实现)
与树的前中后序遍历的DFS思想不同,层次遍历用到的是BFS思想.一般DFS用递归去实现(也可以用栈实现),BFS需要用队列去实现. 层次遍历的步骤是: 1.对于不为空的结点,先把该结点加入到队列中 2 ...
- vROPS中获取虚拟机在VC中的UUID
vROPS中虚拟机对象的ID为resourceID,跟vCenter中虚拟机的UUID是不一致的,因此想要将vROPS中的虚拟机和vCenter中的虚拟机对应肯定不能靠虚拟机名称,而是一定要靠UUID ...
- git如何生成单个文件的补丁
背景:有时候碰到一个commit包含了好几个文件的修改,但是我只需要其中一个文件的修改内容,那么这时候就需要以下方法来生成这一个文件对应修改内容的补丁 答:git format-patch " ...