How to set up the JWT token before the VS loadtest

后端 未结 1 1786
死守一世寂寞
死守一世寂寞 2021-01-25 18:46

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.

相关标签:
1条回答
  • 2021-01-25 19:12

    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.

    0 讨论(0)
提交回复
热议问题