2017-08-29 85 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