How to set up the JWT token before the VS loadtest

我们两清 提交于 2019-12-02 11:39:31

问题


I am trying to run a Visual Studio Load Test, but before it runs I have to call a login webAPI to generate a JWT token and then use this token in all of the webAPI web tests.

I only need to call the login to generate the token once. The same token value is used in all my other webAPI calls. How can I put this step into my load test before I really trigger the load test?


回答1:


The "Test mix" part of a load test can specify an "initialisation" test that can do the login and collect the token. One of the context parameters (CP) of a load test is the $LoadTestUserContext. This is intended for holding values between tests executed for the same virtual user (VU). The login test can save the token into the $LoadTestUserContext and the other tests can access it from there.

Note that each VU has its own $LoadTestUserContext. If the "percentage of new users" in the scenario is high then the initialisation test for a new VU (i.e. the login test) will be executed many times.

If all the VUs should use the same token value then another possibility is to create a scenario that executes the login test once, by setting the iterations property of that scenario to 1; also set a constant load of 1. This test could have a PostRequest or a PostWebTest plugin that saves the token value into a static variable of some class. A second scenario then runs all of the real load test cases, its "delay start time" property could be a few seconds, long enough to run the login test. A plugin of these tests could write the static value into a CP of the test.



来源:https://stackoverflow.com/questions/49804066/how-to-set-up-the-jwt-token-before-the-vs-loadtest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!