automationnunitapp-configleanft

Toggling App.Config settings at runtime C#


I am wondering what the best approach to toggling App.Config settings for C# would be. This is involving our test suite, and we would like the option to either choose a remote or local environment to kick the tests off. We use LeanFT and NUnit as our testing framework, and currently in order to get tests to run remote we have to add an <leanft></leanft> config in the App.config file. How can I specify different configurations at run time when I kick these tests off thru the command line? Thanks!


Solution

  • Any leanft configuration can be modified at runtime, by using the SDK namespace or the Report namespace.

    Here's an example using NUnit 3 showing how you can achieve this

    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();
            }
        }
    }