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

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

问题描述

让我们考虑一个场景,我们必须运行创建帐户 api"的性能测试,它将输入作为标头/路径参数身份验证令牌"并输入用户帐户名称等数据.所以对于上面的场景,我们有 2 个特征文件,

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,

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

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.

这是我的模拟课,

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!")
  }
}

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

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,

假设我们有一些用于对特定数据进行 CRUD 操作的特征文件.这是我如何编写功能场景,

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

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

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

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

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

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

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

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.

推荐答案

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

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

您可以通过设置空手道轻松切换环境.env 系统属性.

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

例如:

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

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

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.

如果您期望 2 个特征文件神奇地共享数据,而这些数据在正确构建测试的情况下是不可能且不需要的.

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

如果你真的需要这个,请创建一个 Java 单例并从每个功能访问它.不过完全不推荐这个.

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

在空手道 0.9.0 以后,您可以调用单个场景中的功能,如果它有一个标签:

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天全站免登陆