简体   繁体   English

@karate 如何将参数传递给加特林模拟类中的特征文件?

[英]@karate How to pass parameter to a feature file in gatling simulation class?

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 .让我们考虑一个场景,我们必须运行“创建帐户 api”的性能测试,它将输入作为标头/路径参数“身份验证令牌”和输入数据,如用户帐户名称。 So for above scenario we have 2 feature file as,所以对于上面的场景,我们有 2 个特征文件,

to run performance test for POST http://baseUrl/auth_param/create/input_data 1. One feature(eg: generateAuth.feature) file which will have the auth token 2. Second feature(createAccount.feature) file which take parameter as a auth token, input data.为 POST http://baseUrl/auth_param/create/input_data运行性能测试 1. 一个特性(例如:generateAuth.feature)文件将具有身份验证令牌 2. 第二个特性(createAccount.feature)文件,它以参数作为参数身份验证令牌,输入数据。

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?在这里,我可以从为 createAccount.feature 文件输入的 generateAuth.feature 文件中读取身份验证,以便我可以作为参数传递吗? Please suggest me how to pass parameters to createAccount.feature while calling in karateFeature method.请建议我如何在调用 karateFeature 方法时将参数传递给 createAccount.feature。

Let me put a requirement here,让我在这里提出一个要求,

let's say we have some feature files for CRUD operations on a particular data.假设我们有一些用于对特定数据进行 CRUD 操作的特征文件。 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.只需使用 CRUD 文件来测试单个流。

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.创建新的 4 个性能测试特征文件(每个 CRUD 方法一个)并在各自的测试特征文件中调用这些 CRUD 特征文件。 Finally we just call test feature files in the respective gatling simulation class.最后,我们只在各自的 gatling 模拟类中调用测试特征文件。 **(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.只需在相应的 gatling 模拟类中调用 CRUD 文件并将所需的参数传递给它们。 (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) (在这种情况下,我们只需要创建 4 个模拟类并在基本操作上运行它们,如创建、读取、删除等)

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只需使用普通的空手道概念,例如karate-config.js

You can easily switch environments by setting the karate.env system property.您可以通过设置karate.env系统属性轻松切换环境

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.请使用 SINGLE 功能。 I suggest you move the generateAuth into the Background of the feature.我建议您将generateAuth移到该功能的Background中。 Also refer to the docs on callSingle() for advanced options.另请参阅callSingle()上的文档以了解高级选项。

If you are expecting 2 feature files to magically share data that is not possible and not needed if you structure your tests correctly.如果您期望 2 个功能文件神奇地共享数据,而这些数据在您正确构建测试的情况下是不可能且不需要的。

If you really really need this, please create a Java singleton and access it from each feature.如果你真的需要这个,请创建一个 Java 单例并从每个功能访问它。 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 :编辑:在空手道 0.9.0 以后,如果功能具有标签,您可以在功能中调用单个场景

classpath:animals/cats/create.feature@sometagname

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM