@karate如何在加特林模拟类中将参数传递给特征文件? [英] @karate How to pass parameter to a feature file in gatling simulation class?

查看:44
本文介绍了@karate如何在加特林模拟类中将参数传递给特征文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们考虑一个场景,我们必须对创建帐户api"进行性能测试,该测试将输入作为标头/路径参数"Auth token"并输入诸如用户帐户名之类的数据.因此,对于上述情况,我们有2个功能文件,即

运行POST的性能测试 http://baseUrl/auth_param/create/input_data 1.一个具有身份验证功能的功能(例如:generateAuth.feature)文件代币2.第二个特征(createAccount.feature)文件,其参数为身份验证令牌,输入数据.

这是我的模拟课,

  class< MyClass>扩展模拟{前 {println(模拟即将开始!")}val generateAuthTest = case("generateAuth").exec(karateFeature("classpath:path/generateAuth.feature"))val createAccountTest = censing("test").exec(karateFeature("classpath:path/createAccount.feature"))设置(createAccountTest.inject(rampUsers(1)超过(10秒))).maxDuration(1分钟)后 {println(模拟完成!")}} 

在这里,我可以从为createAccount.feature文件输入的generateAuth.feature文件中读取身份验证,以便我可以将其作为参数传递吗?请建议我在调用karateFeature方法时如何将参数传递给createAccount.feature.

让我在这里提出一个要求,

假设我们有一些功能文件,用于对特定数据进行CRUD操作.在这里,我要如何编写功能方案,

  1. 我将创建新的功能文件来编写场景
  2. 只需使用CRUD文件来测试单个流.

现在,如果我针对单个操作进行性能测试用例,我觉得有2种方法,

  1. 创建新的4个性能测试功能文件(每个CRUD一个)方法),然后在相应的测试中调用这些CRUD功能文件功能文件.最后,我们只是在各自的加特林模拟课程.**(在这种情况下,我最终将创建更多的测试功能文件以及用于性能,我要避免)**
  2. 只需在各自的加特林模拟类中调用CRUD文件,然后将所需的参数传递给它们.(在这种情况下,我们只需要创建4个模拟类,并在诸如创建,读取,删除等操作的基础上运行它们)

这里只是想了解性能测试的第二种方法,用空手道是否可以实现?如果可以,请告诉我如何做?

摘要-我认为使用第3个功能文件(额外)可以实现个别用例,但我不想制作额外的功能文件对于每种情况,这样我就可以避免维护工作并可以采取从功能中重用现有功能文件的优势测试到性能测试.

解决方案

只需使用常规的空手道概念,例如 karate-config.js

通过设置 karate,您可以轻松地切换环境.env 系统属性.

例如:

mvn测试-DargLine =-Dkarate.env = e2e"

编辑问题后,很显然您有一个要测试的单流程.请使用单功能.我建议您将 generateAuth 移至该功能的 Background 中.另请参阅 callSingle()上的文档以获取高级选项.

如果您期望2个功能文件神奇地共享数据,而这些数据正确地构成了测试就不会并且不需要.

如果您确实需要此功能,请创建一个Java单例并从每个功能部件中访问它.完全不建议这样做.

从空手道0.9.0开始,您可以在一个带有标签的功能:

  classpath:animals/cats/create.feature@sometagname 

Let's consider a scenario, we have to run the performance test for "create an account api" which takes input as header/path param "Auth token" and input data like user account name . So for above scenario we have 2 feature file as,

to run performance test for POST http://baseUrl/auth_param/create/input_data 1. One feature(e.g: generateAuth.feature) file which will have the auth token 2. Second feature(createAccount.feature) file which take parameter as a auth token, input data.

Here is my simulation class,

class <MyClass> extends Simulation {

  before {
    println("Simulation is about to start!")
  }
  val generateAuthTest = scenario("generateAuth").exec(karateFeature("classpath:path/generateAuth.feature")) 
  val createAccountTest = scenario("test").exec(karateFeature("classpath:path/createAccount.feature"))
  setUp(
    createAccountTest.inject(rampUsers(1) over (10 seconds))).maxDuration(1 minutes)
  after {
    println("Simulation is finished!")
  }
}

Here, can i read auth from generateAuth.feature file which is input for createAccount.feature file, so that i can pass as a parameter? Please suggest me how to pass parameters to createAccount.feature while calling in karateFeature method.

Let me put a requirement here,

let's say we have some feature files for CRUD operations on a particular data. Here how i go to write functional scenario,

  1. I will create new feature file to write a scenario
  2. just use CRUD files to test a SINGLE flow.

Now if i go for Performance test cases on individual operation, i feel there are 2 ways,

  1. Create new 4 performance test feature files (one for each CRUD method) and call these CRUD feature files in the respective test feature file. Finally we just call test feature files in the respective gatling simulation class. **(In this case, I will end up with creating more test feature files as well simulation classes for performance, which I want to avoid) **
  2. Just call CRUD files in the respective gatling simulation class and pass the required parameters to them.(In this case , we just need to create only 4 simulation classes and run them on the basic of operation like create,read,delete and so on)

Here just wanted to know 2nd way of performance test, is it achievable or not in karate and if yes please let me know how?

Summary- I think its achievable using 3rd feature file (extra) for individual use case but I do not want to make an extra feature file for each case so that I can avoid maintenance work and can take advantage of re-usability of existing feature file from functional test to performance test.

解决方案

Just use the normal Karate concepts such as karate-config.js

You can easily switch environments by setting the karate.env system property.

For example:

mvn test -DargLine="-Dkarate.env=e2e"

EDIT: after you edited your question, it is clear you have a SINGLE flow you want to test. please use a SINGLE feature. I suggest you move the generateAuth into the Background of the feature. Also refer to the docs on callSingle() for advanced options.

If you are expecting 2 feature files to magically share data that is not possible and not needed if you structure your tests correctly.

If you really really need this, please create a Java singleton and access it from each feature. Totally don't recommend this though.

EDIT: In Karate 0.9.0 onwards, you can call a single scenario within a feature if it has a tag:

classpath:animals/cats/create.feature@sometagname

这篇关于@karate如何在加特林模拟类中将参数传递给特征文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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