如何从继承自 WebTest 的 class 中的代码访问 运行 设置 -> 上下文参数
How can I access a Run Setting -> Context Parameter from code in a class inheriting from WebTest
VS2013,我有一个负载测试。我希望能够 运行 在各种环境(即开发、暂存、生产)中进行此负载测试。我希望能够在 运行 时间传递一些特定于环境的信息,例如服务器 url、登录信息等。所以我在考虑配置或上下文参数。我可以找到很多关于如何添加上下文参数 ( https://msdn.microsoft.com/en-us/library/ff406971(v=vs.120).aspx ) 的信息,但是 none 关于如何访问所述参数的信息。有人能指出我正确的方向吗?上下文参数是否适合这项工作?
我能想到的将 "configuration" 添加到 VS 负载测试的最佳方法是使用上下文参数。为此,您需要:
- 添加上下文参数(右键单击所需的 运行 设置)。
- 然后,为了能够通过编码访问这些参数
WebTests,你需要创建一个负载测试插件class(参见下面的插件class)。
- 最后你需要将这个插件添加到负载测试中(右键单击负载测试的根节点)。
此 class 将添加所有活动的 运行 设置的上下文参数您的编码 WebTest。
using System;
using Microsoft.VisualStudio.TestTools.LoadTesting;
namespace VerificationLoadTest
{
public class ContextParameterLoadTestPlugin : ILoadTestPlugin
{
LoadTest LoadTest;
public void Initialize(LoadTest loadTest)
{
this.LoadTest = loadTest;
this.LoadTest.TestStarting += new EventHandler<TestStartingEventArgs>(TestStarting);
}
void TestStarting(object sender, TestStartingEventArgs e)
{
foreach (string key in LoadTest.Context.Keys)
{
e.TestContextProperties.Add(key, LoadTest.Context[key]);
}
}
}
}
编译项目并将插件添加到负载测试(上面的第 3 步)后,可以像这样访问参数:
public class ContextParameterTest : WebTest
{
public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
var serviceUrl = this.Context[ContextParamKey].ToString();
WebTestRequest webTestRequest = new WebTestRequest(serviceUrl);
... build your request
request.PostRequest += request_PostRequest;
yield return request;
request = null;
}
}
您提供的link包含字词
... if the Web performance test in the load test uses the same context parameter name as a context parameter in the load test, the context parameter in the load test will override the context parameter that is used in the Web performance test
通过将 then 括在双花括号中,可以在 Web 测试中的许多地方使用上下文参数。例如,上下文参数中的值,比如 MyContextParameter
,可以用在 URL 或查询字符串等中,样式为 {{MyContextParameter}}
也可以是 some text {{MyContextParameter}} and more text
.
可以使用e.WebTest.Context
字段在网络测试插件或网络测试请求插件中读取上下文参数,其中e
是...EventArgs
参数。
在 VS 2015 中,我认为在这方面没有太大区别,所有 负载测试上下文条目在 C# 中通过 WebTest.Context
可用,并且执行不需要通过插件显式添加。在负载测试插件中,您还可以通过 LoadTest.Context
仅访问负载测试上下文。
我发现调试测试在调查这些类型的问题时非常有用。除了 运行 之外,负载测试、Web 测试 (*.webtest) 和编码的 Web 测试(通过上下文菜单)都支持调试操作。这将遵守您的断点并允许您查看 WebTest
实例的所有成员。如果您不使用编码的 Web 测试,则必须在某处使用 C# 代码来放置断点,例如通过自定义 plug-in、提取器等
VS2013,我有一个负载测试。我希望能够 运行 在各种环境(即开发、暂存、生产)中进行此负载测试。我希望能够在 运行 时间传递一些特定于环境的信息,例如服务器 url、登录信息等。所以我在考虑配置或上下文参数。我可以找到很多关于如何添加上下文参数 ( https://msdn.microsoft.com/en-us/library/ff406971(v=vs.120).aspx ) 的信息,但是 none 关于如何访问所述参数的信息。有人能指出我正确的方向吗?上下文参数是否适合这项工作?
我能想到的将 "configuration" 添加到 VS 负载测试的最佳方法是使用上下文参数。为此,您需要:
- 添加上下文参数(右键单击所需的 运行 设置)。
- 然后,为了能够通过编码访问这些参数 WebTests,你需要创建一个负载测试插件class(参见下面的插件class)。
- 最后你需要将这个插件添加到负载测试中(右键单击负载测试的根节点)。
此 class 将添加所有活动的 运行 设置的上下文参数您的编码 WebTest。
using System;
using Microsoft.VisualStudio.TestTools.LoadTesting;
namespace VerificationLoadTest
{
public class ContextParameterLoadTestPlugin : ILoadTestPlugin
{
LoadTest LoadTest;
public void Initialize(LoadTest loadTest)
{
this.LoadTest = loadTest;
this.LoadTest.TestStarting += new EventHandler<TestStartingEventArgs>(TestStarting);
}
void TestStarting(object sender, TestStartingEventArgs e)
{
foreach (string key in LoadTest.Context.Keys)
{
e.TestContextProperties.Add(key, LoadTest.Context[key]);
}
}
}
}
编译项目并将插件添加到负载测试(上面的第 3 步)后,可以像这样访问参数:
public class ContextParameterTest : WebTest
{
public override IEnumerator<WebTestRequest> GetRequestEnumerator()
{
var serviceUrl = this.Context[ContextParamKey].ToString();
WebTestRequest webTestRequest = new WebTestRequest(serviceUrl);
... build your request
request.PostRequest += request_PostRequest;
yield return request;
request = null;
}
}
您提供的link包含字词
... if the Web performance test in the load test uses the same context parameter name as a context parameter in the load test, the context parameter in the load test will override the context parameter that is used in the Web performance test
通过将 then 括在双花括号中,可以在 Web 测试中的许多地方使用上下文参数。例如,上下文参数中的值,比如 MyContextParameter
,可以用在 URL 或查询字符串等中,样式为 {{MyContextParameter}}
也可以是 some text {{MyContextParameter}} and more text
.
可以使用e.WebTest.Context
字段在网络测试插件或网络测试请求插件中读取上下文参数,其中e
是...EventArgs
参数。
在 VS 2015 中,我认为在这方面没有太大区别,所有 负载测试上下文条目在 C# 中通过 WebTest.Context
可用,并且执行不需要通过插件显式添加。在负载测试插件中,您还可以通过 LoadTest.Context
仅访问负载测试上下文。
我发现调试测试在调查这些类型的问题时非常有用。除了 运行 之外,负载测试、Web 测试 (*.webtest) 和编码的 Web 测试(通过上下文菜单)都支持调试操作。这将遵守您的断点并允许您查看 WebTest
实例的所有成员。如果您不使用编码的 Web 测试,则必须在某处使用 C# 代码来放置断点,例如通过自定义 plug-in、提取器等