在stanalone运行中在空手道DSL功能文件中使用模拟 [英] Using mocks in Karate DSL feature file with stanalone run

查看:86
本文介绍了在stanalone运行中在空手道DSL功能文件中使用模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有REST服务,用不同于Java的语言编写. 它几乎没有来自其他REST服务的依赖性.

I have REST service, written in language different from Java. It have few dependencies from other REST services.

例如正在开发和测试的服务是 A ,其他服务分别是 B C .

For example service under development and testing is A, other services are respectively B and C.

我想为 A 运行系统测试,某些测试需要 B 或/和 C 联机并执行来自的查询> A .

I want to run system test for A, some tests require B or/and C to be online and perform queries from A.

我写了b-mock.featue和c-mock.feature来代表模拟服务.

I wrote b-mock.featue and c-mock.feature to represent that services in mock.

我还编写了一些a-test-smth.feature文件来对 A

Also I wrote some a-test-smth.feature files to run test against A

是否可以在a-test-smth.feature中添加一些信息以启用一些用于具体测试的模拟?

Is it possible to add some information into a-test-smth.feature to enable some mocks for concrete test?

现在,我应该运行两次独立的karate.jar,首先-进行模拟.第二-用于运行测试.这种方法行得通,但是,我无法接受:

Now I should run standalone karate.jar twice, first - for mocking. second - for run tests. That approach works, but, I can't ceck that:

  • A 的某些API调用不是必需的, B C
  • 无法模拟服务 B ,例如无法缓慢或错误地获取响应答案
  • some API calls to A not required B or C
  • can't emulate service B down or for example slow or incorrect response answer fetching

谢谢.

推荐答案

您是否正在使用Java?如果是这样,那么最好的方法是使用Java代码执行测试的设置.您可以为B和c启动2个模拟,然后然后为服务A启动主要测试.最后,如果需要进行清理.

Are you using Java ? If so then the best approach is to perform the set-up of your test in Java code. You can start 2 mocks for B and c and then start the main test for your service A. And at the end do clean-up if needed.

您可以以此为例: https ://github.com/intuit/karate/tree/master/karate-netty#consumer-provider-example

第3行显示了如何启动模拟并运行空手道测试.

Row 3 shows how you can start a mock and run a Karate test.

如果您不使用Java,而只想使用 独立JAR,则实际上可以使用Java-interop,而且非常简单,我刚刚尝试过.

If you are not using Java and would like to use only the stand-alone JAR, it is actually possible using Java-interop and quite easy, I just tried it.

首先创建一段足够聪明的JavaScript代码以启动空手道模拟:

First create this bit of JavaScript code that is smart enough to start a Karate mock:

function() {
  var Mock = Java.type('com.intuit.karate.netty.FeatureServer');
  var file = new java.io.File('src/test/java/mock/web/cats-mock.feature');
  var server = Mock.start(file, 0, false, null);
  return server.port;
}

这就是您在主要空手道测试的Background中的外观.您可以看到如何根据需要执行一些条件逻辑,并且有很多方法可以根据您的环境进行更改.

And this is how it can look in the Background of your main Karate test. You can see how you can do some conditional logic if needed and you have plenty of ways to change things based on your environment.

Background:
    * def starter = read('start-mock.js')
    * def port = karate.env == 'mock' ? starter() : 8080
    * url 'http://localhost:' + port + '/cats'

这能回答您的问题吗?让我知道,我将把这个技巧添加到文档中!

Does this answer your question ? Let me know and I will add this trick to the documentation !

这篇关于在stanalone运行中在空手道DSL功能文件中使用模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆