问题
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