测试进程执行器(不仅是)。
项目描述
mirakuru
Mirakuru 是一个专为功能性和集成测试而设计的进程编排工具。
也许您希望在启动程序之前启动数据库,或者您可能只需要为测试设置额外的服务。这就是您应该考虑使用 mirakuru 来为您的程序或测试添加超能力的地方。
用法
在依赖于多个进程的项目中,可能需要用测试来保护代码,以验证进程间通信。因此,需要设置所有所需的数据库、辅助和应用程序服务以验证它们的协作。同步(或编排)测试过程与被测试进程可能是一场噩梦。
如果是这样,那么 mirakuru 就是您需要的。
Mirakuru 启动进程并等待明确的运行指示。库提供了七个执行器以适应不同情况
SimpleExecutor - 启动一个进程,不等待任何东西。当需要停止或杀死进程及其子进程时非常有用。所有其他执行器的基类。
Executor - 执行器的基类,用于验证进程是否已启动。
OutputExecutor - 等待进程打印指定的输出。
TCPExecutor - 等待通过 TCP 连接到进程的能力。
UnixSocketExecutor - 等待通过 Unix socket 连接到进程的能力。
HTTPExecutor - 等待成功发出 HEAD 请求(以及之前的 TCP)。
PidExecutor - 等待指定的 .pid 文件存在。
SimpleExecutor
最简单的执行器实现。它简单地将构造函数中传入的进程启动,并报告它正在运行。
from mirakuru import SimpleExecutor
process = SimpleExecutor('my_special_process')
process.start()
# Here you can do your stuff, e.g. communicate with the started process
process.stop()
OutputExecutor
OutputExecutor 是一种执行器,它启动进程,但除非接收到指定的标记/横幅,否则不会报告它已启动。
from mirakuru import OutputExecutor
process = OutputExecutor('my_special_process', banner='processed!')
process.start()
# Here you can do your stuff, e.g. communicate with the started process
process.stop()
启动时发生的操作是这样的:执行器会不断检查启动进程产生的输出,并查找输出中的横幅部分。一旦输出被识别,如示例中的 processed! 被找到。它被认为是启动了,执行器将脚本从等待状态释放,以便工作。
TCPExecutor
是用于启动使用 TCP 连接的进程的执行器。此执行器尝试在指定的主机:端口上与进程连接,以查看它是否开始接受连接。一旦它这样做,它就会将进程报告为已启动,并将代码返回到正常执行。
from mirakuru import TCPExecutor
process = TCPExecutor('my_special_process', host='localhost', port=1234)
process.start()
# Here you can do your stuff, e.g. communicate with the started process
process.stop()
HTTPExecutor
是用于启动 Web 应用程序的执行器。要启动它,你除了命令外,还需要传递一个 URL。此 URL 将用于发出(默认为 HEAD)请求。一旦成功,执行器将被认为是启动了,并将代码返回到正常执行。
from mirakuru import HTTPExecutor
process = HTTPExecutor('my_special_process', url='http://localhost:6543/status')
process.start()
# Here you can do your stuff, e.g. communicate with the started process
process.stop()
然而,此执行器除了 HEAD 请求外,还继承了 TCPExecutor,因此它会首先尝试通过 TCP 连接到进程,以确定是否可以尝试发出 HEAD 请求。
默认情况下,HTTPExecutor 等待其子进程响应 2XX HTTP 状态代码。如果您认为其他代码有效,则需要将这些代码指定在 'status' 参数中。
from mirakuru import HTTPExecutor
process = HTTPExecutor('my_special_process', url='http://localhost:6543/status', status='(200|404)')
process.start()
"status" 参数可以是单个代码整数,如 200、404、500,或正则表达式字符串 - '^(2|4)00$'、'2dd'、'd{3}' 等。
还有可能更改用于向服务器执行请求的方法。默认情况下是 HEAD,但 GET、POST 或其他也是可能的。
from mirakuru import HTTPExecutor
process = HTTPExecutor('my_special_process', url='http://localhost:6543/status', status='(200|404)', method='GET')
process.start()
PidExecutor
是启动给定进程并等待找到指定文件后返回控制的执行器。此类的一个示例用途是编写用于进程的集成测试,这些进程通过创建 .pid 文件来通知它们正在运行。
from mirakuru import PidExecutor
process = PidExecutor('my_special_process', filename='/var/msp/my_special_process.pid')
process.start()
# Here you can do your stuff, e.g. communicate with the started process
process.stop()
from mirakuru import HTTPExecutor
from httplib import HTTPConnection, OK
def test_it_works():
# The ``./http_server`` here launches some HTTP server on the 6543 port,
# but naturally it is not immediate and takes a non-deterministic time:
executor = HTTPExecutor("./http_server", url="http://127.0.0.1:6543/")
# Start the server and wait for it to run (blocking):
executor.start()
# Here the server should be running!
conn = HTTPConnection("127.0.0.1", 6543)
conn.request("GET", "/")
assert conn.getresponse().status is OK
executor.stop()
执行器启动进程的命令可以是字符串或列表。
# command as string
TCPExecutor('python -m smtpd -n -c DebuggingServer localhost:1025', host='localhost', port=1025)
# command as list
TCPExecutor(
['python', '-m', 'smtpd', '-n', '-c', 'DebuggingServer', 'localhost:1025'],
host='localhost', port=1025
)
作为上下文管理器使用
启动
Mirakuru 执行器还可以作为上下文管理器使用。
from mirakuru import HTTPExecutor
with HTTPExecutor('my_special_process', url='http://localhost:6543/status') as process:
# Here you can do your stuff, e.g. communicate with the started process
assert process.running() is True
assert process.running() is False
定义的进程在进入上下文时启动,在退出时退出。
停止
Mirakuru 还允许在给定上下文中停止进程。为此,只需使用内置的 stopped 上下文管理器即可。
from mirakuru import HTTPExecutor
process = HTTPExecutor('my_special_process', url='http://localhost:6543/status').start()
# Here you can do your stuff, e.g. communicate with the started process
with process.stopped():
# Here you will not be able to communicate with the process as it is killed here
assert process.running() is False
assert process.running() is True
定义的进程在进入上下文时停止,在退出时启动。
方法链
Mirakuru 鼓励方法链,这样您就可以内联一些操作,例如。
from mirakuru import SimpleExecutor
command_stdout = SimpleExecutor('my_special_process').start().stop().output
贡献和报告错误
源代码可在以下位置获取: ClearcodeHQ/mirakuru。问题跟踪器位于 GitHub Issues。项目 PyPI 页面。
Windows 支持
坦白说,没有,Python在所需位置的支持略有不同,并且团队没有为Windows开发的经验。然而,我们欢迎那些将允许Windows支持的贡献。
见
此外,随着WSL的引入,原始Windows支持的需求可能不是那么紧急……如果您有任何想法或愿意贡献力量,请从上述问题开始。
发布
首先安装pipenv和–dev依赖项,然后运行
pipenv run tbump [NEW_VERSION]
项目详情
下载文件
下载您平台上的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。