2017-08-29 78 views
0

我在想什麼最好的方法來切換C#的App.Config設置。這涉及我們的測試套件,我們希望選擇遠程或本地環境來啓動測試。我們使用LeanFT和NUnit作爲我們的測試框架,目前爲了讓測試能夠遠程運行,我們必須在App.config文件中添加一個<leanft></leanft>配置。當我通過命令行啓動這些測試時,如何在運行時指定不同的配置?謝謝!在運行時切換App.Config設置C#

回答

1

通過使用SDK命名空間或Report命名空間,可以在運行時修改任何配置。

下面是使用NUnit 3的例子展示如何實現這個

using NUnit.Framework; 
using HP.LFT.SDK; 
using HP.LFT.Report; 
using System; 

namespace LeanFtTestProject 
{ 
    [TestFixture] 
    public class LeanFtTest 
    { 
     [OneTimeSetUp] 
     public void TestFixtureSetUp() 
     { 
      // Initialize the SDK 
      SDK.Init(new SdkConfiguration() 
      { 
       AutoLaunch = true, 
       ConnectTimeoutSeconds = 20, 
       Mode = SDKMode.Replay, 
       ResponseTimeoutSeconds = 20, 
       ServerAddress = new Uri("ws://127.0.0.1:5095") // local or remote, decide at runtime 
      }); 

      // Initialize the Reporter (if you want to use it, ofc) 
      Reporter.Init(new ReportConfiguration() 
      { 
       Title = "The Report title", 
       Description = "The report description", 
       ReportFolder = "RunResults", 
       IsOverrideExisting = true, 
       TargetDirectory = "", // which means the current parent directory 
       ReportLevel = ReportLevel.All, 
       SnapshotsLevel = CaptureLevel.All 
      }); 

     } 

     [SetUp] 
     public void SetUp() 
     { 
      // Before each test 
     } 

     [Test] 
     public void Test() 
     { 
      Reporter.ReportEvent("Doing something", "Description"); 
     } 

     [TearDown] 
     public void TearDown() 
     { 
      // Clean up after each test 
     } 

     [OneTimeTearDown] 
     public void TestFixtureTearDown() 
     { 
      // If you used the reporter, invoke this at the end of the tests 
      Reporter.GenerateReport(); 

      // And perform this cleanup as the last leanft step 
      SDK.Cleanup(); 
     } 
    } 
} 
+0

這似乎是它會被硬編碼然而,正如我不就能夠切換它。我有一個運行本地的默認App.Config設置。我想定義一個額外的配置,我可以通過一些命令行參數進行切換。我能用這種方法做到嗎? – Tree55Topz

+0

當然。這甚至不是'LeanFT',也就是'C#' - 你定義了一個名爲'serverToUse'的變量,並將它用在'SdkConfiguration'構造函數的'ServerAddress'屬性中(AKA'new Uri(serverToUse)')。 – Adelin

+0

要解析命令行參數,您可以從[此答案]中挑選出來(https://stackoverflow.com/questions/491595/best-way-to-parse-command-line-arguments-in-c) – Adelin