Testing shell commands from Python

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

为何使用Python进行测试?python

您可使用专用工具来测试shell命令。为何选择Python而不是其余语言或者工具?可是若是您常常使用Python,那么使用Python是有意义的,由于Python已经包含了健壮的测试功能,这些功能很容易与其余工具集成。经过测试Python中的shell命令,您能够利用这些工具,并避免跟踪不一样地方的测试。另外,若是您已经熟悉用Python编写测试,那么为shell命令编写测试就变得垂手可得了。git

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).github

 

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

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().windows

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

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!less

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/

相关文章
相关标签/搜索