如何为多个NUnit测试创建单个/全局日志文件

问题描述 投票:0回答:1

我正在Visual Studio Test Explorer中执行一些写入日志文件的测试(我正在使用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
            }
        }
    }

test explorer

logs

c# selenium nunit serilog
1个回答
0
投票

您在灯具类中使用了[OneTimeSetUp],因此该方法对每个灯具执行一次。它在基类中的事实并不会使它运行更少的时间,因为基类是每个固定装置的逻辑部分。

您需要创建一个SetUpFixture,一个标有[SetUpFixture]的类,然后在其中放置一次性设置方法。如果要对整个程序集只初始化一次日志,请将SetUpFixture类放在任何名称空间之外。

© www.soinside.com 2019 - 2024. All rights reserved.