2013-09-23 39 views
0

我需要从负载测试插件中获取负载测试的测试迭代次数,其中有一个LoadTest对象的实例。我已经搜索了LoadTest对象的属性,并且与通常用于配置负载测试的树视图编辑器相比,感觉有很多缺失。如何从负载测试插件中获取负载测试中的测试迭代次数?

我已经将测试迭代的次数作为Context参数再次定义,并将其传递给我的Web测试,但由于我正在复制数据,因此感觉像是黑客攻击。

class MyLoadTestPlugin : ILoadTestPlugin 
{ 
    private LoadTest loadTest; 

    public void Initialize(LoadTest test) 
    { 
     loadTest = test; 

     loadTest.TestStarting += (_, e) => 
     { 
      // Get # of Test Iterations in load test here, 
      // "loadTest" object does not have nearly as 
      // many properties as it should, compared to 
      // the tree view editor. 
     }; 
    }  
} 

回答

1

一个web测试在当前迭代次数webTest.Context.WebTestIteration(和也作为名为$WebTestIteration上下文参数)。

一个负载测试可以访问当前迭代次数在TestStartingEventArgs对象:

loadTest.TestStarting += ((sender, e) => 
{ 
    int iteration = e.TestIterationNumber; 
}; 

为了证明自己,这些值是相同的,并且有相同的数字没有意外的行为,是跨越重用不同的场景,我编写了这些插件,并检出。

(感谢@AdrianHHH为指出,以前的代码是不完全的)

public class LoadTestIteration : ILoadTestPlugin 
{ 
    List<int> usedTestIterationNumbers = new List<int>(); 
    public void Initialize(LoadTest loadTest) 
    { 
     loadTest.TestStarting += (sender, e) => 
     { 
      e.TestContextProperties["$LoadTest.TestIterationNumber"] = e.TestIterationNumber; 
      System.Diagnostics.Debug.Assert(!usedTestIterationNumbers.Contains(e.TestIterationNumber), "Duplicate LoadTest TestIterationNumber: " + e.TestIterationNumber); 
      usedTestIterationNumbers.Add(e.TestIterationNumber); 
     }; 
    } 
} 

public class TestWebTestIteration : WebTestPlugin 
{ 
    public override void PreWebTest(object sender, PreWebTestEventArgs e) 
    { 
     int lti = (int)e.WebTest.Context["$LoadTest.TestIterationNumber"]; 
     int wti = e.WebTest.Context.WebTestIteration; 
     System.Diagnostics.Debug.Assert(lti == wti, String.Format("$LoadTestIteration {0} differs from $WebTestIteration {1}", lti, wti)); 
    } 
} 
+1

请您澄清一下,随着测试的进行,“lastLti”和“lastWti”如何更新。 – AdrianHHH

2

使用LoadTestPlugin阅读这是一个XML文件.loadtest文件。以下是读取.loadtest文件中的TotalIterations的示例。

using System; 
using Microsoft.VisualStudio.TestTools.LoadTesting; 
using System.IO; 
using System.Xml; 

namespace LoadTest 
{ 
    public class LoadTestPluginImpl : ILoadTestPlugin 
    { 

     LoadTest mLoadTest; 

     static int TotalIterations; 
     public void Initialize(LoadTest loadTest) 
     { 
      mLoadTest = loadTest; 
      //connect to the TestStarting event. 
      mLoadTest.TestStarting += new EventHandler<TestStartingEventArgs>(mLoadTest_TestStarting); 
      ReadTestConfig(); 
     } 

     void mLoadTest_TestStarting(object sender, TestStartingEventArgs e) 
     { 
      //When the test starts, copy the load test context parameters to 
      //the test context parameters 
      foreach (string key in mLoadTest.Context.Keys) 
      { 
       e.TestContextProperties.Add(key, mLoadTest.Context[key]); 
      } 
      //add the CurrentTestIteration to the TestContext 
      e.TestContextProperties.Add("TestIterationNumber", e.TestIterationNumber); 
      //add the TotalIterations to the TestContext and access from the Unit Test. 
      e.TestContextProperties.Add("TotalIterations", TotalIterations); 

     } 

     void ReadTestConfig() 
     { 
      string filePath = Path.Combine(Environment.CurrentDirectory, mLoadTest.Name + ".loadtest"); 

      if (File.Exists(filePath)) 
      { 
       string runSettings = mLoadTest.RunSettings.Name; 
       XmlDocument xdoc = new XmlDocument(); 
       xdoc.Load(filePath); 

       XmlElement root = xdoc.DocumentElement; 

       string xmlNameSpace = root.GetAttribute("xmlns"); 
       XmlNamespaceManager xmlMgr = new XmlNamespaceManager(xdoc.NameTable); 
       if (!string.IsNullOrWhiteSpace(xmlNameSpace)) 
       { 
        xmlMgr.AddNamespace("lt", xmlNameSpace); 
       } 

       var nodeRunSettings = xdoc.SelectSingleNode(string.Format("//lt:LoadTest/lt:RunConfigurations/lt:RunConfiguration[@Name='{0}']", runSettings), xmlMgr); 
       //var nodeRunSettings = xdoc.SelectSingleNode(string.Format("//lt:LoadTest", runSettings), xmlMgr); 
       if (nodeRunSettings != null) 
       { 
        TotalIterations = Convert.ToInt32(nodeRunSettings.Attributes["TestIterations"].Value); 
       } 

      } 
     } 
    } 
} 

同样,您可以读取其他值。