在运行时切换 App.Config 设置 C#

Toggling App.Config settings at runtime C#

我想知道切换 C# App.Config 设置的最佳方法是什么。这涉及到我们的测试套件,我们希望可以选择远程或本地环境来启动测试。我们使用 LeanFT 和 NUnit 作为我们的测试框架,目前为了对 运行 远程进行测试,我们必须在 App.config 文件中添加一个 <leanft></leanft> 配置。当我通过命令行启动这些测试时,如何在 运行 时间指定不同的配置?谢谢!

通过使用 SDK 命名空间或 Report 命名空间,可以在运行时修改任何 leanft 配置。

这是一个使用 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();
        }
    }
}