2017-08-29 5 views
0

C#のApp.Config設定を切り替える最適な方法は何でしょうか。これにはテストスイートが含まれており、リモートまたはローカルの環境を選択してテストをオフにすることもできます。私たちはテストフレームワークとしてLeanFTとNUnitを使用しています。現在は、テストをリモートで実行するために、<leanft></leanft>の設定をApp.configファイルに追加する必要があります。コマンドラインでこれらのテストを実行するときに、実行時に異なる設定を指定するにはどうすればよいですか?ありがとう!実行時のApp.Config設定の切り替えC#

答えて

1

SDK名前空間またはReport名前空間を使用して、実行時に任意の構成を変更できます。ここで

は、あなたが、私はそれを切り替えることができないだろうのようにそれは難しい、しかし、コード化されることになるようにこれは思えるこの

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

を達成する方法を示すNUnitの3を使用した例です。私はローカルで実行されるデフォルトの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

関連する問題