簡體   English   中英

如何為多個 NUnit 測試創建單個/全局日志文件

[英]How to a create single/global log file for multiple NUnit tests

我正在 Visual Studio 測試資源管理器中執行一些寫入日志文件的測試(我正在使用 Serilog)。 目前,每次我的 BaseTest 執行LoggingFactory.InitializeLoggingFactory()它都會創建一個新的日志文件,因此我為每個測試 class 有多個日志。 如何全局調用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
            }
        }
    }

測試資源管理器

日志

您在夾具 class 中使用了[OneTimeSetUp] ,因此該方法對每個夾具執行一次。 它位於基礎 class 中的事實並沒有減少它的運行次數,因為基礎 class 是每個夾具的邏輯部分。

您需要創建一個 SetUpFixture,一個標有[SetUpFixture]的 class 並將一次性設置方法放在那里。 如果您希望整個程序集的日志只初始化一次,請將 SetUpFixture class 放置在任何命名空間之外。

還有一件事......不要從 SetUpFixture 繼承任何 TestFixtures ......它獨立存在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM