NUnit的。使用ITestListener获取测试失败的屏幕截图

时间:2016-10-26 17:39:57

标签: c# nunit screenshot event-listener

我想截取失败的测试用例的屏幕截图。但我不知道如何强迫Nunit使用我的听众。 我试图使用IAddins,但Nunit没有NUnit.Core.Extensibility lib。 我的代码:

using System;
using OpenQA.Selenium;
using NUnit.Framework.Interfaces;
using AT_MentoringPortal.Driver;
using System.Drawing.Imaging;

namespace AT_MentoringPortal.listeners
{
    public class ScreenshotListener : ITestListener
    {
        private readonly string path = ".//screens//";

        public void TestFinished(ITestResult result)
        {
            if (result.ResultState.Status == TestStatus.Failed)
            {
                IWebDriver driver = DriverFactory.GetDriver();
                this.MakeScreenshot(driver, result.Name);
            }
        }

        public void MakeScreenshot(IWebDriver driver, string testName)
        {
            string timestamp = DateTime.Now.ToString("yyyy-MM-dd-hhmm-ss");
            var screenshot = ((ITakesScreenshot)driver).GetScreenshot();
            screenshot.SaveAsFile($"{this.path}{timestamp} {testName}", ImageFormat.Jpeg);
        }

        public void TestOutput(TestOutput output)
        {
           // throw new NotImplementedException();
        }

        public void TestStarted(ITest test)
        {
           // throw new NotImplementedException();
        }
    }
}

请告诉我如何在测试课中开始我的听众。

1 个答案:

答案 0 :(得分:0)

ITestListener是NUnit自身在运行测试时使用的内部接口。在NUnit V2(TestListener)中有一个类似的界面,你可以创建使用它的插件。 NUnit 3没有像NUnit 2那样的插件,尽管它可以通过其他方式扩展。

您是否只想为某些测试保存屏幕截图?或者对于某个灯具​​中的每个测试?或者更一般地说?

要在夹具中执行此操作,可以使用OneTimeTearDown方法。

相关问题