2013-09-23 38 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); 
       } 

      } 
     } 
    } 
} 

同樣,您可以讀取其他值。