【问题标题】:How to a create single/global log file for multiple NUnit tests如何为多个 NUnit 测试创建单个/全局日志文件
【发布时间】:2020-05-05 22:36:29
【问题描述】:

我正在 Visual Studio 测试资源管理器中执行一些写入日志文件的测试(我正在使用 Serilog)。目前,每次我的 BaseTest 执行LoggingFactory.InitializeLoggingFactory()它都会创建一个新的日志文件,因此每个测试类都有多个日志。如何在全局范围内调用 LoggingFactory.InitializeLoggingFactory() 以便每次执行所有测试都可以得到 1 个日志文件?

    abstract class BaseTest
    {
        protected static IWebDriver driver;
        protected static WebDriverWait wait;
        protected static Actions actions;
        protected static ExcelDataFactory excelDataFactory;

        [OneTimeSetUp]
        public void startLogger()
        {
            LoggingFactory.InitializeLoggingFactory();
        }

        [SetUp]
        public void Initialize()
        {
            driver = new Browser().ConfigureBrowser(MyProps.Default.Browser);
            wait = new WebDriverWait(driver, TimeSpan.FromSeconds(MyProps.Default.TimeOut));
            actions = new Actions(driver);

            excelDataFactory = new ExcelDataFactory();
            ScreenshotFactory.SetDriverForScreenshot(driver);
            ExceptionTracker.RemoveAllExceptions();
        }

        [TearDown]
        public void CleanUp()
        {
            ExceptionTracker.PrintAllExceptions();

            if (driver != null)
            {
                driver.Close(); // Close the chrome window
                driver.Quit(); // Close the console app that was used to kick off the chrome window
                driver.Dispose(); // Close the driver.exe
            }
        }
    }

【问题讨论】:

    标签: c# selenium nunit serilog


    【解决方案1】:

    您在夹具类中使用了[OneTimeSetUp],因此该方法对每个夹具执行一次。它在基类中的事实并没有减少它的运行次数,因为基类是每个夹具的逻辑部分。

    您需要创建一个SetUpFixture,一个标有[SetUpFixture] 的类,并将一次性设置方法放在那里。如果您希望整个程序集的日志只初始化一次,请将 SetUpFixture 类放在任何命名空间之外。

    还有一件事...不要从 SetUpFixture 继承任何 TestFixture...它是独立存在的。

    【讨论】:

    • 谢谢查理,我会试试你说的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多