用于创建(微)服务测试的实用程序。基于mountebank。

mountep的Python项目详细描述


https://snap-ci.com/butla/mountepy/branch/master/build_imagehttps://coveralls.io/repos/butla/mountepy/badge.svg?branch=master&service=githubhttps://requires.io/github/butla/mountepy/requirements.svg?branch=master

用于创建http(micro)服务测试的实用程序。基于Mountebank

mountepy通过在给定的http服务之后生成和清理来工作 进程和mountebank。多亏你不再需要“X开始” 在为你的应用程序运行测试之前。不,你的测试开始了 “X”,只有在需要的时候才可以挂上或挂下,而且次数不限 需要。

  • 测试框架不可知(使用unittest、nose、py.test或其他… 但我喜欢py.test)。
  • 支持对微服务进行快速可靠的端到端测试。他们 不会意识到他们处于某种测试模式。
  • 在Python3.4,Ubuntu14x64上测试。
  • 以下路线图中的规划特征。 如果您有建议,可以将其作为github问题发布。 也欢迎拉取请求:)

我建议使用pytest进行服务过程弹性组合测试 固定装置。您的过程可能会在每个测试套件中启动一次,在每个测试中启动一次, 等

安装

$ pip install mountepy

mountebank(包括nodejs)的独立发行版将是 第一次运行时下载。

如果不希望mountepy下载mountebank:

  1. 安装nodejs和npm。在ubuntu上是
$ sudo apt-get install -y nodejs-legacy npm
  1. 自行安装mountebank
$ npm install -g mountebank --production

示例

mountebank充当外部http服务的模拟。 下面是如何生成mountebank进程,并用存根配置它 在某些http服务中,断言它实际上正在响应。 mountebank进程在with块之后被终止。

# requests is installed alongside Mountepyimportmountepy,requestswithmountepy.Mountebank()asmb:imposter=mb.add_imposter_simple(path='/something',response='mock response')stub_url='http://localhost:{}/something'.format(imposter.port)assertrequests.get(stub_url).text=='mock response'

把你的服务作为一个完整的过程来测试是个好主意。 假设您有一个单文件wsgi(例如烧瓶或瓶子)应用程序 它用字符串响应根路径('\')上的GET。 它可以在RET_STR环境变量中看到。 另外,应用程序需要知道运行哪个端口,所以我们也要传递它 作为环境变量。{port}是mountepy的特殊值。 无论通过与否,都将填写申请的端口 在对象构造期间或从可用端口自动选择。

# port_for is installed alongside Mountepyimportmountepy,requests,port_for,os,sysservice_port=port_for.select_random()service=mountepy.HttpService([sys.executable,'sample_app.py'],port=service_port,env={'PORT':'{port}','RET_STR':'Just some text.'})withservice:assertrequests.get(service.url).text=='Just some text.'

启动在Gunicorn上运行的更复杂的服务 可以这样:

importos,sysgunicorn_path=os.path.join(os.path.dirname(sys.executable),'gunicorn')service_command=[gunicorn_path,'your_package.app:get_app()','--bind',':{port}','--enable-stdio-inheritance','--pythonpath',','.join(sys.path)]service=HttpService(service_command)# You can use start/stop methods instead of using the "with" statement.# It's the same for Mountebank objects.service.start()# now you test stuff...service.stop()

使用mountepy的“真实世界”可以在PyDAS中找到。

测量测试覆盖率

mountepy在一个单独的过程中启动代码,因此通常很难获得 有关测试所涵盖代码的信息。 幸运的是,这个问题由Coverage解决。 见this documentation page

简而言之,您需要:

  • 在每个新的python进程中运行coverage.process_startup()。 (这可以通过安装coverage_pth来实现,但需要注意)
  • COVERAGE_PROCESS_START环境变量设置为.coveragerc
  • 运行测试本身:coverage run (...)coverage combine,然后coverage report -m

同样,请参见PyDAS’s tox.ini进行演示。

运行测试

使用子模块克隆repo,然后安装并运行tox。

$ git clone --recursive git@github.com:butla/mountepy.git
$ sudo pip install tox
$ cd mountepy
$ tox

激励(2015-12-30)

  • 为什么Mountebank?可能是 作为独立应用程序部署,正在积极开发和 支持TCP模拟,可用于模拟中断的HTTP 信息。
  • 为什么不Pretenders? 不支持TCP,而且开发似乎不是真的 主动的。
  • 为什么不WireMock? 不支持TCP,我不想被迫将Java安装到 运行测试,它似乎没有比mountebank更多的功能。
  • 为什么要创建新项目?已经有一个Python Mountebank wrapper,但是 没什么。

许可证

mountepy是根据BSD Zero Clause license授权的。

为什么我没有使用一个更流行的许可证,如麻省理工学院,2或3条款bsd或apache2?好吧,这个实际上等于2条款bsd(我看不出它和mit许可证之间有什么功能上的区别),除了在衍生作品中保留原始许可证文本的规则。所以如果你碰巧重新发现把我的库和你的软件放在一起你不需要附上我的许可证副本。所以你不会因为懒惰而违反任何版权法(例如,我喜欢这样)。不客气。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
日期和现在之间的Java时间   java以适当的方式更新jLabel和jTextField   java如何从PQ(单链表)中删除最大值   java可以通过任何方式找出哪些NSF文件属于Domino服务器   java Velocity 2不会设置属性   如何使用java流迭代索引映射列表   maven依赖项在编译期间工作,但在java运行时失败。lang.NoClassDefFoundError&java。lang.ClassNotFoundException   java有没有办法完全隐藏Web应用程序代码?   Android appcompat v7 21库中的java FadingActionBar错误   Eclipse/com中的java AdMob。谷歌。安卓gms。ads.AdView   java My Service表示它正在运行,但其状态尚不清楚,似乎没有绑定   java无法启动Apache Tomcat Web应用程序容器   JDK:java中的命名约定。可丢弃的   Spring SerSecurity中的java身份验证错误   Maven:使用JDK8编译Java7   java在使用ExecutorService时如何管理内存?   comm.jar通信串行端口java   java LibGDX创建动画